3 The `ObjectCache` pattern is an extension to the `CallbackCache` pattern. It has
4 the same methods, but instead caches output from any instance method calls or
11 use Zend\Cache\PatternFactory;
13 $object = new stdClass();
14 $objectCache = PatternFactory::factory('object', [
20 ## Configuration Options
22 Option | Data Type | Default Value | Description
23 ------ | --------- | ------------- | -----------
24 `storage` | `string | array | Zend\Cache\Storage\StorageInterface` | none | Adapter used for reading and writing cached data.
25 `object` | `object` | none | The object for which to cache method calls.
26 `object_key` | `null | string` | Class name of object | Hopefully unique!
27 `cache_output` | `boolean` | `true` | Whether or not to cache method output.
28 `cache_by_default` | `boolean` | `true` | Cache all method calls by default.
29 `object_cache_methods` | `array` | `[]` | List of methods to cache (if `cache_by_default` is disabled).
30 `object_non_cache_methods` | `array` | `[]` | List of methods to blacklist (if `cache_by_default` is enabled).
31 `object_cache_magic_properties` | `boolean` | `false` | Whether or not to cache properties exposed by method overloading.
35 In addition to the methods defined in `PatternInterface`, this implementation
36 defines the following methods.
39 namespace Zend\Cache\Pattern;
41 use Zend\Cache\Exception;
43 class ObjectCache extends CallbackCache
46 * Call and cache a class method
48 * @param string $method Method name to call
49 * @param array $args Method arguments
51 * @throws Exception\RuntimeException
54 public function call($method, array $args = []);
57 * Method overloading: proxies to call().
59 * @param string $method Method name to call
60 * @param array $args Method arguments
62 * @throws Exception\RuntimeException
65 public function __call($method, array $args);
68 * Generate a unique key in base of a key representing the callback part
69 * and a key representing the arguments part.
71 * @param string $method The method
72 * @param array $args Callback arguments
74 * @throws Exception\RuntimeException
76 public function generateKey($method, array $args = []);
79 * Property overloading: write data to a named property.
82 * Magic properties will be cached too if the option cacheMagicProperties
83 * is enabled and the property doesn't exist in real. If so it calls __set
84 * and removes cached data of previous __get and __isset calls.
89 * @see http://php.net/manual/language.oop5.overloading.php#language.oop5.overloading.members
91 public function __set($name, $value);
94 * Property overloading: read data from a named property.
97 * Magic properties will be cached too if the option cacheMagicProperties
98 * is enabled and the property doesn't exist in real. If so it calls __get.
100 * @param string $name
102 * @see http://php.net/manual/language.oop5.overloading.php#language.oop5.overloading.members
104 public function __get($name);
107 * Property overloading: check if a named property exists.
110 * Magic properties will be cached too if the option cacheMagicProperties
111 * is enabled and the property doesn't exist in real. If so it calls __get.
113 * @param string $name
115 * @see http://php.net/manual/language.oop5.overloading.php#language.oop5.overloading.members
117 public function __isset($name);
120 * Property overloading: unset a named property.
123 * Magic properties will be cached too if the option cacheMagicProperties
124 * is enabled and the property doesn't exist in real. If so it removes
125 * previous cached __isset and __get calls.
127 * @param string $name
129 * @see http://php.net/manual/language.oop5.overloading.php#language.oop5.overloading.members
131 public function __unset($name);
134 * Handle casting to string
137 * @see http://php.net/manual/language.oop5.magic.php#language.oop5.magic.tostring
139 public function __toString();
142 * Intercept and cache invokable usage.
145 * @see http://php.net/manual/language.oop5.magic.php#language.oop5.magic.invoke
147 public function __invoke();
156 $filter = new Zend\Filter\RealPath();
157 $cachedFilter = Zend\Cache\PatternFactory::factory('object', [
159 'object_key' => 'RealpathFilter',
162 // The realpath filter doesn't output anything
163 // so the output don't need to be caught and cached
164 'cache_output' => false,
167 $path = $cachedFilter->call("filter", ['/www/var/path/../../mypath']);
170 $path = $cachedFilter->filter('/www/var/path/../../mypath');