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.
Pattern objects can either be created from the provided Zend\Cache\PatternFactory factory, or, by simply instantiating one of the Zend\Cache\Pattern\* classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | use Zend\Cache\PatternFactory;
use 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,
)));
|
Flag indicating whether or not to cache by default. Used by the ClassCache and ObjectCache patterns.
Used by the CallbackCache, ClassCache, and ObjectCache patterns. Flag used to determine whether or not to cache output.
Set the name of the class to cache. Used by the ClassCache pattern.
Set list of method return values to cache. Used by ClassCache Pattern.
Set list of method return values that should not be cached. Used by the ClassCache pattern.
Set directory permissions; proxies to “dir_umask” property, setting the inverse of the provided value. Used by the CaptureCache pattern.
Set the directory umask value. Used by the CaptureCache pattern.
Set whether or not file locking should be used. Used by the CaptureCache pattern.
Set file permissions; proxies to the “file_umask” property, setting the inverse of the value provided. Used by the CaptureCache pattern.
Set file umask; used by the CaptureCache pattern.
Set value for index filename. Used by the CaptureCache pattern.
Set object to cache; used by the ObjectCache pattern.
Set flag indicating whether or not to cache magic properties. Used by the ObjectCache pattern.
Set list of object methods for which to cache return values. Used by ObjectCache pattern.
Set the object key part; used to generate a callback key in order to speed up key generation. Used by the ObjectCache pattern.
Set list of object methods for which not to cache return values. Used by the ObjectCache pattern.
Set location of public directory; used by the CaptureCache pattern.
Set the storage adapter. Required for the following Pattern classes: CallbackCache, ClassCache, ObjectCache, OutputCache.
Set the prefix used for tag keys. Used by the CaptureCache pattern.
Set list of tags to use for captured content. Used by the CaptureCache pattern.
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() Returns null|Zend\Cache\Storage\Adapter
setOptions(Zend\Cache\Pattern\PatternOptions $options)
Set pattern options
Returns Zend\Cache\Pattern
getOptions()
Get all pattern options
Returns PatternOptions instance.
Using the callback cache pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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',
));
|
Using the object cache pattern
1 2 3 4 5 6 7 8 9 10 11 | 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');
|
Using the class cache pattern
1 2 3 4 5 6 7 8 9 10 | 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');
|
Using the output cache pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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') . "<br />\n";
// end capturing output, write content to cache storage and display
// captured content
$outputCache->end();
}
echo 'This output is never cached.';
|
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:
1 | ErrorDocument 404 /index.php
|
Within your index.php you can add the following content:
1 2 3 4 5 6 7 8 9 10 11 | 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
|
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.