Zend Framework Source Code Snippets

Bootstrap Cache Resource

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
<?php
 
class App_Application_Resource_Cache extends Zend_Application_Resource_ResourceAbstract
{
  /**   * Default registry key
   */
  const DEFAULT_REGISTRY_KEY = 'App_Cache';
 
  /**   * Cache instance
   *
   * @var Zend_Cache
   */
  protected $_cache = null; 
  /**
   * Inititalize cache resource
   *
   * @return Zend_Cache   */
  public function init ()
  {
    return $this->getCache();
  } 
  /**
   * Return 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;
  }}
1
2
3
4
56
7
8
9
1011
12
13
14
# custom resources
pluginPaths.App_Application_Resource_ = App/Application/Resource
 
# cache
resources.cache.frontend.adapter = coreresources.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

Comments

MiB MiB
7 months ago

Great snippet! Especially the part with conditional cache for various cache-aware modules.

It would be great to add possibility to define more than one cache instance.

Regards

Michał

alex alex
7 months ago

Awesome snippet, thank you sad!

Armand Armand
7 months ago

Not sure if this is an issue for everyone, but the line:
/// use as default database metadata cache
if (isset($options['isDefaultMetadataCache']) && true === (bool) $options['isDefaultMetadataCache']) {
Singular_Db_Table_Abstract::setDefaultMetadataCache($this->_cache);
}
is issuing an error.

!!

sad sad
7 months ago

Singular_Db_Table_Abstract::setDefaultMetadataCache($this->_cache);

fixed to

Zend_Db_Table_Abstract::setDefaultMetadataCache($this->_cache);

thx!

Mattias Mattias
6 months ago

Hi,

thanks for the nice snippet !
I had to change these two lines to get it work:

const DEFAULT_REGISTRY_KEY = 'App_Cache';
to
const DEFAULT_REGISTRY_KEY = 'cache';

and

resources.cache.registry_key = cache
to
resources.cache.registry_key = App_Cache

Mattias Mattias
6 months ago

ah,

registry gives the possibility to create caches with different keys, etc....,

now i got, sry!

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Custom bootstrap resource for cache.

Snippet details

Created:
sad sad
7 months ago
Edited:
sad sad
7 months ago
Revision Id:
138
Edit Message:
Initial Release
ZF Version
1.8.3
Tags:
bootstrap cache resource
Comments:
6
Views:
774
Points:
1 (1 votes)

History

r138

Initial Release

sad sad
7 months ago
diff
r132

Initial Release

sad sad
7 months ago