Previous Next

Zend\Cache\Pattern

Overview

Cache patterns are configurable objects to solve known performance bottlenecks. Each should be used only in the specific situations they are designed to address. For example you can use one of the CallbackCache, ObjectCache or ClassCache patterns to cache method and function calls; to cache output generation, the OutputCache pattern could assist.

All cache patterns implements the same interface, Zend\Cache\Pattern, and most extend the abstract class Zend\Cache\Pattern\AbstractPattern to implement basic logic.

Configuration is provided via the Zend\Cache\Pattern\PatternOptions class, which can simply be instantiated with an associative array of options passed to the constructor. To configure a pattern object, you can set an instance of Zend\Cache\Pattern\PatternOptions with setOptions, or provide your options (either as an associative array or PatternOptions instance) as the second argument to the factory.

It's also possible to use a single instance of Zend\Cache\Pattern\PatternOptions and pass it to multiple pattern objects.

Quick Start

Pattern objects can either be created from the provided Zend\Cache\PatternFactory factory, or, by simply instantiating one of the Zend\Cache\Pattern\* classes.

use Zend\Cache\PatternFactory,
    Zend\Cache\Pattern\PatternOptions;

// Via the factory:
$callbackCache = PatternFactory::factory('callback', array(
    'storage'      => 'apc',
    'cache_output' => true,
));

// OR, the equivalent manual instantiation:
$callbackCache = new \Zend\Cache\Pattern\CallbackCache();
$callbackCache->setOptions(new PatternOptions(array(
    'storage'      => 'apc',
    'cache_output' => true,
)));

Configuration Options

cache_by_default

Flag indicating whether or not to cache by default. Used by the ClassCache and ObjectCache patterns.

  • setCacheByDefault ( bool $cacheByDefault )

    Implements a fluent interface.

  • getCacheByDefault ( void )

    Returns boolean.

cache_output

Used by the CallbackCache, ClassCache, and ObjectCache patterns. Flag used to determine whether or not to cache output.

  • setCacheOutput ( bool $cacheOutput )

    Implements a fluent interface.

  • getCacheOutput ( void )

    Returns boolean

class

Set the name of the class to cache. Used by the ClassCache pattern.

  • setclass ( string $class )

    Implements a fluent interface.

  • getClass ( void )

    Returns null|string

class_cache_methods

Set list of method return values to cache. Used by ClassCache Pattern.

  • setClassCacheMethods ( array $classCacheMethods )

    Implements a fluent interface.

  • getClassCacheMethods ( void )

    Returns array

class_non_cache_methods

Set list of method return values that should not be cached. Used by the ClassCache pattern.

  • setClassNonCacheMethods ( array $classNonCacheMethods )

    Implements a fluent interface.

  • getClassNonCacheMethods ( void )

    Returns array

dir_perm

Set directory permissions; proxies to "dir_umask" property, setting the inverse of the provided value. Used by the CaptureCache pattern.

  • setDirPerm ( string|int $dirPerm )

    Implements a fluent interface.

  • getDirPerm ( void )

    Returns int

dir_umask

Set the directory umask value. Used by the CaptureCache pattern.

  • setDirUmask ( int $dirUmask )

    Implements a fluent interface.

  • getDirUmask ( void )

    Returns int

file_locking

Set whether or not file locking should be used. Used by the CaptureCache pattern.

  • setFileLocking ( bool $fileLocking )

    Implements a fluent interface.

  • getFileLocking ( void )

    Returns bool

file_perm

Set file permissions; proxies to the "file_umask" property, setting the inverse of the value provided. Used by the CaptureCache pattern.

  • setFilePerm ( int|string $filePerm )

    Implements a fluent interface.

  • getFilePerm ( void )

    Returns int

file_umask

Set file umask; used by the CaptureCache pattern.

  • setFileUmask ( int $fileUmask )

    Implements a fluent interface.

  • getFileUmask ( void )

    Returns int

index_filename

Set value for index filename. Used by the CaptureCache pattern.

  • setIndexFilename ( string $indexFilename )

    Implements a fluent interface.

  • getIndexFilename ( void )

    Returns string

object

Set object to cache; used by the ObjectCache pattern.

  • setObject ( object $object )

    Implements a fluent interface.

  • getObject ( void )

    Returns null|object.

object_cache_magic_properties

Set flag indicating whether or not to cache magic properties. Used by the ObjectCache pattern.

  • setObjectCacheMagicProperties ( bool $objectCacheMagicProperties )

    Implements a fluent interface.

  • getObjectCacheMagicProperties ( void )

    Returns bool

object_cache_methods

Set list of object methods for which to cache return values. Used by ObjectCache pattern.

  • setObjectCacheMethods ( array $objectCacheMethods )

    Implements a fluent interface.

  • getObjectCacheMethods ( void )

    Returns array

object_key

Set the object key part; used to generate a callback key in order to speed up key generation. Used by the ObjectCache pattern.

  • setObjectKey ( null|string $objectKey )

    Implements a fluent interface.

  • getObjectKey ( void )

    Returns null|string

object_non_cache_methods

Set list of object methods for which not to cache return values. Used by the ObjectCache pattern.

  • setObjectNonCacheMethods ( array $objectNonCacheMethods )

    Implements a fluent interface.

  • getObjectNonCacheMethods ( void )

    Returns array

public_dir

Set location of public directory; used by the CaptureCache pattern.

  • setPublicDir ( void )

    Implements a fluent interface.

  • getPublicDir ( void )

    Returns null|string

storage

Set the storage adapter. Required for the following Pattern classes: CallbackCache, ClassCache, ObjectCache, OutputCache.

  • setStorage ( string|array|Zend\Cache\Storage\Adapter $storage )

    Implements a fluent interface.

  • getStorage ( void )

    Returns null|Zend\Cache\Storage\Adapter

tag_key

Set the prefix used for tag keys. Used by the CaptureCache pattern.

  • setTagKey ( string $tagKey )

    Implements a fluent interface.

  • getTagKey ( void )

    Returns string

tags

Set list of tags to use for captured content. Used by the CaptureCache pattern.

  • setTags ( array $tags )

    Implements a fluent interface.

  • getTags ( void )

    Returns array

Set storage adapter to use for tags. Used by the CaptureCache pattern.

  • setTagStorage ( string|array|Zend\Cache\Storage\Adapter $tagStorage )

    Implements a fluent interface.

  • getTagStorage ( void )

    Returns null|Zend\Cache\Storage\Adapter

Available Methods

setOptions
setOptions ( Zend\Cache\Pattern\PatternOptions $options )

Set pattern options

Returns Zend\Cache\Pattern

getOptions
getOptions ( void )

Get all pattern options

Returns PatternOptions instance.

Examples

Example #1 Using the callback cache pattern

use Zend\Cache\PatternFactory;

$callbackCache = PatternFactory::factory('callback', array(
    'storage' => 'apc'
));

// Calls and caches the function doResourceIntensiceStuff with three arguments
// and returns result
$result = $callbackCache->call('doResourceIntensiveStuff', array(
    'argument1',
    'argument2',
    'argumentN',
));

Example #2 Using the object cache pattern

use Zend\Cache\PatternFactory;

$object      = new MyObject();
$objectProxy = PatternFactory::factory('object', array(
    'object'  => $object,
    'storage' => 'apc',
));

// Calls and caches $object->doResourceIntensiveStuff with three arguments
// and returns result
$result = $objectProxy->doResourceIntensiveStuff('argument1', 'argument2', 'argumentN');

Example #3 Using the class cache pattern

use Zend\Cache\PatternFactory;

$classProxy = PatternFactory::factory('class', array(
    'class'   => 'MyClass',
    'storage' => 'apc',
));

// Calls and caches MyClass::doResourceIntensiveStuff with three arguments
// and returns result
$result = $classProxy->doResourceIntensiveStuff('argument1', 'argument2', 'argumentN');

Example #4 Using the output cache pattern

use Zend\Cache\PatternFactory;

$outputCache = PatternFactory::factory('output', array(
    'storage' => 'filesystem',
));

// Start capturing all output (excluding headers) and write it to storage.
// If there is already a cached item with the same key it will be
// output and return true, else false.
if ($outputCache->start('MyUniqueKey') === false) {
    echo 'cache output since: ' . date('H:i:s') . "
\n"; // end capturing output, write content to cache storage and display // captured content $outputCache->end(); } echo 'This output is never cached.';

Example #5 Using the capture cache pattern

You need to configure your HTTP server to redirect missing content to run your script generating it.

This example uses Apache with the following .htaccess:

ErrorDocument 404 /index.php

Within your index.php you can add the following content:

use Zend\Cache\PatternFactory;

$capture = PatternFactory::factory('capture', array(
    'public_dir' => __DIR__,
));

// Start capturing all output excl. headers. and write to public directory
// If the request was already written the file will be overwritten.
$capture->start();

// do stuff to dynamically generate output
Previous Next