Zend\Cache\Storage\Adapter

Overview

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

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

All adapters implements the interface Zend\Cache\Storage\Adapter and most extend Zend\Cache\Storage\Adapter\AbstractAdapter, which comes with basic logic.

Configuration is handled by either Zend\Cache\Storage\Adapter\AdapterOptions, or an adapter-specific options class if it exists. You may pass the options instance to the class at instantiation or 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 either the options instance or associative array to the Zend\Cache\StorageFactory::factory method.

Note

Many methods throw exceptions

Because many caching methods can throw exceptions, you need to catch them manually or you can use the plug-in Zend\Cache\Storage\Plugin\ExceptionHandler to automatically catch them and redirect exceptions into a log file using the option “exception_callback”.

Quick Start

Caching adapters can either be created from the provided Zend\Cache\StorageFactory factory, or by simply instantiating one of the Zend\Cache\Storage\Adapter\*classes.

To make life easier, the Zend\Cache\StorageFactory comes with a factory method to create an adapter and create/add all requested plugins at once.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use Zend\Cache\StorageFactory;

// Via factory:
$cache = StorageFactory::factory(array(
    'adapter' => 'apc',
    'plugins' => array(
        'exception_handler' => array('throw_exceptions' => false),
    ),
));

// Alternately:
$cache  = StorageFactory::adapterFactory('apc');
$plugin = StorageFactory::pluginFactory('exception_handler', array(
    'throw_exceptions' => false,
));
$cache->addPlugin($plugin);

// Or manually:
$cache  = new Zend\Cache\Storage\Adapter\Apc();
$plugin = new Zend\Cache\Storage\Plugin\ExceptionHandler(array(
    'throw_exceptions' => false,
));
$cache->addPlugin($plugin);

Configuration Options

ignore_missing_items

Enables or disables ignoring of missing items.

If enabled and a missing item was requested:

  • getItem, getMetadata: return false
  • removeItem[s]: return true
  • incrementItem[s], decrementItem[s]: add a new item with 0 as base
  • touchItem[s]: add new empty item

If disabled and a missing item was requested:

  • getItem, getMetadata, incrementItem[s], decrementItem[s], touchItem[s]
  • setIgnoreMissingItems(boolean $flag) Implements a fluent interface.
  • getIgnoreMissingItems() Returns boolean
key_pattern

Pattern against which to validate cache keys.

  • setKeyPattern(null|string $pattern) Implements a fluent interface.
  • getKeyPattern() Returns string
namespace

The “namespace” in which cache items will live.

  • setNamespace(string $namespace) Implements a fluent interface.
  • getNamespace() Returns string
namespace_pattern

Pattern against which to validate namespace values.

  • setNamespacePattern(null|string $pattern) Implements a fluent interface.
  • getNamespacePattern() Returns string
readable

Enable/Disable reading data from cache.

  • setReadable(boolean $flag) Implements a fluent interface.
  • getReadable() Returns boolean
ttl

Set time to live.

  • setTtl(int|float $ttl) Implements a fluent interface.
  • getTtl() Returns float
writable

Enable/Disable writing data to cache.

  • setWritable(boolean $flag) Implements a fluent interface.
  • getWritable() Returns boolean

Available Methods

setOptions

setOptions(array|Traversable|Zend\Cache\Storage\Adapter\AdapterOptions $options)

Set options.

Implements a fluent interface.

getOptions

getOptions()

Get options

Returns Zend\Cache\Storage\Adapter\AdapterOptions

getItem

getItem(string $key, array $options = array ())

Get an item.

Returns mixed

getItems

getItems(array $keys, array $options = array ())

Get multiple items.

Returns array

hasItem

hasItem(string $key, array $options = array ())

Test if an item exists.

Returns boolean

hasItems

hasItems(array $keys, array $options = array ())

Test multiple items.

Returns array

getMetadata

getMetadata(string $key, array $options = array ())

Get metadata of an item.

Returns array|boolean

getMetadatas

getMetadatas(array $keys, array $options = array ())

Get multiple metadata

Returns array

setItem

setItem(string $key, mixed $value, array $options = array ())

Store an item.

Returns boolean

setItems

setItems(array $keyValuePairs, array $options = array ())

Store multiple items.

Returns boolean

addItem

addItem(string $key, mixed $value, array $options = array ())

Add an item.

Returns boolean

addItems

addItems(array $keyValuePairs, array $options = array ())

Add multiple items.

Returns boolean

replaceItem

replaceItem(string $key, mixed $value, array $options = array ())

Replace an item.

Returns boolean

replaceItems

replaceItems(array $keyValuePairs, array $options = array ())

Replace multiple items.

Returns boolean

checkAndSetItem

checkAndSetItem(mixed $token, string|null $key, mixed $value, array $options = array ())

Set item only if token matches

It uses the token from received from getItem() to check if the item has changed before overwriting it.

Returns boolean

touchItem

touchItem(string $key, array $options = array ())

Reset lifetime of an item

Returns boolean

touchItems

touchItems(array $keys, array $options = array ())

Reset lifetime of multiple items.

Returns boolean

removeItem

removeItem(string $key, array $options = array ())

Remove an item.

Returns boolean

removeItems

removeItems(array $keys, array $options = array ())

Remove multiple items.

Returns boolean

incrementItem

incrementItem(string $key, int $value, array $options = array ())

Increment an item.

Returns int|boolean

incrementItems

incrementItems(array $keyValuePairs, array $options = array ())

Increment multiple items.

Returns boolean

decrementItem

decrementItem(string $key, int $value, array $options = array ())

Decrement an item.

Returns int|boolean

decrementItems

decrementItems(array $keyValuePairs, array $options = array ())

Decrement multiple items.

Returns boolean

getDelayed

getDelayed(array $keys, array $options = array ())

Request multiple items.

Returns boolean

find

find(int $mode = 2, array $options = array ())

Find items.

Returns boolean

fetch

fetch()

Fetches the next item from result set

Returns array|boolean

fetchAll

fetchAll()

Returns all items of result set.

Returns array

clear

clear(int $mode = 1, array $options = array ())

Clear items off all namespaces.

Returns boolean

clearByNamespace

clearByNamespace(int $mode = 1, array $options = array ())

Clear items by namespace.

Returns boolean

optimize

optimize(array $options = array ())

Optimize adapter storage.

Returns boolean

getCapabilities

getCapabilities()

Capabilities of this storage

Returns Zend\Cache\Storage\Capabilities

getCapacity

getCapacity(array $options = array ())

Get storage capacity.

Returns array|boolean

TODO: Examples

Project Versions

Table Of Contents

Previous topic

Zend_Cache Frontends

Next topic

Zend\Cache\Storage\Capabilities

This Page

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Go to Zend\Cache\Storage\Adapter on GitHub.
  2. Edit file contents using GitHub's text editor in your web browser
  3. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  4. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  5. Your changes are now queued for review under project's Pull requests tab on GitHub.