首頁  >  文章  >  後端開發  >  thinkphp 缓存入门教程

thinkphp 缓存入门教程

WBOY
WBOY原創
2016-07-25 08:52:18878瀏覽
  1. return array(
  2. 'DB_TYPE'=>'mysql',
  3. 'DB_HOST'=>'127.0.0.1',
  4. 'DB_NAME'=>'w3note',
  5. 'DB_USER'=>'root',
  6. 'DB_PWD'=>'123456',
  7. 'DB_PORT'=>'3306',
  8. 'DB_PREFIX'=>'w3_',
  9. 'DATA_CACHE_TYPE'=>'file',//设置缓存方式为file
  10. 'DATA_CACHE_TIME'=>'600',//缓存周期600秒
  11. );
  12. ?>
复制代码

Thinkphp缓存函数的使用 在thinkphp中用快捷缓存函数S()进行缓存,例如:

  1. // 本类由系统自动生成,仅供测试用途
  2. class IndexAction extends Action{
  3. public function index(){
  4. //如果有缓存,则读取缓存数据
  5. //如果没有缓存,则读取数据库当中的数据放入缓存
  6. $lists=S('lists');
  7. if(emptyempty($lists)){
  8. $news=M('news');
  9. $lists=$news->select();
  10. S('lists',$lists,600);
  11. echo '这是直接读取数据库的数据';
  12. }
  13. dump($list);
  14. ?>
复制代码

访问http://127.0.0.1/Home/index.php/Index/index 输出 直接读取数据库的数据:

  1. array(10) {
  2. [0] => array(12) {
  3. ["id"] => string(1) "1"
  4. ["catid"] => string(2) "13"
  5. ["title"] => string(4) "thinkphp的缓存技术"
  6. ["content"] => string(8) "thinkphp的缓存技术"
  7. ["tags"] => string(4) "缓存"
  8. ["thumb"] => string(0) ""
  9. ["description"] => string(7) "thinkphp的缓存技术"
  10. ["inputtime"] => string(10) "1348370202"
  11. ["posid"] => string(1) "1"
  12. ["ord"] => string(1) "2"
  13. ["hits"] => string(1) "1"
  14. ["status"] => string(1) "1"
  15. }
复制代码

注意,第一次运行时,会打印出如上面所示信息,刷新一下页面后,少了“ 这是直接读取数据库的数据",即读取的是先前生成的缓存数据。



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn