Cache Patterns

ObjectCache

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.

Quick Start

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 $storage adapter can be any adapter which implements the StorageInterface. Check out the Pattern Quick Start-Section for a standard adapter which can be used here.

Configuration Options

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.

Examples

Caching a Filter

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');

Available Methods

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  non-empty-string $method  Method name to call
     * @param  array  $args    Method arguments
     * @return mixed
     * @throws Exception\RuntimeException
     * @throws \Exception
     */
    public function call(string $method, array $args = []): mixed;

    /**
     * Method overloading: proxies to call().
     *
     * @param  non-empty-string $method  Method name to call
     * @param  array  $args    Method arguments
     * @return mixed
     * @throws Exception\RuntimeException
     * @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  non-empty-string     $method  The method
     * @param  array      $args    Callback arguments
     * @return non-empty-string
     * @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  non-empty-string $name
     * @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  non-empty-string $name
     * @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  non-empty-string $name
     * @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  non-empty-string $name
     * @see    http://php.net/manual/language.oop5.overloading.php#language.oop5.overloading.members
     */
    public function __unset(string $name): void;

    /**
     * Handle casting to string
     *
     * @see    http://php.net/manual/language.oop5.magic.php#language.oop5.magic.tostring
     */
    public function __toString(): string;

    /**
     * Intercept and cache invokable usage.
     *
     * @see    http://php.net/manual/language.oop5.magic.php#language.oop5.magic.invoke
     */
    public function __invoke(): mixed;
}