ATK and Caching

From Achievo/ATK Wiki

Jump to: navigation, search

ATK Howto: ATK and Caching

Complexity: Advanced
Author: Sandy Pleyte <sandy at achievo dot org>

List of other Howto's

Source by Sandy Pleyte: http://www.ibuildings.com/blog/archives/821-ATK-and-Caching.html

In previous posts by my colleagues Martin and Lineke you could already read that caching is becoming important when you are going to build scalable web applications. For many projects we use ATK as our framework, but it didn't have a caching API. So in the core of our products we only use file caching because that's available on all systems. If you wanted to use an external cache, for example Zend Platform, this was mostly done in a custom module. This made sure we didn't get large if-constructions in the core products to check for availability of an external cache.

That was, until yesterday... because now there is a cache object (atkCache) available in the ATK SVN trunk. The new atkCache class is a factory class, which builds and returns instances of atkCache which has all the methods to communicate with an external cache to handle the actual storage and retrieval. At this moment atkCache has support for the following caching backends:


  • APC (Alternative PHP Cache)
  • eAccelerator
  • Zend Platform (3 versions, output cache, disk cache and shared memory cache, the last two can only be used with version 3.6 and higher of Zend Platform).
  • Memcache to store data in RAM on the localhost or on another server
  • Xcache
  • File (which stores data in the file system)
  • Var (a variable-based cache that lasts only for the duration of a single request)

Although the configuration options for the cache methods are slightly different, the API for each of them is identical (because the API is abstracted via the atkCache class).

Creating an instance

You can get an instance of the atkCache object in 2 ways. By specifying the desired methods in $config_cache_method and letting atkCache look up which cache is supported on your system or by giving the name of the cache you want to use to the getInstance method. Example:

<?php
 
// This will give a file cache object
$config_cache_method = 'file';
$filecache = atkCache::getInstance();
 
// In the next example we let atkCache look up which cache is supported by your system.
// if Zend Platform is available, it will use that, else memcache and file based caching
// as a last resort.
$config_cache_method = array('zp_shm','memcache','file');
$cache = atkCache::getInstance();
 
// Or ask directory for a file cache object
$filecache = atkCache::getInstance('file');
 
?>

If you ask for a cache object with an unsupported method it will return a var cache by default.

Configuration

By default the lifetime for a cached value will be 1 hour for every method. But this can be configured for every cache method separately in a configuration value. For example, you can do this in your config.inc.php

<?php
 
  // configurations for every cache method
  $config_cache['file']['lifetime']=3600;
  $config_cache['file']['path']=atkconfig('atktempdir').'cache/';
  $config_cache['file']['context']=array();
  $config_cache['memcache']['lifetime']=3600;
  $config_cache['memcache']['host']='localhost';
  $config_cache['memcache']['port']=11211;
  $config_cache['memcache']['timeout']=1;
  
  // set the default cache method
  $config_cache_method = array('zp_shm','file');
  // if we are using it on a shared hosting, set a namespace  
  $config_cache_namespace = 'default';
?>

For a complete overview of which configuration options you can use, you can look at the wiki page.

Usage

Once you have a cache object, you can use it to store and retrieve data you want to use. The most important methods you can use are:

add: adds data to the cache if it doesn't already exists set: change an existing cache entry to a new value get: for retrieving data out of the cache delete: for deleting cache data

In the following example, we will use the cache for caching the available ATK themes. If the cache entry doesn't exists we will retrieve the data by searching for it on the file system and save it in the cache; if it does exist, we will use that instead.


<?php
 
include 'atk.inc';
 
// Get a cache instance
$cache = atkCache::getInstance();
 
// Check if the available themes are in the cache. If the
// entry is past it's lifetime it will be automatically
if(!$cache->get('available_themes'))
{
    // No themes stored in the cache under the id 'available_themes'
    // now use the directory traverser to search for themes
   $themes = array('steelblue');
   
   // Save the found themes in the cache for 1 day instead of the
  // default lifetime of 1 hour
  $cache->set('available_themes',$themes, 86400);
}
 
?>

atkCache also implements the PHP5 arrayAccess interface, so it's also possible to retrieve / save values like:

<?php
 
$cache = atkCache::getInstance();
 
// Retrieve the available themes from the cache
$themes = $cache['available_themes'];
// Save themes into the cache
$cache['available_themes']=array('steelblue');
// Remove value from cache
unset($cache['available_themes']);
 
?>

Custom cache

With the atkCache it's also possible to create and use your own cache. Just create a class based on atkCache and use it like this:

<?php
$config['modules.news.cache.newscache']['lifetime'] = 300;
atkCache::getInstance('modules.news.cache.newscache');
?>

Summary

As you can see it's very easy to use the atkCache to make your applications faster. One of the applications that already uses the atkCache is Achievo and on certain pages this gives a performance boost of more than 50 percent!

Personal tools
Navigation