Pluf : PHP 5 Framework   Download | Bug Tracking | Source | Documentation | Forum | Latest Changes | Home

Cache Framework

Pluf is fast, but sometimes, you need to interact wiht other servers or run complex operations. To avoid delay in rendering your pages, you should cache the results of the operation to avoid regenerating the values at each page display. The cache framework is doing this for you.

Simple Usage

This is a typical usage:

$cache = Pluf_Cache::factory();
if (null === ($foo=$cache->get('my-key'))) {
    $foo = run_complex_operation();
    $cache->set('my-key', $foo);
}
return $foo;

Configuration

In your configuration file, you define the cache engine and its parameters together with the default timeout of the cache in seconds.

$cfg['cache_engine'] = 'Pluf_Cache_File';
$cfg['cache_timeout'] = 300;

Pluf_Cache_File engine

The only configuration variable that is mandatory is:

$cfg['cache_file_folder'] = '/path/to/cache';

Pluf_Cache_Memcached engine

From the memcached website, memcached is:

a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

To install it on a debian/ubuntu based system simply do:

sudo apt-get install memcached php5-memcache
sudo pico /etc/php5/conf.d/memcache.ini

When editing the memcache.ini file, replace:

; extension=memcache.so

with:

extension=memcache.so

Then restart Apache or your fastcgi process.

The configuration keys are:

  • cache_memcached_keyprefix (''): A prefix added to the keys to use the same server for different applications and avoid key clashes.
  • cache_memcached_server ('localhost'): The memcached server you want to use.
  • cache_memcached_port (11211): The port to connect to the memcached server.
  • cache_memcached_compress (0): If you want to use compression, set the value to the constant MEMCACHE_COMPRESSED.

Pluf_Cache_Apc engine

APC is the Alternative PHP Cache. It is both an opcode cache and an in memory variable store. You need to have it installed and configured to use it.

The configuration keys are:

  • cache_apc_keyprefix (''): A prefix added to the keys to use the same server for different applications and avoid key clashes.
  • cache_apc_compress (false): If you want to use compression, set the value to true.

The compression is done a the php level using the gzinflate/gzdeflate functions.

Detailed Usage

To get a variable from the cache you need to create a cache object and get the variable with the get method.

$cache = Pluf_Cache::factory();
$foo = $cache->get('my-key', null);

The get methods has for second parameter the default value to return if the key is not found in the cache. The default value is null.

To set a variable in the cache, you of course use the set method:

$cache = Pluf_Cache::factory();
$cache->set('my-key', $foo, 300);

The third parameter is the timeout for the key in seconds. The timeout is optional and if not defined, the cache_timeout configuration variable is used.

Note: An object can be stored in cache only if it can be serialized and unserialized using the (un)serialize functions of PHP.

Report issues in the documentation

If you find errors or issues in the documentation, report a bug. We will update the documentation accordingly.

Pluf © 2005-2008 Loïc d'Anterroches, supported by Céondo Ltd.