emblog 缓存强化配置方案
1.memcache缓存配置(1)mcache.php 目录include/lib
内容:
<?php
class MCache{
private static $_instance;
private static $_connect_type = '';
private $_memcache;
/**
* 私有化构造函数,禁止使用关键字new来实例Mcache类
*/
private function __construct() {
if (!class_exists('Memcache')) {
throw new Exception('Class Memcache not exists');
}
$conn = self::$_connect_type;
$this->_memcache = new Memcache();
$this->_memcache->$conn('localhost', '11211');
}
/**
* 克隆私有化,禁止克隆实例
*/
private function __clone() {}
/**
* 类入口,通过此静态方法对类进行实例化
*/
public static function getInstance($type = 'connect'){
self::$_connect_type = ($type == 'connect') ? $type : 'pconnect';
if (!self::$_instance instanceof self) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* 把数据添加到缓存
* @param string $key 缓存的key
* @param string|array|int... $value 缓存的数据
* @param int $flag使用zlib MEMCACHE_COMPRESSED
* @param int $expire_time缓存时间
*/
public function set($key, $value,$flag = 0 ,$expire_time = 0){
$this->_memcache->set($key, $value, $flag, $expire_time);
}
/**
* 替换缓存数据
* @param string $key 缓存的key
* @param string|array|int... $value 缓存的数据
* @param int $flag使用zlib MEMCACHE_COMPRESSED
* @param int $expire_time缓存时间
*/
public function replace($key, $value,$flag = 0 , $expire_time = 0){
$this->_memcache->replace($key, $value, $flag, $expire_time);
}
/**
* 从缓存读取数据
* @param string|array|int... $key
*/
public function get($key){
return $this->_memcache->get($key);
}
/**
* 从缓存删除数据
* @param string|array|int... $key
*/
public function del($key,$expire_time = 0){
$this->_memcache->delete($key, $expire_time);
}
public function close(){
return $this->_memcache->close();
}
}
(2)修改include/lib/cache.php
搜索
private function __construct() {
$this->db = Database::getInstance();
}
改为:
private function __construct() {
$this->db = Database::getInstance();
$this->memcache = MCache::getInstance();
}
搜索
function cacheWrite ($cacheData, $cacheName) {
$cachefile = EMLOG_ROOT . '/content/cache/' . $cacheName . '.php';
$cacheData = "<?php exit;//" . $cacheData;
@ $fp = fopen($cachefile, 'wb') OR emMsg('读取缓存失败。如果您使用的是Unix/Linux主机,请修改缓存目录 (content/cache) 下所有文件的权限为777。如果您使用的是Windows主机,请联系管理员,将该目录下所有文件设为可写');
@ $fw = fwrite($fp, $cacheData) OR emMsg('写入缓存失败,缓存目录 (content/cache) 不可写');
$this->{$cacheName.'_cache'} = null;
fclose($fp);
}
改为:
function cacheWrite ($cacheData, $cacheName) {
$this->memcache->set($cacheName,$cacheData);
}
搜索
function readCache($cacheName) {
if ($this->{$cacheName.'_cache'} != null) {
return $this->{$cacheName.'_cache'};
} else {
$cachefile = EMLOG_ROOT . '/content/cache/' . $cacheName . '.php';
// 如果缓存文件不存在则自动生成缓存文件
if (!is_file($cachefile) || filesize($cachefile) <= 0) {
if (method_exists($this, 'mc_' . $cacheName)) {
call_user_func(array($this, 'mc_' . $cacheName));
}
}
if ($fp = fopen($cachefile, 'r')) {
$data = fread($fp, filesize($cachefile));
fclose($fp);
clearstatcache();
$this->{$cacheName.'_cache'} = unserialize(str_replace("<?php exit;//", '', $data));
return $this->{$cacheName.'_cache'};
}
}
}
改为:
function readCache($cacheName) {
if($this->memcache->get($cacheName)===false){call_user_func(array($this, 'mc_' . $cacheName));}
$data = $this->memcache->get($cacheName);
$this->{$cacheName.'_cache'} = unserialize($data);
return $this->{$cacheName.'_cache'};
}
2.文件缓存配置
页:
[1]