codeigniter 2.2.0 对memcache的使用,貌似存在一点bug,官方暂未修补,暂时需要改动到其基类,方法如下,仅供参考:
1. system\libraries\Cache\drivers\Cache_memcached.php
about line 165:
$this->_memcached = new Memcached();
改为:
if(extension_loaded('memcached')){
$this->_memcached = new Memcached();
}elseif (extension_loaded('memcache')){
$this->_memcached = new Memcache();
}else{
log_message('error', 'At least one memcache extension need be loaded!');
}
(以上是参考某个大神的分享)
about line 201
if ( ! extension_loaded('memcached'))
改为:
if ( ! extension_loaded('memcached') && ! extension_loaded('memcache') )
2. 增加 application/config/memcached.php 文件 (有多个服务器则自己增加多个元素)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* MC配置文件
* Create at 2014-10-09
*/
$memcached = array(
'server1' => array(
'hostname' => '127.0.0.1',
'port' => '11211',
'weight' => 1
)
);
$config['memcached'] = $memcached;
3. 修改 application/config/config.php 在最后增加
// Memcache enable
$config['memcache_enable'] = true;
4. 修改 application/config/autoload.php 大概在83行
$autoload['config'] = array('memcached');
5. 在你要使用的地方,加上
$this->load->driver('cache');
增加一个mc则使用:
$this->cache->memcached->save('foo', 'baddr', 10);
获取一个mc则使用:
$getCache = $this->cache->memcached->get('foo');
|