Advertisement
dragonbe

Example of Caching with Core

Mar 2nd, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class UserController extends Zend_Controller_Action
  4. {
  5.     protected $_cache;
  6.  
  7.     public function init()
  8.     {
  9.         $this->_cache = Zend_Cache('Core','File', array ('automatic_serialization' => true), array ('cache_dir' => APPLICATION_PATH . '/cache));
  10.    }
  11.  
  12.    public function getUserAction()
  13.    {
  14.         $userId = $this->getRequest()->getParam('id', 0);
  15.         if (!$user = $this->_cache->load('USER_FROM_DB_' . $userId)) {
  16.             $user = new Application_Model_User();
  17.             $userMapper = new Application_Model_UserMapper();
  18.             $userMapper->find($user, $userId);
  19.             $this->_cache->save($user, 'USER_FROM_DB_' . $userId);
  20.         }
  21.         // value of $user now is an Application_Model_User instance
  22.         ...
  23.    }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement