laminas/laminas-cache
Laminas Cache provides flexible caching for PHP applications, with adapters for common backends, cache storage, patterns, plugins, and PSR-compatible integrations. Includes tools for configuring, managing, and testing cache behavior in Laminas apps.
The ObjectCache pattern is an extension to the CallbackCache pattern. It has
the same methods, but instead caches output from any instance method calls or
public properties.
use Laminas\Cache\Pattern\ObjectCache;
use Laminas\Cache\Pattern\PatternOptions;
use stdClass;
$object = new stdClass();
$objectCache = new ObjectCache(
$storage,
new PatternOptions([
'object' => $object,
])
);
Storage Adapter
The
$storageadapter can be any adapter which implements theStorageInterface. Check out the Pattern Quick Start-Section for a standard adapter which can be used here.
| Option | Data Type | Default Value | Description |
|---|---|---|---|
storage |
string|array|Laminas\Cache\Storage\StorageInterface |
none | deprecated Adapter used for reading and writing cached data. |
object |
object |
none | The object for which to cache method calls. |
object_key |
null|string |
Class name of object | Hopefully unique! |
cache_output |
bool |
true |
Whether or not to cache method output. |
cache_by_default |
bool |
true |
Cache all method calls by default. |
object_cache_methods |
array |
[] |
List of methods to cache (if cache_by_default is disabled). |
object_non_cache_methods |
array |
[] |
List of methods to blacklist (if cache_by_default is enabled). |
object_cache_magic_properties |
bool |
false |
Whether or not to cache properties exposed by method overloading. |
use Laminas\Cache\Pattern\ObjectCache;
use Laminas\Cache\Pattern\PatternOptions;
$filter = new \Laminas\Filter\RealPath();
$cachedFilter = new ObjectCache(
$storage,
new PatternOptions([
'object' => $filter,
'object_key' => 'RealpathFilter',
// The realpath filter doesn't output anything
// so the output don't need to be caught and cached
'cache_output' => false,
])
);
$path = $cachedFilter->call("filter", ['/www/var/path/../../mypath']);
// OR
$path = $cachedFilter->filter('/www/var/path/../../mypath');
In addition to the methods defined in PatternInterface and the StorageCapableInterface, this implementation
defines the following methods.
namespace Laminas\Cache\Pattern;
use Laminas\Cache\Exception;
class ObjectCache extends CallbackCache
{
/**
* Call and cache a class method
*
* [@param](https://github.com/param) non-empty-string $method Method name to call
* [@param](https://github.com/param) array $args Method arguments
* [@return](https://github.com/return) mixed
* [@throws](https://github.com/throws) Exception\RuntimeException
* [@throws](https://github.com/throws) \Exception
*/
public function call(string $method, array $args = []): mixed;
/**
* Method overloading: proxies to call().
*
* [@param](https://github.com/param) non-empty-string $method Method name to call
* [@param](https://github.com/param) array $args Method arguments
* [@return](https://github.com/return) mixed
* [@throws](https://github.com/throws) Exception\RuntimeException
* [@throws](https://github.com/throws) \Exception
*/
public function __call(string $method, array $args): mixed;
/**
* Generate a unique key in base of a key representing the callback part
* and a key representing the arguments part.
*
* [@param](https://github.com/param) non-empty-string $method The method
* [@param](https://github.com/param) array $args Callback arguments
* [@return](https://github.com/return) non-empty-string
* [@throws](https://github.com/throws) Exception\RuntimeException
*/
public function generateKey(string $methodOrProperty, array $args = []): string;
/**
* Property overloading: write data to a named property.
*
* NOTE:
* Magic properties will be cached too if the option cacheMagicProperties
* is enabled and the property doesn't exist in real. If so it calls __set
* and removes cached data of previous __get and __isset calls.
*
* [@param](https://github.com/param) non-empty-string $name
* [@see](https://github.com/see) http://php.net/manual/language.oop5.overloading.php#language.oop5.overloading.members
*/
public function __set(string $name, mixed $value): void;
/**
* Property overloading: read data from a named property.
*
* NOTE:
* Magic properties will be cached too if the option cacheMagicProperties
* is enabled and the property doesn't exist in real. If so it calls __get.
*
* [@param](https://github.com/param) non-empty-string $name
* [@see](https://github.com/see) http://php.net/manual/language.oop5.overloading.php#language.oop5.overloading.members
*/
public function __get($name): mixed;
/**
* Property overloading: check if a named property exists.
*
* NOTE:
* Magic properties will be cached too if the option cacheMagicProperties
* is enabled and the property doesn't exist in real. If so it calls __get.
*
* [@param](https://github.com/param) non-empty-string $name
* [@see](https://github.com/see) http://php.net/manual/language.oop5.overloading.php#language.oop5.overloading.members
*/
public function __isset($name): bool;
/**
* Property overloading: unset a named property.
*
* NOTE:
* Magic properties will be cached too if the option cacheMagicProperties
* is enabled and the property doesn't exist in real. If so it removes
* previous cached __isset and __get calls.
*
* [@param](https://github.com/param) non-empty-string $name
* [@see](https://github.com/see) http://php.net/manual/language.oop5.overloading.php#language.oop5.overloading.members
*/
public function __unset(string $name): void;
/**
* Handle casting to string
*
* [@see](https://github.com/see) http://php.net/manual/language.oop5.magic.php#language.oop5.magic.tostring
*/
public function __toString(): string;
/**
* Intercept and cache invokable usage.
*
* [@see](https://github.com/see) http://php.net/manual/language.oop5.magic.php#language.oop5.magic.invoke
*/
public function __invoke(): mixed;
}
How can I help you explore Laravel packages today?