class Apcu4Backend extends ApcuBackend (View source)

deprecated in drupal:8.8.0 and is removed from from drupal:9.0.0. Use \Drupal\Core\Cache\ApcuBackend instead.

Stores cache items in the Alternative PHP Cache User Cache (APCu).

This class is used with APCu versions >= 4.0.0 and < 5.0.0.

Properties

protected string $bin

The name of the cache bin to use.

from  ApcuBackend
protected string $sitePrefix

Prefix for all keys in the storage that belong to this site.

from  ApcuBackend
protected string $binPrefix

Prefix for all keys in this cache bin.

from  ApcuBackend
protected CacheTagsChecksumInterface $checksumProvider

The cache tags checksum provider.

from  ApcuBackend

Methods

__construct(string $bin, string $site_prefix, CacheTagsChecksumInterface $checksum_provider)

Constructs a new ApcuBackend instance.

string
getApcuKey(string $cid)

Prepends the APCu user variable prefix for this bin to a cache item ID.

object|false
get(string $cid, bool $allow_invalid = FALSE)

Returns data from the persistent cache.

array
getMultiple(array $cids, bool $allow_invalid = FALSE)

Returns data from the persistent cache when given an array of cache IDs.

APCUIterator
getAll(string $prefix = '')

Returns all cached items, optionally limited by a cache ID prefix.

mixed
prepareItem(object $cache, bool $allow_invalid)

Prepares a cached item.

set(string $cid, mixed $data, int $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = [])

Stores data in the persistent cache.

setMultiple(array $items = [])

Store multiple items in the persistent cache.

delete(string $cid)

Deletes an item from the cache.

deleteMultiple(array $cids)

Deletes multiple items from the cache.

deleteAll()

Deletes all cache items in a bin.

garbageCollection()

Performs garbage collection on a cache bin.

removeBin()

Remove a cache bin.

invalidate(string $cid)

Marks a cache item as invalid.

invalidateMultiple(array $cids)

Marks cache items as invalid.

invalidateAll()

Marks all cache items as invalid.

APCUIterator
getIterator(mixed $search = NULL, int $format = APC_ITER_ALL, int $chunk_size = 100, int $list = APC_LIST_ACTIVE)

Instantiates and returns the APCUIterator class.

Details

__construct(string $bin, string $site_prefix, CacheTagsChecksumInterface $checksum_provider)

Constructs a new ApcuBackend instance.

Parameters

string $bin

The name of the cache bin.

string $site_prefix

The prefix to use for all keys in the storage that belong to this site.

CacheTagsChecksumInterface $checksum_provider

The cache tags checksum provider.

string getApcuKey(string $cid)

Prepends the APCu user variable prefix for this bin to a cache item ID.

Parameters

string $cid

The cache item ID to prefix.

Return Value

string

The APCu key for the cache item ID.

object|false get(string $cid, bool $allow_invalid = FALSE)

Returns data from the persistent cache.

Parameters

string $cid

The cache ID of the data to retrieve.

bool $allow_invalid

(optional) If TRUE, a cache item may be returned even if it is expired or has been invalidated. Such items may sometimes be preferred, if the alternative is recalculating the value stored in the cache, especially if another concurrent request is already recalculating the same value. The "valid" property of the returned object indicates whether the item is valid or not. Defaults to FALSE.

Return Value

object|false

The cache item or FALSE on failure.

array getMultiple(array $cids, bool $allow_invalid = FALSE)

Returns data from the persistent cache when given an array of cache IDs.

Parameters

array $cids

An array of cache IDs for the data to retrieve. This is passed by reference, and will have the IDs successfully returned from cache removed.

bool $allow_invalid

(optional) If TRUE, cache items may be returned even if they have expired or been invalidated. Such items may sometimes be preferred, if the alternative is recalculating the value stored in the cache, especially if another concurrent thread is already recalculating the same value. The "valid" property of the returned objects indicates whether the items are valid or not. Defaults to FALSE.

Return Value

array

An array of cache item objects indexed by cache ID.

protected APCUIterator getAll(string $prefix = '')

Returns all cached items, optionally limited by a cache ID prefix.

APCu is a memory cache, shared across all server processes. To prevent cache item clashes with other applications/installations, every cache item is prefixed with a unique string for this site. Therefore, functions like apcu_clear_cache() cannot be used, and instead, a list of all cache items belonging to this application need to be retrieved through this method instead.

Parameters

string $prefix

(optional) A cache ID prefix to limit the result to.

Return Value

APCUIterator

An APCUIterator containing matched items.

protected mixed prepareItem(object $cache, bool $allow_invalid)

Prepares a cached item.

Checks that the item is either permanent or did not expire.

Parameters

object $cache

An item loaded from self::get() or self::getMultiple().

bool $allow_invalid

If TRUE, a cache item may be returned even if it is expired or has been invalidated. See ::get().

Return Value

mixed

The cache item or FALSE if the item expired.

set(string $cid, mixed $data, int $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = [])

Stores data in the persistent cache.

Core cache implementations set the created time on cache item with microtime(TRUE) rather than REQUEST_TIME_FLOAT, because the created time of cache items should match when they are created, not when the request started. Apart from being more accurate, this increases the chance an item will legitimately be considered valid.

Parameters

string $cid

The cache ID of the data to store.

mixed $data

The data to store in the cache. Some storage engines only allow objects up to a maximum of 1MB in size to be stored by default. When caching large arrays or similar, take care to ensure $data does not exceed this size.

int $expire

One of the following values:

  • CacheBackendInterface::CACHE_PERMANENT: Indicates that the item should not be removed unless it is deleted explicitly.
  • A Unix timestamp: Indicates that the item will be considered invalid after this time, i.e. it will not be returned by get() unless $allow_invalid has been set to TRUE. When the item has expired, it may be permanently deleted by the garbage collector at any time.
array $tags

An array of tags to be stored with the cache item. These should normally identify objects used to build the cache item, which should trigger cache invalidation when updated. For example if a cached item represents a node, both the node ID and the author's user ID might be passed in as tags. For example ['node:123', 'node:456', 'user:789'].

setMultiple(array $items = [])

Store multiple items in the persistent cache.

Parameters

array $items

An array of cache items, keyed by cid. In the form: @code $items = array( $cid => array( // Required, will be automatically serialized if not a string. 'data' => $data, // Optional, defaults to CacheBackendInterface::CACHE_PERMANENT. 'expire' => CacheBackendInterface::CACHE_PERMANENT, // (optional) The cache tags for this item, see CacheBackendInterface::set(). 'tags' => array(), ), ); @endcode

delete(string $cid)

Deletes an item from the cache.

If the cache item is being deleted because it is no longer "fresh", you may consider using invalidate() instead. This allows callers to retrieve the invalid item by calling get() with $allow_invalid set to TRUE. In some cases an invalid item may be acceptable rather than having to rebuild the cache.

Parameters

string $cid

The cache ID to delete.

deleteMultiple(array $cids)

Deletes multiple items from the cache.

If the cache items are being deleted because they are no longer "fresh", you may consider using invalidateMultiple() instead. This allows callers to retrieve the invalid items by calling get() with $allow_invalid set to TRUE. In some cases an invalid item may be acceptable rather than having to rebuild the cache.

Parameters

array $cids

An array of cache IDs to delete.

deleteAll()

Deletes all cache items in a bin.

garbageCollection()

Performs garbage collection on a cache bin.

The backend may choose to delete expired or invalidated items.

removeBin()

Remove a cache bin.

invalidate(string $cid)

Marks a cache item as invalid.

Invalid items may be returned in later calls to get(), if the $allow_invalid argument is TRUE.

Parameters

string $cid

The cache ID to invalidate.

invalidateMultiple(array $cids)

Marks cache items as invalid.

Invalid items may be returned in later calls to get(), if the $allow_invalid argument is TRUE.

Parameters

array $cids

An array of cache IDs to invalidate.

invalidateAll()

Marks all cache items as invalid.

Invalid items may be returned in later calls to get(), if the $allow_invalid argument is TRUE.

protected APCUIterator getIterator(mixed $search = NULL, int $format = APC_ITER_ALL, int $chunk_size = 100, int $list = APC_LIST_ACTIVE)

Instantiates and returns the APCUIterator class.

Parameters

mixed $search

A PCRE regular expression that matches against APC key names, either as a string for a single regular expression, or as an array of regular expressions. Or, optionally pass in NULL to skip the search.

int $format

The desired format, as configured with one or more of the APCITER* constants.

int $chunk_size

The chunk size. Must be a value greater than 0. The default value is 100.

int $list

The type to list. Either pass in APC_LIST_ACTIVE or APC_LIST_DELETED.

Return Value

APCUIterator