Cache resource for the Zend Framework

Hello. Just want to say that this article is intended for those already familiar with this framework. In new versions of Zend Framework, the concept of initialization of application components using resource plugins. In the standard delivery there is a sufficient number of them, but one I never found. So is the cache. Surfing the Internet and not finding peers, I decided to write my own. So, let's proceed.



Custom resources will be stored in the folder library/app/application/resource. Put the file there Cache.php with sleduyushim source:

<?php

class App_Application_Resource_Cache extends Zend_Application_Resource_Resourceabstract
{
/**
* Default registry key
*/
const DEFAULT_REGISTRY_KEY = 'App_Cache';

/**
* the Cache instance
*
* @var Zend_Cache
*/
protected $_cache = null;

/**
* Inititalize cache resource
*
* @return Zend_Cache
*/
public function init ()
{
return $this->getCache();
}

/**
* Return the cache instance
*
* @return Zend_Cache
*/
public function getCache ()
{
if (null === $this->_cache) {
$options = $this->getOptions();

/// create cache instance
$this->_cache = Zend_Cache::factory(
$options['frontend']['adapter']
$options['backend']['adapter']
$options['frontend']['params']
$options['backend']['params']
);

/// use as default database metadata cache
if (isset($options['isDefaultMetadataCache']) && true === (bool) $options['isDefaultMetadataCache']) {
Zend_Db_Table_Abstract::setDefaultMetadataCache($this->_cache);
}

/// use as default translate cache
if (isset($options['isDefaultTranslateCache']) && true === (bool) $options['isDefaultTranslateCache']) {
Zend_Translate::setCache($this->_cache);
}

/// use as default locale cache
if (isset($options['isDefaultLocaleCache']) && true === (bool) $options['isDefaultLocaleCache']) {
Zend_Locale::setCache($this->_cache);
}

/// add to registry
$key = (isset($options['registry_key']) && !is_numeric($options['registry_key'])) ? $options['registry_key'] : self::DEFAULT_REGISTRY_KEY;
Zend_Registry::set($key, $this->_cache);
}
return $this->_cache;
}
}


* This source code was highlighted with Source Code Highlighter.


Now to initialize the cache by using config of the application:

# cache
resources.cache.frontend.adapter = core
resources.cache.frontend.params.lifetime = 7200
resources.cache.frontend.params.automatic_serialization = true
resources.cache.backend.adapter = file
resources.cache.backend.params.lifetime = 7200
resources.cache.backend.params.cache_dir = APPLICATION_PATH "/../trash/cache"
resources.cache.isDefaultMetadataCache = true
resources.cache.isDefaultTranslateCache = true
resources.cache.isDefaultLocaleCache = true
resources.cache.registry_key = cache

So it is necessary to manually add the following line to work custom resources:

# custom resources
pluginPaths.App_Application_Resource_ = App/Application/Resource

Good day, hope I managed to interest you, if you've read up to here=)
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

The use of Lisp in production

New web-interface for statistics and listen to the calls for IP PBX Asterisk

As we did a free Noodle for iOS and how we plan to earn