emlog使用的是文件缓存了,不管文件缓存多好也好不过memcache缓存了,关于memcache缓存优于文件缓存我们就不介绍了,下面简单的看看配置方法吧.
这次只是简单的HACK emlog cache程序,使用memcache缓存,毕竟memcache缓存在内存, 文件缓存在硬盘(要看I/O的性能),一般来说内存的性能大于硬盘,所以一般来说memcache缓存优于文件缓存.
memcache相对于文件缓存的优点:
1、读写性能优异,特别是高并发时和文件缓存比有明显优势.
2、memcached组建支持集群,并且是自动管理负载均衡.
注意:memcache的原理是内存分块,单个item大于1M的数据存memcache和读取速度可能有点慢.
具体的情况这边我这里就不测试了,大家可以帮忙测试看看.
1、替换文件缓存为memcache缓存
2、去除文件缓存写入和读取
注意:虽然不涉及数据库操作,但是还是请在修改前备份数据.
1、首先添加memcache类文件Mcache.php,放在include/lib文件夹下,服务器地址和端口地址在该文件中,请你自己配置,代码如下:
-
-
-
-
-
-
- class MCache{
- private static $_instance;
- private static $_connect_type = '';
- private $_memcache;
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
- public function set($key, $value,$flag = 0 ,$expire_t(www.111cn.net)ime = 0){
- $this->_memcache->set($key, $value, $flag, $expire_time);
- }
-
-
-
-
-
-
-
-
- public function replace($key, $value,$flag = 0 , $expire_time = 0){
- $this->_memcache->replace($key, $value, $flag, $expire_time);
- }
-
-
-
-
-
- public function get($key){
- return $this->_memcache->get($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,添加实例,第29行修改为如下代码:
- 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即可. |