数据缓存
使用 S 方法进行数据缓存,缓存文件默认的方式是文件缓存(DATA_CACHE_TYPE = File),文件缓存默认的保存路径是 ./Application/Runtime/Temp
当使用默认的缓存方式时,不需要在配置文件中进行配置,直接在控制器中需要缓存数据的地方调用 S 方法即可:
S(缓存名,缓存值,缓存时间)
例如在 IndexController.class.php(./Application/Home/Controller/IndexController.class.php)要对首页数据进行缓存:
namespace Home\Controller;useThink\Controller;useAdmin\Common\Category;class IndexController extendsController {//首页
public functionindex(){if(!$top_cate = S('index_list')) {//读取顶级栏目
$top_cate = M('cate')->where(array('pid'=>0))->order('sort')->select();$cate = M('cate')->order('sort')->select();$bObj = M('blog');$field = array('id','title','time');foreach($top_cate as $key=>$val) {$cids = Category::get_children_id($cate,$val['id']);$cids[] = $val['id'];$where = array('cid'=>array('IN',$cids));$top_cate[$key]['blog'] = $bObj->field($field)->where($where)->order('time DESC')->select();
}//缓存
S('index_list',$top_cate,3600*24);//1天,默认存储路径是 ./Application/Home/Runtime/Temp
}$this->assign('top_cate',$top_cate);$this->display();
}
}
此时 ./Application/Runtime/Temp 中生成了 823c3bcf17c6b7276fa8799355c4c7c8.php
?>
View Code
如果要改变缓存方式,例如不再把缓存存入文件,而是存入 Memcached 中,该项目中 PHP 的 Memcached 扩展是 Memcached:
此时可以通过修改配置文件 ./Applicaiton/Home/Common/Conf/conf.php,添加:
'DATA_CACHE_TYPE'=>'Memcached',
'PERSISTENTID' => 'mlm_cache',//持久链接标示
'MEMCACHED_HOST' => '127.0.0.1', //可数组