ZendFramework中使用Cache缓存机制

如下图所示建立工程:

代码如下:

  1. <?php
  2. /**
  3.  * IndexController - The default controller class
  4.  * 
  5.  * @author
  6.  * @version 
  7.  */
  8. require_once 'Zend/Controller/Action.php';
  9. require_once 'Zend/Cache.php';
  10. require_once 'Zend/Registry.php';
  11. require_once 'Zend/Db.php';
  12. require_once 'Zend/Db/Table.php';;
  13. class IndexController extends Zend_Controller_Action 
  14. {
  15.     public function init() 
  16.     { 
  17.         $params = array ('host' => 'localhost'
  18.                          'username' => 'root'
  19.                          'password' => 'root'
  20.                          'dbname'   => 'mysql'); 
  21.         $db = Zend_Db::factory('Pdo_Mysql'$params); 
  22.         Zend_Db_Table::setDefaultAdapter($db); 
  23.         Zend_Registry::set('db'$db); 
  24.     } 
  25.     
  26.     public function indexAction() 
  27.     {
  28.         $frontendOptions = array(
  29.             'lifeTime' => 7200, // 两小时的缓存生命期
  30.             'automatic_serialization' => true
  31.         );
  32.         
  33.         $backendOptions = array(
  34.             'cache_dir' => 'C:/tmp/' // 放缓存文件的目录
  35.         );
  36.         
  37.         // 取得一个Zend_Cache_Core 对象
  38.         $cache = Zend_Cache::factory('Core'
  39.                                      'File'
  40.                                      $frontendOptions
  41.                                      $backendOptions);
  42.         if(!$result = $cache->load('myresult')) {
  43.             // 缓存不命中;连接到数据库
  44.             $dbAdapter = Zend_Registry::get('db');
  45.             $result = $dbAdapter->query('SELECT * FROM user')->fetchAll();
  46.             $cache->save($result'myresult');
  47.             echo "This one is from database!<br/>";
  48.         } else {
  49.             // cache hit! shout so that we know
  50.             echo "This one is from cache!<br/>";
  51.         }
  52.         print_r($result);                           
  53.     }
  54. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值