Zend_Cache

Zend_Cache, Zend_Config, Zend_Console_Getopt, Zend_Filter, Zend_Log, Zend_Memory, Zend_Registry, Zend_Validate

Moderador: victortorres

Zend_Cache

Mensagempor Alexandre » Seg Ago 10, 2009 12:37 pm

Estou utilizando o Zend_Cache e estou tendo um problema para carregar o cache antes de ler o meu arquivo de configuração que está em xml, ou seja, ele faz o caching das páginas, mas o arquivo de configuração está sendo lido várias vezes. Quero saber como resolvo este problema, pois quero incluir no cache o arquivo de configuração para não ser carregado toda hora?

Segue o código abaixo o código do meu bootstrap.php

Código: Selecionar tudo
protected function _initAutoload() {
      $loader = new Zend_Loader_Autoloader_Resource(array(
         'namespace' => 'RG',
         'basePath'  => APPLICATION_PATH,
         'resourceTypes' => array(
            'models' => array(
               'namespace' => 'Model',
               'path' => 'models/'
            ),
            'plugins' => array(
               'namespace' => 'Plugin',
               'path' => 'library/Plugins/'
            )
         )
      ));
      return $loader;
   }

   protected function _initRegistry() {
      $path = APPLICATION_PATH . '/sites/'.MODULE.'/config.xml';
      if (!file_exists($path)) {
         throw new Exception('Não foi possível localizar o arquivo de configuração para o módulo.');
      }
      $config = new Zend_Config_Xml($path);
      $this->config = $config;
      Zend_Registry::set('config', $config);
   }

   protected function _initFrontController() {
      $controller = Zend_Controller_Front::getInstance();
      $controller->setControllerDirectory(APPLICATION_PATH ."/sites/". MODULE ."/controllers")
         ->setDefaultControllerName('home')
         ->registerPlugin(new RG_Plugin_Caching(array(
            'frontend' => 'Output',
            'backend' => 'File',
            'frontendOptions' => array(
               'lifetime' => 1200, // duração do cache, 20 minutos
               'automatic_serialization' => true
            ),
            'backendOptions' => array(
               'cache_dir' => APPLICATION_PATH ."/sites/". MODULE ."/cache"
            )
         )));

      return $controller;
   }

   protected function _initView() {
      $view = new Zend_View();
      $view->addHelperPath(APPLICATION_PATH .'/library/Helpers/');
      $view->doctype('XHTML1_STRICT');
      $view->setEncoding('ISO-8859-1');
      $view->setEscape('htmlentities');

      $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
      Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

      Zend_Layout::startMvc(array(
         'layout' => 'layout',
         'layoutPath' => APPLICATION_PATH ."/sites/". MODULE ."/view/scripts"
      ));

      return $view;
   }


Minha classe de Cache

Código: Selecionar tudo
       public function __construct($options) {
      if ($options instanceof Zend_Config) {
         $options = $options->toArray();
      }
      if (!is_array($options)) {
         throw new Exception('Cache inválido');
      }

      if (array('frontend', 'backend', 'frontendOptions', 'backendOptions') != array_keys($options)) {
         throw new Exception('Invalid cache options provided');
      }
      //$options['frontendOptions']['automatic_serialization'] = true;
      $this->cache = Zend_Cache::factory(
         $options['frontend'],
         $options['backend'],
         $options['frontendOptions'],
         $options['backendOptions']
      );
   }

   /**
    * Start caching
    *
    * Determine if we have a cache hit. If so, return the response; else,
    * start caching.
    *
    * @param  Zend_Controller_Request_Abstract $request
    * @return void
    */
   public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
      if (!$request->isGet() || ('development' === APPLICATION_ENV)) {
         self::$doNotCache = true;
         return;
      }

      $path = $request->getPathInfo();
      $this->key = md5($path);
      if (false !== ($response = $this->cache->load($this->key))) {
         $response->sendResponse();
         exit;
      }
   }

   /**
    * Store cache
    *
    * @return void
    */
   public function dispatchLoopShutdown() {
      if (self::$doNotCache
         || $this->getResponse()->isRedirect()
         || (null === $this->key)
      ) {
         return;
      }

      $this->cache->save($this->getResponse(), $this->key);
   }
Alexandre
Leitor
 
Mensagens: 1
Data de registro: Seg Ago 10, 2009 12:06 pm

Re: Zend_Cache

Mensagempor 3dingbat » Ter Ago 25, 2009 6:56 pm

Tenta com esse cod aqui>

Código: Selecionar tudo
    public $cache = null;
   
    public function __construct($options) {
        if ($options instanceof Zend_Config) $options = $options->cache->toArray();
        if (!is_array($options)) throw new Exception('Invalid cache options; must be array or Zend_Config object');
        if (array('frontendOptions', 'backendOptions') != array_keys($options)) throw new Exception('Invalid cache options provided');
        $this->cache = Zend_Cache::factory('Page','File',$options['frontendOptions'],$options['backendOptions']);
    }
   
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
        $id = $request->getModuleName().'_'.$request->getControllerName().'_'.$request->getActionName();
        $this->cache->start($id);
    }
   
    public function dispatchLoopShutdown() {
        if($this->getResponse()->isRedirect())
            $this->cache->cancel();
    }
3dingbat
Leitor
 
Mensagens: 1
Data de registro: Dom Set 28, 2008 10:20 am


Retornar para Core

Quem está online

Usuários vendo este fórum: Nenhum usuário registrado online e 0 visitantes