今天就跟大家聊聊有关thinkphp缓存技术的原理是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
Thinkphp缓存文件的配置
Home是我建立的前台项目,在Home\Conf\config.php找到缓存的配置文件,配置如下:
<?php
return array(
'DB_TYPE'=>'mysql',
'DB_HOST'=>'127.0.0.1',
'DB_NAME'=>'w3note',
'DB_USER'=>'root',
'DB_PWD'=>'123456',
'DB_PORT'=>'3306',
'DB_PREFIX'=>'w3_',
'DATA_CACHE_TYPE'=>'file',//设置缓存方式为file
'DATA_CACHE_TIME'=>'600',//缓存周期600秒
);
?>
Thinkphp缓存函数的使用
在thinkphp中,我喜欢使用快捷缓存函数S()进行缓存,其用法如下:
S('data',$Data);//使用data标识缓存$Data数据
S('data',$Data,600);// 缓存$Data数据600秒
$Data = S('data');// 获取缓存数据
S('data',NULL);// 删除缓存数据
下面是是前台项目控制器的完整代码:
<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action{
public function index(){
//如果有缓存,则读取缓存数据
//如果没有缓存,则读取数据库当中的数据放入缓存
$lists=S('lists');
if(emptyempty($lists)){
$news=M('news');
$lists=$news->select();
S('lists',$lists,600);
echo '这是直接读取数据库的数据';
}
dump($list);
?>
访问http://127.0.0.1/Home/index.php/Index/index 输出,这是直接读取数据库的数据:
array(10) {
[0] => array(12) {
["id"] => string(1) "1"
["catid"] => string(2) "13"
["title"] => string(4) "thinkphp的缓存技术"
["content"] => string(8) "thinkphp的缓存技术"
["tags"] => string(4) "缓存"
["thumb"] => string(0) ""
["description"] => string(7) "thinkphp的缓存技术"
["inputtime"] => string(10) "1348370202"
["posid"] => string(1) "1"
["ord"] => string(1) "2"
["hits"] => string(1) "1"
["status"] => string(1) "1"
}看完上述内容,你们对thinkphp缓存技术的原理是什么有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注天达云行业资讯频道,感谢大家的支持。