Home > Tips & tricks > Programming > PHP: Using Zend_Cache with Zend_config
PHP: Using Zend_Cache with Zend_config PDF Print E-mail
Written by Administrator   
Saturday, 23 August 2008 10:52
The zend framework provides a class to handle configuration called Zend_Config. It also provides a class to manage cache: Zend_Cache. But unless the Zend_Translate component, Zend_Config does not natively support Zend_Cache. This article explains a solution.

1 - Classical usage of Zend_config:

//[...]
$config = new Zend_Config_Ini('path/to/config.ini');
//[...]

limits:
For big config files, this is a waste of time as the configuration file has to be parsed for each user's query and application config files seldom changes. Adding a cache mecanism is a good way of optimizing this, but unlike Zend_Translate, Zend_Config does not provides a native support for Zend_Cache.

2 - Using Zend_Config with Zend_Cache transparently

2.1 problems / expectations

  • caching the config file needs configuration parameters such as lifetime and cache path that cannot be put in configuration file itself => in this example, we use the session_save_path() as storage, which should be writable by php in most cases
  • using Zend_Cache should be transparent to avoid bootstrap pollution => in this example we use a special configuration class on which only 1 static method call is needed to get a cached config object. Thus, using it does not add more code than typical Zend_config use as seen above

2.2 The proposed new way

Using this example's class will make the code from chapter 1 to become the following:

//[...]
$config = MyApp_Config::getInstance();
//[...]

 

2.3 The Example class

2.3.1 Source code

  1. require_once 'Zend/Config/Ini';
  2. require_once 'Zend/Cache';
  3.  
  4. /**
  5.  * This class is used to retreive the application's configuration throught a static method. This
  6.  * way, it is easy to use Zend_Cache to cache the whole configuration which seldom contains changes
  7.  * @package MyApp
  8.  */
  9. class MyApp_Config
  10. {
  11. /**
  12.   * full path to the configuration file
  13.   */
  14. const CONFIG_FILE = 'settings/application.ini';
  15.  
  16. /**
  17.   * Cached instance of the configuration
  18.   * @var Zend_Cache
  19.   */
  20. private static $_cache = null;
  21.  
  22.  
  23. /**
  24.   * get the application's configuration
  25.   * @return Zend_Config
  26.   */
  27. public static function getConfig()
  28. {
  29. return new Zend_Config_Ini(self::CONFIG_FILE,null);
  30. }
  31.  
  32.  
  33. /**
  34.   * get the path for configuration cache (session's save path)
  35.   * @return string
  36.   */
  37. public static function getConfigCachePath()
  38. {
  39. }
  40.  
  41.  
  42. /**
  43.   * get an instance of the class (in fact a Zend_Config instance)
  44.   * @return Zend_Config a cached instance of Zend_Config
  45.   */
  46. public static function getInstance()
  47. {
  48. // if never initialized, get a Zend_Cache instance
  49. if (!self::$cache) {
  50. self::$cache = Zend_Cache::factory('class',
  51. 'file',
  52. array('cached_entity' => 'MyApp_Config'),
  53. array('cache_dir' => MyApp_Config::getConfigCachePath()));
  54. }
  55.  
  56. return self::$cache->getConfig();
  57. }
  58.  
  59.  
  60. /**
  61.   * clean the cache. A call to getInstance() must have been called once to make this work.
  62.   */
  63. public static function clearCache()
  64. {
  65. if (self::$cache) {
  66. self::$cache->clean();
  67. }
  68. }
  69. }

2.3.2 Remarks

  • This example only works with file based caching (but it should not be difficult to adapt it to use others frontend)
  • As we are caching the application's configuration file itself, it is not really possible to store cache path in a configuration file (unless you would like the cached config system to use another config file itself... hmmm)
  • This example is suitable for relatively biug config files. If you use little ones, you should do some benchmark to see if it provides any enhancement. I tested this code with a small config file (a dozen of entries) and the footprint was not perceptible using a profiler. So in all cases, this code should not add any overhead.

2.4 Cleanning up the cache

In case your config file changed and you don't want to or can't wait for the cache to expires, you can call the clearCache() method in a admin page or a special controller with some access restriction.

3 Conclusion

This was just a little example I wrote with my humble knowledges. It might be a starting point for more complexe configuration file architecture or for other caching mecanism (database, memcached, ...)

Feel free to send me advices or remarks ;)

Last Updated on Saturday, 23 August 2008 12:34
 
Comments (1)
1 Saturday, 14 January 2012 04:03
Berger32Eunice
I strictly recommend not to hold back until you get enough money to order all you need! You can just take the mortgage loans or just secured loan and feel yourself fine

Add your comment

Your name:
Your email:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification:
 
Original design by i-cons.ch / namibia-forum.ch