tp3.2缓存方式mysql_ThinkPHP 3.2.3 数据缓存与静态缓存

本文介绍了ThinkPHP 3.2.3中如何使用数据缓存和静态缓存。数据缓存通过S方法实现,默认采用文件缓存,也可以配置为Memcached。实例中展示了如何将首页数据缓存到文件和Memcached,并给出了配置及代码示例。静态缓存则通过HTML_CACHE_ON配置开启,可以为特定控制器方法生成静态HTML文件,提高页面访问速度。
摘要由CSDN通过智能技术生成

数据缓存

使用 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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

?>

View Code

如果要改变缓存方式,例如不再把缓存存入文件,而是存入 Memcached 中,该项目中 PHP 的 Memcached 扩展是 Memcached:

dc853c94838fc9b7ec213f5369bdb9af.png

此时可以通过修改配置文件 ./Applicaiton/Home/Common/Conf/conf.php,添加:

'DATA_CACHE_TYPE'=>'Memcached',

'PERSISTENTID' => 'mlm_cache',//持久链接标示

'MEMCACHED_HOST' => '127.0.0.1', //可数组

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值