Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laminas Cache Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Adapters

Storage adapters are wrappers for real storage resources such as memory or the filesystem, using the well known adapter pattern.

They come with tons of methods to read, write, and modify stored items, and to get information about stored items and the storage.

All adapters implement Laminas\Cache\Storage\StorageInterface, and most extend Laminas\Cache\Storage\Adapter\AbstractAdapter, which provides a foundation of common logic.

Configuration is handled by either Laminas\Cache\Storage\Adapter\AdapterOptions, or an adapter-specific options class if it exists. You may pass the options instance to the class at instantiation, via the setOptions() method, or, alternately, pass an associative array of options in either place (internally, these are then passed to an options class instance). Alternately, you can pass associative array to the Laminas\Cache\Service\StorageAdapterFactoryInterface::create method.

Quick Start

Caching adapters can either be created from the provided Laminas\Cache\Service\StorageAdapterFactoryInterface, or by instantiating one of the Laminas\Cache\Storage\Adapter\* classes. To make life easier, the Laminas\Cache\Service\StorageAdapterFactoryInterface comes with a create() method to create an adapter and all requested plugins at once.

use Laminas\Cache\Service\StorageAdapterFactoryInterface;
use Laminas\Cache\Service\StoragePluginFactoryInterface;
use Psr\Container\ContainerInterface;

/** [@var](https://github.com/var) ContainerInterface $container */
$container = null; // can be any configured PSR-11 container

/** [@var](https://github.com/var) StorageAdapterFactoryInterface $storageFactory */
$storageFactory = $container->get(StorageAdapterFactoryInterface::class);

// Via factory:
$cache = $storageFactory->create(
    'apcu',
    ['ttl' => 3600],
    [
        [
            'name' => 'exception_handler',
            'options' => [
                'throw_exceptions' => false,
             ], 
        ],
    ]
);

// Via array configuration:
$cache = $storageFactory->createFromArrayConfiguration([
    'adapter' => 'apcu',
    'options' => ['ttl' => 3600],
    'plugins' => [
        [
            'name' => 'exception_handler',
            'options' => [
                'throw_exceptions' => false,
             ], 
        ],
    ],
]);

// Alternately, create the adapter and plugin separately:
$cache  = $storageFactory->create('apcu', ['ttl' => 3600]);
$pluginFactory = $container->get(StoragePluginFactoryInterface::class);
$plugin = $pluginFactory->create('exception_handler', [
    'throw_exceptions' => false,
]);
$cache->addPlugin($plugin);

// Or do it completely manually:
$cache  = new Laminas\Cache\Storage\Adapter\Apcu();
$cache->getOptions()->setTtl(3600);

$plugin = new Laminas\Cache\Storage\Plugin\ExceptionHandler();
$plugin->getOptions()->setThrowExceptions(false);
$cache->addPlugin($plugin);

Many Methods throw Exceptions

Because many caching operations throw an exception on error, you need to catch them. You can do so manually, or you can use the plugin Laminas\Cache\Storage\Plugin\ExceptionHandler with throw_exceptions set to false to automatically catch them. You can also define an exception_callback to log exceptions.

Basic Configuration Options

The following configuration options are defined by Laminas\Cache\Storage\Adapter\AdapterOptions and are available for every supported adapter. Adapter-specific configuration options are described on adapter level below.

Option Data Type Default Value Description
ttl integer 0 Time to live
namespace string “laminascache” The “namespace” in which cache items will live
key_pattern null|string null Pattern against which to validate cache keys
readable boolean true Enable/Disable reading data from cache
writable boolean true Enable/Disable writing data to cache

StorageInterface

Laminas\Cache\Storage\StorageInterface is the basic interface implemented by all storage adapters.

namespace Laminas\Cache\Storage;

use Traversable;

interface StorageInterface
{
    /**
     * Set options.
     *
     * [@param](https://github.com/param) array|Traversable|Adapter\AdapterOptions $options
     * [@return](https://github.com/return) StorageInterface Fluent interface
     */
    public function setOptions($options);

    /**
     * Get options
     *
     * [@return](https://github.com/return) Adapter\AdapterOptions
     */
    public function getOptions();

    /* reading */

    /**
     * Get an item.
     *
     * [@param](https://github.com/param)  string  $key
     * [@param](https://github.com/param)  bool $success
     * [@param](https://github.com/param)  mixed   $casToken
     * [@return](https://github.com/return) mixed Data on success, null on failure
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function getItem($key, & $success = null, & $casToken = null);

    /**
     * Get multiple items.
     *
     * [@param](https://github.com/param)  array $keys
     * [@return](https://github.com/return) array Associative array of keys and values
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function getItems(array $keys);

    /**
     * Test if an item exists.
     *
     * [@param](https://github.com/param)  string $key
     * [@return](https://github.com/return) bool
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function hasItem($key);

    /**
     * Test multiple items.
     *
     * [@param](https://github.com/param)  array $keys
     * [@return](https://github.com/return) array Array of found keys
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function hasItems(array $keys);

    /**
     * Get metadata of an item.
     *
     * [@param](https://github.com/param)  string $key
     * [@return](https://github.com/return) array|bool Metadata on success, false on failure
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function getMetadata($key);

    /**
     * Get multiple metadata
     *
     * [@param](https://github.com/param)  array $keys
     * [@return](https://github.com/return) array Associative array of keys and metadata
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function getMetadatas(array $keys);

    /* writing */

    /**
     * Store an item.
     *
     * [@param](https://github.com/param)  string $key
     * [@param](https://github.com/param)  mixed  $value
     * [@return](https://github.com/return) bool
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function setItem($key, $value);

    /**
     * Store multiple items.
     *
     * [@param](https://github.com/param)  array $keyValuePairs
     * [@return](https://github.com/return) array Array of not stored keys
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function setItems(array $keyValuePairs);

    /**
     * Add an item.
     *
     * [@param](https://github.com/param)  string $key
     * [@param](https://github.com/param)  mixed  $value
     * [@return](https://github.com/return) bool
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function addItem($key, $value);

    /**
     * Add multiple items.
     *
     * [@param](https://github.com/param)  array $keyValuePairs
     * [@return](https://github.com/return) array Array of not stored keys
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function addItems(array $keyValuePairs);

    /**
     * Replace an existing item.
     *
     * [@param](https://github.com/param)  string $key
     * [@param](https://github.com/param)  mixed  $value
     * [@return](https://github.com/return) bool
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function replaceItem($key, $value);

    /**
     * Replace multiple existing items.
     *
     * [@param](https://github.com/param)  array $keyValuePairs
     * [@return](https://github.com/return) array Array of not stored keys
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function replaceItems(array $keyValuePairs);

    /**
     * Set an item only if token matches
     *
     * It uses the token received from getItem() to check if the item has
     * changed before overwriting it.
     *
     * [@param](https://github.com/param)  mixed  $token
     * [@param](https://github.com/param)  string $key
     * [@param](https://github.com/param)  mixed  $value
     * [@return](https://github.com/return) bool
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     * [@see](https://github.com/see)    getItem()
     * [@see](https://github.com/see)    setItem()
     */
    public function checkAndSetItem($token, $key, $value);

    /**
     * Reset lifetime of an item
     *
     * [@param](https://github.com/param)  string $key
     * [@return](https://github.com/return) bool
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function touchItem($key);

    /**
     * Reset lifetime of multiple items.
     *
     * [@param](https://github.com/param)  array $keys
     * [@return](https://github.com/return) array Array of not updated keys
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function touchItems(array $keys);

    /**
     * Remove an item.
     *
     * [@param](https://github.com/param)  string $key
     * [@return](https://github.com/return) bool
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function removeItem($key);

    /**
     * Remove multiple items.
     *
     * [@param](https://github.com/param)  array $keys
     * [@return](https://github.com/return) array Array of not removed keys
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function removeItems(array $keys);

    /**
     * Increment an item.
     *
     * [@param](https://github.com/param)  string $key
     * [@param](https://github.com/param)  int    $value
     * [@return](https://github.com/return) int|bool The new value on success, false on failure
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function incrementItem($key, $value);

    /**
     * Increment multiple items.
     *
     * [@param](https://github.com/param)  array $keyValuePairs
     * [@return](https://github.com/return) array Associative array of keys and new values
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function incrementItems(array $keyValuePairs);

    /**
     * Decrement an item.
     *
     * [@param](https://github.com/param)  string $key
     * [@param](https://github.com/param)  int    $value
     * [@return](https://github.com/return) int|bool The new value on success, false on failure
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function decrementItem($key, $value);

    /**
     * Decrement multiple items.
     *
     * [@param](https://github.com/param)  array $keyValuePairs
     * [@return](https://github.com/return) array Associative array of keys and new values
     * [@throws](https://github.com/throws) \Laminas\Cache\Exception\ExceptionInterface
     */
    public function decrementItems(array $keyValuePairs);

    /* status */

    /**
     * Capabilities of this storage
     *
     * [@return](https://github.com/return) Capabilities
     */
    public function getCapabilities();
}

AvailableSpaceCapableInterface

Laminas\Cache\Storage\AvailableSpaceCapableInterface implements a method to allow retrieving the current available space remaining in storage.

namespace Laminas\Cache\Storage;

interface AvailableSpaceCapableInterface
{
    /**
     * Get available space in bytes
     *
     * [@return](https://github.com/return) int|float
     */
    public function getAvailableSpace();
}

TotalSpaceCapableInterface

Laminas\Cache\Storage\TotalSpaceCapableInterface implements a method to allow retrieving the total storage space.

namespace Laminas\Cache\Storage;

interface TotalSpaceCapableInterface
{
    /**
     * Get total space in bytes
     *
     * [@return](https://github.com/return) int|float
     */
    public function getTotalSpace();
}

ClearByNamespaceInterface

Laminas\Cache\Storage\ClearByNamespaceInterface implements a method to allow clearing all cached items within a given namespace.

namespace Laminas\Cache\Storage;

interface ClearByNamespaceInterface
{
    /**
     * Remove items of given namespace
     *
     * [@param](https://github.com/param) string $namespace
     * [@return](https://github.com/return) bool
     */
    public function clearByNamespace($namespace);
}

ClearByPrefixInterface

Laminas\Cache\Storage\ClearByPrefixInterface implements a method to allow clearing all cached items that have a given prefix (within the currently configured namespace).

namespace Laminas\Cache\Storage;

interface ClearByPrefixInterface
{
    /**
     * Remove items matching given prefix
     *
     * [@param](https://github.com/param) string $prefix
     * [@return](https://github.com/return) bool
     */
    public function clearByPrefix($prefix);
}

ClearExpiredInterface

Laminas\Cache\Storage\ClearExpiredInterface implements a method to allow clearing all expired items (within the current configured namespace).

namespace Laminas\Cache\Storage;

interface ClearExpiredInterface
{
    /**
     * Remove expired items
     *
     * [@return](https://github.com/return) bool
     */
    public function clearExpired();
}

FlushableInterface

Laminas\Cache\Storage\FlushableInterface implements a method for flushing the entire cache storage.

namespace Laminas\Cache\Storage;

interface FlushableInterface
{
    /**
     * Flush the whole storage
     *
     * [@return](https://github.com/return) bool
     */
    public function flush();
}

IterableInterface

Laminas\Cache\Storage\IterableInterface implements a method for retrieving an iterator of all items in storage. It extends IteratorAggregate, so it's possible to directly iterate over the storage implementations that implement this interface using foreach.

namespace Laminas\Cache\Storage;

use IteratorAggregate;

/**
 *
 * [@method](https://github.com/method) IteratorInterface getIterator() Get the storage iterator
 */
interface IterableInterface extends IteratorAggregate
{
    /**
     * [@return](https://github.com/return) \Traversable
     */
    public function getIterator();
}

OptimizableInterface

Laminas\Cache\Storage\OptimizableInterface implements a method for running optimization processes on the storage adapter.

namespace Laminas\Cache\Storage;

interface OptimizableInterface
{
    /**
     * Optimize the storage
     *
     * [@return](https://github.com/return) bool
     */
    public function optimize();
}

TaggableInterface

Laminas\Cache\Storage\TaggableInterface implements methods for tagging items, and cleaning (expiring) items matching tags.

namespace Laminas\Cache\Storage;

interface TaggableInterface
{
    /**
     * Set tags to an item by given key.
     * An empty array will remove all tags.
     *
     * [@param](https://github.com/param) string   $key
     * [@param](https://github.com/param) string[] $tags
     * [@return](https://github.com/return) bool
     */
    public function setTags($key, array $tags);

    /**
     * Get tags of an item by given key
     *
     * [@param](https://github.com/param) string $key
     * [@return](https://github.com/return) string[]|FALSE
     */
    public function getTags($key);

    /**
     * Remove items matching given tags.
     *
     * If $disjunction only one of the given tags must match
     * else all given tags must match.
     *
     * [@param](https://github.com/param) string[] $tags
     * [@param](https://github.com/param)  bool  $disjunction
     * [@return](https://github.com/return) bool
     */
    public function clearByTags(array $tags, $disjunction = false);
}

APCu Adapter

Laminas\Cache\Storage\Adapter\Apcu stores cache items in shared memory through the PHP extension APCu (Alternative PHP Cache).

This adapter implements the following interfaces:

  • Laminas\Cache\Storage\StorageInterface
  • Laminas\Cache\Storage\AvailableSpaceCapableInterface
  • Laminas\Cache\Storage\ClearByNamespaceInterface
  • Laminas\Cache\Storage\ClearByPrefixInterface
  • Laminas\Cache\Storage\FlushableInterface
  • Laminas\Cache\Storage\IterableInterface
  • Laminas\Cache\Storage\TotalSpaceCapableInterface

Capabilities

Capability Value
supportedDatatypes null, bool, int, float, string, array (serialized), object (serialized)
supportedMetadata internal_key, atime, ctime, mtime, rtime, size, hits, ttl
minTtl 1
maxTtl 0
staticTtl true
ttlPrecision 1
useRequestTime value of apc.use_request_time from php.ini
lockOnExpire 0
maxKeyLength 5182
namespaceIsPrefix true
namespaceSeparator Option value of namespace_separator

Adapter Specific Options

Name Data Type Default Value Description
namespace_separator string ":" A separator for the namespace and prefix.

BlackHole Adapter

Laminas\Cache\Storage\Adapter\BlackHole does not store any cache items. This adapter is useful to bypass caching behavior. This might be the case in development mode or unit testing.

This adapter implements the following interfaces:

  • Laminas\Cache\Storage\StorageInterface
  • Laminas\Cache\Storage\AvailableSpaceCapableInterface
  • Laminas\Cache\Storage\ClearByNamespaceInterface
  • Laminas\Cache\Storage\ClearByPrefixInterface
  • Laminas\Cache\Storage\ClearExpiredInterface
  • Laminas\Cache\Storage\FlushableInterface
  • Laminas\Cache\Storage\IterableInterface
  • Laminas\Cache\Storage\OptimizableInterface
  • Laminas\Cache\Storage\TaggableInterface
  • Laminas\Cache\Storage\TotalSpaceCapableInterface

Capabilities

Capability Value
supportedDatatypes null, bool, int, float, string, array, object
supportedMetadata none
minTtl 0 or 1, depending on psr option.
maxTtl 0
staticTtl false or true, depending on psr option
ttlPrecision 1
useRequestTime false
lockOnExpire 0
maxKeyLength -1
namespaceIsPrefix true
namespaceSeparator none

Adapter Specific Options

Name Data Type Default Value Description
psr bool false Flag to specify whether the adapter should be compatible with CacheItemPoolDecorator or SimpleCacheDecorator

Deprecation Notice

The psr option was introduce to provide non-BC compatible way to use the BlackHole adapter with the PSR-6 and PSR-16 decorator. Ignore this option if this adapter is not used in combination with these decorators. This option is already flagged as internal and thus will be removed in laminas/laminas-cache-storage-adapter-blackhole 2.0.

Filesystem Adapter

Laminas\Cache\Storage\Adapter\Filesystem stores cache items on the filesystem.

This adapter implements the following interfaces:

  • Laminas\Cache\Storage\StorageInterface
  • Laminas\Cache\Storage\AvailableSpaceCapableInterface
  • Laminas\Cache\Storage\ClearByNamespaceInterface
  • Laminas\Cache\Storage\ClearByPrefixInterface
  • Laminas\Cache\Storage\ClearExpiredInterface
  • Laminas\Cache\Storage\FlushableInterface
  • Laminas\Cache\Storage\IterableInterface
  • Laminas\Cache\Storage\OptimizableInterface
  • Laminas\Cache\Storage\TaggableInterface
  • Laminas\Cache\Storage\TotalSpaceCapableInterface

Capabilities

Capability Value
supportedDatatypes string, null => string, boolean => string, integer => string, double => string
supportedMetadata mtime, filespec, atime, ctime
minTtl 1
maxTtl 0
staticTtl false
ttlPrecision 1
useRequestTime false
lockOnExpire 0
maxKeyLength 251
namespaceIsPrefix true
namespaceSeparator Option value of namespace_separator

Adapter Specific Options

| Name | Data Type | Default Value | Description | |-----------------------|----------...

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport