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.
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.
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(
'apc',
['ttl' => 3600],
[
[
'name' => 'exception_handler',
'options' => [
'throw_exceptions' => false,
],
],
]
);
// Via array configuration:
$cache = $storageFactory->createFromArrayConfiguration([
'adapter' => 'apc',
'options' => ['ttl' => 3600],
'plugins' => [
[
'name' => 'exception_handler',
'options' => [
'throw_exceptions' => false,
],
],
],
]);
// Alternately, create the adapter and plugin separately:
$cache = $storageFactory->create('apc', ['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\Apc();
$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\ExceptionHandlerwiththrow_exceptionsset tofalseto automatically catch them. You can also define anexception_callbackto log exceptions.
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 |
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();
}
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();
}
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();
}
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);
}
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);
}
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();
}
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();
}
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();
}
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();
}
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);
}
Laminas\Cache\Storage\Adapter\Apc stores cache items in shared memory through the
PHP extension APC (Alternative PHP Cache).
This adapter implements the following interfaces:
Laminas\Cache\Storage\StorageInterfaceLaminas\Cache\Storage\AvailableSpaceCapableInterfaceLaminas\Cache\Storage\ClearByNamespaceInterfaceLaminas\Cache\Storage\ClearByPrefixInterfaceLaminas\Cache\Storage\FlushableInterfaceLaminas\Cache\Storage\IterableInterfaceLaminas\Cache\Storage\TotalSpaceCapableInterface| 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 |
| Name | Data Type | Default Value | Description |
|---|---|---|---|
namespace_separator |
string |
":" | A separator for the namespace and prefix. |
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\StorageInterfaceLaminas\Cache\Storage\AvailableSpaceCapableInterfaceLaminas\Cache\Storage\ClearByNamespaceInterfaceLaminas\Cache\Storage\ClearByPrefixInterfaceLaminas\Cache\Storage\ClearExpiredInterfaceLaminas\Cache\Storage\FlushableInterfaceLaminas\Cache\Storage\IterableInterfaceLaminas\Cache\Storage\OptimizableInterfaceLaminas\Cache\Storage\TaggableInterfaceLaminas\Cache\Storage\TotalSpaceCapableInterface| 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 |
| 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
psroption was introduce to provide non-BC compatible way to use theBlackHoleadapter 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 inlaminas/laminas-cache-storage-adapter-blackhole2.0.
Laminas\Cache\Storage\Adapter\Dba stores cache items into
dbm-like databases using the PHP extension
dba.
This adapter implements the following interfaces:
Laminas\Cache\Storage\StorageInterfaceLaminas\Cache\Storage\AvailableSpaceCapableInterfaceLaminas\Cache\Storage\ClearByNamespaceInterfaceLaminas\Cache\Storage\ClearByPrefixInterfaceLaminas\Cache\Storage\FlushableInterfaceLaminas\Cache\Storage\IterableInterfaceLaminas\Cache\Storage\OptimizableInterfaceLaminas\Cache\Storage\TotalSpaceCapableInterface| Capability | Value |
|---|---|
supportedDatatypes |
string, null => string, boolean => string, integer => string, double => string |
supportedMetadata |
none |
minTtl |
0 |
maxKeyLength |
0 |
namespaceIsPrefix |
true |
namespaceSeparator |
Option value of namespace_separator |
| Name | Data Type | Default Value | Description |
|---|---|---|---|
namespace_separator |
string |
":" | A separator for the namespace and prefix. |
pathname |
string |
"" | Pathname to the database file. |
mode |
string |
"c" | The mode with which to open the database; plea... |
How can I help you explore Laravel packages today?