sfCacheBackportPlugin - 1.1.2

Symfony 1.1+ cache system for Symfony 1.0 with an adapter, object caching, fail-safe cache spawning and debugbar/logging support.

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

sfCacheBackportPlugin

This plugin has currently many different usages:

  • Provides Symfony 1.1+ cache classes for Symfony 1.0 (with an adapter for view caching).
  • Also provides a transparent and extensible API to cache objects with the cache classes, for any version of Symfony.
  • A convenient way to get cache classes depending on your environment.

Using the classes

It's the same as using the classes from Symfony 1.1+ except that you add Backport at the end of the class name. One nice addition is the sfNoCacheBackport class; especially if you use it with sfSpawnCache.

Using sfSpawnCache

It's fairly common to have many different environments, with different caches available. You also might want to avoid to render your production website unusable if, say, your Memcache server goes down. Enter sfSpawnCache!

Usage: sfSpawnCache::get($classes, $options, $reuse)

Example: sfSpawnCache::get(array('sfPenguinCache', 'sfXCacheCache', 'sfNoCache'), array('penguins'=>'plenty'))

For more information, read the associated PHPDoc.

Caching views in Symfony 1.0 with the backported classes

Example config (factories.yml):

view_cache:
  class: sfCacheBackportAdapter
  param:
    classes: [ sfXCacheCacheBackport, sfFileCacheBackport, sfNoCacheBackport]
    options:
      automatic_cleaning_factor: 0
      cache_dir:  %SF_TEMPLATE_CACHE_DIR%
      prefix: myapp_viewcache

This will try to use sfXCacheCacheBackport, then sfFileCacheBackport if it fails, then sfNoCacheBackport. The cache should be enabled in your settings.yml by the way.

Object Caching

As an added bonus the classes ship with functions to store and retrieve objects from the cache. The point is to be able to store objects without worrying about the cache capabilities (for instance Memcache handles serialization for you, the others don't).

They behave exactly like set() et get() are respectively are setObject() and getObject().

I want to use the object caching with Symfony 1.1+

Create a sfCacheBackport.class.php in your lib/ folder with the following content:

class sfCacheBackport extends sfCache {}

And done.

What is this callable thing I saw in the code? Is it working?

Before caching

Example:

sfMixer::register('MyClass:cache:set', array('MyClass', 'cacheSet'));

The function can alter the object, and if we return an object, the cache will use it instead (only useful if you clone it).

public static function cacheSet(BaseObject &$node)
{
  // Cache the penguins
  $node->getPenguin();

  // This will only afect the cached object, not the current instance
  $object = clone $node;
  $object->setCulture(false);

  return $object;
}

After caching

Example:

sfMixer::register('MyClass:cache:get', array('MyClass', 'cacheGet'));

The function can alter the object, and if we return false, the call will act like it was a miss.

public static function cacheGet(BaseObject &$node)
{
  // Waking up
  $node->drinkCoffee();

  // Object outdated?
  if ($node->computeSomething() > 42)
  {
    return false;
  }
}

The debug features

If both sfConfig options sf_debug and sf_logging_enabled are enabled, sfSpawnCache will automatically encapsulate the spawned cache object with sfDebugCacheBackport. This class will forward all methods to the real cache object and log the calls. If you want to use it manually, just use:

$debug_cache = new sfDebugCacheBackport($real_cache);

If app_cache_backport_log_to_db is enabled (the default), the cache actions will be logged to the Database tab of the DebugBar. In all cases, the ObjectCache timer will have the total number of calls to and the time spent into setObject() and getObject().