CodeIgniter自带缓存功能使用
准备:
自己写一个缓存的类,存放在libraries中
class My_cache{
private $ci;
public function __construct(){
$this->ci =& get_instance();
$this->ci->load->driver('cache',array('adapter' => 'file'));
}
public function save($id,$data,$time=0){
if($time==0){
$time=7200;
}
$this->ci->cache->save($id,$data,$time);
return true;
}
public function get($id){
if ( ! $value = $this->ci->cache->get($id))
{
return null;
}else{
return $value;
}
}
}
使用前先引用
public function __construct() {
parent::__construct();
$this->load->library('my_cache');
}
1、获取缓存内容
$this->my_cache->get("index_news")
2、写入缓存内容
$this->my_cache->save("index_news", $news_list,30*60)