Source

sfCacheBackportPlugin /

Filename Size Date modified Message
lib
test/unit
188 B
1.0 KB
3.1 KB
2.0 KB
5.4 KB
# 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;
      }
    }