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文件夾下,服務器地址和端口地址在該文件中,請你自己配置,代碼如下:
- /**
- * 此類為單例模式,取得實例方法: $cache = MCache::getInstance();
- * @author Star.Yu <vip@myxzy.com>
- * @date 2014.5.25
- *
- */
- 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_t(www.111cn.net)ime = 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,添加實例,第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);
- }
- /**
- * 讀取緩存文件Vevb.com
- */
- 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即可.
新聞熱點
疑難解答