这次只是简单的hack emlog cache程序,使用memcache缓存,毕竟memcache缓存在内存, 文件缓存在硬盘(要看i/o的性能),一般来说内存的性能大于硬盘,所以一般来说memcache缓存优于文件缓存。
memcache相对于文件缓存的优点:
1、读写性能优异,特别是高并发时和文件缓存比有明显优势。
2、memcached组建支持集群,并且是自动管理负载均衡
注意:memcache的原理是内存分块,单个item大于1m的数据存memcache和读取速度可能有点慢。
具体的情况这边我这里就不测试了,大家可以帮忙测试看看。
[2014-05-25]memcache 1.0beta
1、替换文件缓存为memcache缓存
2、去除文件缓存写入和读取
注意:虽然不涉及数据库操作,但是还是请在修改前备份数据
1、首先添加memcache类文件mcache.php,放在include/lib文件夹下,服务器地址和端口地址在该文件中,请你自己配置
代码如下 复制代码
_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,添加实例,第28行修改为
代码如下 复制代码
private function __construct() {
$this->db = database::getinstance();
$this->memcache = mcache::getinstance();
} 3、修改include/lib/cache.php的读写memcache缓存,大概507行下面的cachewrite和readcache函数修改为
/**
* 写入缓存
*/
function cachewrite ($cachedata, $cachename) {
$this->memcache->set($cachename,$cachedata);
}
/**
* 读取缓存文件
*/
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'};
}
到此修改已经完毕,已经缓存了文件缓存到memcache缓存中了,如有什么问题可以留言评论。
卸载方法:如果不想用memcache缓存了,就用原版的cache.php替换掉修改的cache.php,然后删除mcache.php即可
