ThinkPHP 3.2.3 中手册中数据缓存的地址是:http://www.kancloud.cn/manual/thinkphp/1835
静态缓存的地址是:http://www.kancloud.cn/manual/thinkphp/1839
数据缓存
使用 S 方法进行数据缓存,缓存文件默认的方式是文件缓存(DATA_CACHE_TYPE = File),文件缓存默认的保存路径是 ./Application/Runtime/Temp
当使用默认的缓存方式时,不需要在配置文件中进行配置,直接在控制器中需要缓存数据的地方调用 S 方法即可:
S(缓存名,缓存值,缓存时间)
例如在 IndexController.class.php(./Application/Home/Controller/IndexController.class.php)要对首页数据进行缓存:
<?php namespace Home\Controller; use Think\Controller; use Admin\Common\Category;
class IndexController extends Controller { //首页 public function index(){ 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
<?php //000000086400a:7:{i:0;a:5:{s:2:"id";s:1:"1";s:4:"name";s:4:"HTML";s:3:"pid";s:1:"0";s:4:"sort";s:1:"1";s:4:"blog";a:0:{}}i:1;a:5:{s:2:"id";s:1:"3";s:4:"name";s:10:"JavaScript";s:3:"pid";s:1:"0";s:4:"sort";s:1:"3";s:4:"blog";a:1:{i:0;a:3:{s:2:"id";s:1:"1";s:5:"title";s:2:"js";s:4:"time";s:10:"1455438771";}}}i:2;a:5:{s:2:"id";s:1:"4";s:4:"name";s:3:"PHP";s:3:"pid";s:1:"0";s:4:"sort";s:1:"4";s:4:"blog";a:3:{i:0;a:3:{s:2:"id";s:1:"4";s:5:"title";s:20:"thinkphp关联模型";s:4:"time";s:10:"1456541875";}i:1;a:3:{s:2:"id";s:1:"3";s:5:"title";s:15:"无限极分类";s:4:"time";s:10:"1456541682";}i:2;a:3:{s:2:"id";s:1:"2";s:5:"title";s:6:"对象";s:4:"time";s:10:"1455438885";}}}i:3;a:5:{s:2:"id";s:1:"2";s:4:"name";s:7:"DIV+CSS";s:3:"pid";s:1:"0";s:4:"sort";s:1:"5";s:4:"blog";a:0:{}}i:4;a:5:{s:2:"id";s:1:"5";s:4:"name";s:5:"MySQL";s:3:"pid";s:1:"0";s:4:"sort";s:1:"5";s:4:"blog";a:0:{}}i:5;a:5:{s:2:"id";s:1:"6";s:4:"name";s:5:"Linux";s:3:"pid";s:1:"0";s:4:"sort";s:1:"6";s:4:"blog";a:0:{}}i:6;a:5:{s:2:"id";s:1:"8";s:4:"name";s:6:"其他";s:3:"pid";s:1:"0";s:4:"sort";s:1:"7";s:4:"blog";a:0:{}}} ?>