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.
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,
)));
|
Configuration Options¶
- cache_by_default
Flag indicating whether or not to cache by default. Used by the
ClassCache
andObjectCache
patterns.setCacheByDefault(bool $cacheByDefault)
Implements a fluent interface.getCacheByDefault()
Returns boolean.
- cache_output
Used by the
CallbackCache
,ClassCache
, andObjectCache
patterns. Flag used to determine whether or not to cache output.setCacheOutput(bool $cacheOutput)
Implements a fluent interface.getCacheOutput()
Returns boolean
- class
Set the name of the class to cache. Used by the
ClassCache
pattern.setclass(string $class)
Implements a fluent interface.getClass()
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()
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()
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()
Returns int
- dir_umask
Set the directory umask value. Used by the
CaptureCache
pattern.setDirUmask(int $dirUmask)
Implements a fluent interface.getDirUmask()
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()
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()
Returns int
- file_umask
Set file umask; used by the
CaptureCache
pattern.setFileUmask(int $fileUmask)
Implements a fluent interface.getFileUmask()
Returns int
- index_filename
Set value for index filename. Used by the
CaptureCache
pattern.setIndexFilename(string $indexFilename)
Implements a fluent interface.getIndexFilename()
Returns string
- object
Set object to cache; used by the
ObjectCache
pattern.setObject(object $object)
Implements a fluent interface.getObject()
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()
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()
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()
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()
Returns array
- public_dir
Set location of public directory; used by the
CaptureCache
pattern.setPublicDir()
Implements a fluent interface.getPublicDir()
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()
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()
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()
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()
Returns null|Zend\Cache\Storage\Adapter
Available Methods¶
- setOptions
setOptions(Zend\Cache\Pattern\PatternOptions $options)
Set pattern options
Returns Zend\Cache\Pattern
- getOptions
getOptions()
Get all pattern options
Returns
PatternOptions
instance.
Examples¶
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
|