|

楼主 |
发表于 2011-7-27 00:33:10
|
显示全部楼层
还是自己搞定了,我把步骤和代码贴出来,以供未来人直接用
1、在application/models里面建立文件memcache_model.php(注意文件名和大小写)
内容如下PHP复制代码 <?php
class Memcache_model extends CI_Model {
public static $memcached = array(
‘maingroup ' => array(
array('host ' => '192 .168 .0 .1 ', 'port ' => '11211', 'weight ' => 10),
array('host ' => '192 .168 .0 .2 ', 'port ' => '11211', 'weight ' => 50),
),
'other ' => array(
array('host ' => '192 .168 .0 .3 ', 'port ' => '11212', 'weight ' => 10),
array('host ' => '192 .168 .0 .4 ', 'port ' => '11211', 'weight ' => 50),
)
);
final public function instance($group='other ') {
$memcache = new Memcache;
foreach (self: memcached[$group] as $v) {
$memcache->addserver($v['host '], $v['port '], 1, $v['weight ']); //1 表示长连接
}
return $memcache;
}
}
复制代码
2、在application/controllers下面建立cachetest.php文件
内容如下:
PHP复制代码 <?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Cachetest extends CI_Controller {
function __construct () {
parent ::__construct ();
$this->load->model('Memcache_model');
}
function index () {
$key = 'good';
$value = 'goodvalue';
$memcacheObj = $this->Memcache_model->instance('other');
$memcacheObj->set($key, $value);
print_r($memcacheObj->get($key));
}
} 复制代码
3、运行http://XXX.XXX.com/cachetest 看看效果吧! |
|