MemcachedBackend

class \TYPO3\CMS\Core\Cache\Backend\MemcachedBackend

Extends
AbstractBackend
Implements
TaggableBackendInterface, TransientBackendInterface

A caching backend which stores cache entries by using Memcached.

This backend uses the following types of Memcache keys:

  • tag_xxx xxx is tag name, value is array of associated identifiers identifier. This is "forward" tag index. It is mainly used for obtaining content by tag (get identifier by tag -> get content by identifier)
  • ident_xxx xxx is identifier, value is array of associated tags. This is "reverse" tag index. It provides quick access for all tags associated with this identifier and used when removing the identifier

Each key is prepended with a prefix. By default prefix consists from two parts separated by underscore character and ends in yet another underscore character:

  • "TYPO3"
  • Current site path obtained from Environment::getProjectPath() This prefix makes sure that keys from the different installations do not conflict.

Note: When using the Memcached backend to store values of more than ~1 MB, the data will be split into chunks to make them fit into the memcached limits.

Table of Contents

Interfaces

TaggableBackendInterface
A contract for a cache backend which supports tagging.
TransientBackendInterface
A contract for a cache backends which store variables in volatile memory and as such support receiving any variable type to store.

Constants

DATETIME_EXPIRYTIME_UNLIMITED  : mixed = '9999-12-31T23:59:59+0000'
MAX_BUCKET_SIZE  : int = 1048534
Max bucket size, (1024*1024)-42 bytes
UNLIMITED_LIFETIME  : mixed = 0

Properties

$cache  : FrontendInterface
Reference to the cache which uses this backend
$cacheIdentifier  : string
$context  : string
The current application context
$defaultLifetime  : int
Default lifetime of a cache entry in seconds
$flags  : int
Indicates whether the memcache uses compression or not (requires zlib), either 0 or \Memcached::OPT_COMPRESSION / MEMCACHE_COMPRESSED
$identifierPrefix  : string
A prefix to separate stored data from other data possibly stored in the memcache
$memcache  : Memcache|Memcached
Instance of the PHP Memcache class
$servers  : array
Array of Memcache server configurations
$usedPeclModule  : string
Used PECL module for memcached

Methods

__construct()  : mixed
Constructs this backend
collectGarbage()  : mixed
Does nothing, as memcached does GC itself
findIdentifiersByTag()  : array
Finds and returns all cache entry identifiers which are tagged by the specified tag.
flush()  : mixed
Removes all cache entries of this cache.
flushByTag()  : mixed
Removes all cache entries of this cache which are tagged by the specified tag.
flushByTags()  : mixed
Backwards compatibility safeguard since re-introducing flushByTags as API.
get()  : mixed
Loads data from the cache.
has()  : bool
Checks if a cache entry with the specified identifier exists.
initializeObject()  : mixed
Initializes the identifier prefix
remove()  : bool
Removes all cache entries matching the specified identifier.
set()  : mixed
Saves data in the cache.
setCache()  : mixed
Initializes the identifier prefix when setting the cache.
setDefaultLifetime()  : mixed
Sets the default lifetime for this cache backend
setPeclModule()  : mixed
Sets the preferred PECL module
addIdentifierToTags()  : mixed
Associates the identifier with the given tags
calculateExpiryTime()  : DateTime
Calculates the expiry time by the given lifetime. If no lifetime is specified, the default lifetime is used.
findTagsByIdentifier()  : array
Finds all tags for the given identifier. This function uses reverse tag index to search for tags.
getCompression()  : bool
Getter for compression flag
removeIdentifierFromAllTags()  : mixed
Removes association of the identifier with the given tags
setCompression()  : mixed
Setter for compression flags bit
setInternal()  : bool
Stores the actual data inside memcache/memcached
setServers()  : mixed
Setter for servers to be used. Expects an array, the values are expected to be formatted like "<host>[:<port>]" or "unix://<path>"

Constants

DATETIME_EXPIRYTIME_UNLIMITED

public mixed DATETIME_EXPIRYTIME_UNLIMITED = '9999-12-31T23:59:59+0000'

MAX_BUCKET_SIZE

Max bucket size, (1024*1024)-42 bytes

public int MAX_BUCKET_SIZE = 1048534

UNLIMITED_LIFETIME

public mixed UNLIMITED_LIFETIME = 0

Properties

$cacheIdentifier

protected string $cacheIdentifier

$context

The current application context

protected string $context

This variable is currently unused and set to "production" always. It is only kept to keep backwards compatibility.

$defaultLifetime

Default lifetime of a cache entry in seconds

protected int $defaultLifetime = 3600

$flags

Indicates whether the memcache uses compression or not (requires zlib), either 0 or \Memcached::OPT_COMPRESSION / MEMCACHE_COMPRESSED

protected int $flags

$identifierPrefix

A prefix to separate stored data from other data possibly stored in the memcache

protected string $identifierPrefix

$memcache

Instance of the PHP Memcache class

protected Memcache|Memcached $memcache

$servers

Array of Memcache server configurations

protected array $servers = []

$usedPeclModule

Used PECL module for memcached

protected string $usedPeclModule = ''

Methods

__construct()

Constructs this backend

public __construct(string $context[, array $options = [] ]) : mixed

Parameters

$context : string

Unused, for backward compatibility only

$options : array = []

Configuration options - depends on the actual backend

Tags

throws
Exception

if memcache is not installed

collectGarbage()

Does nothing, as memcached does GC itself

public collectGarbage() : mixed

findIdentifiersByTag()

Finds and returns all cache entry identifiers which are tagged by the specified tag.

public findIdentifiersByTag(string $tag) : array

Parameters

$tag : string

The tag to search for

Return values

array —

An array of entries with all matching entries. An empty array if no entries matched

flush()

Removes all cache entries of this cache.

public flush() : mixed

Tags

throws
Exception

flushByTag()

Removes all cache entries of this cache which are tagged by the specified tag.

public flushByTag(string $tag) : mixed

Parameters

$tag : string

The tag the entries must have

flushByTags()

Backwards compatibility safeguard since re-introducing flushByTags as API.

public flushByTags(string[] $tags) : mixed

See https://review.typo3.org/#/c/50537/ comments for patch set 14.

The method is here even though it is only required for TaggableBackendInterface. We add it here to ensure third party cache backends do not fail but instead delegate to a less efficient linear flushing behavior.

Parameters

$tags : string[]

get()

Loads data from the cache.

public get(string $entryIdentifier) : mixed

Parameters

$entryIdentifier : string

An identifier which describes the cache entry to load

Return values

mixed —

The cache entry's content as a string or FALSE if the cache entry could not be loaded

has()

Checks if a cache entry with the specified identifier exists.

public has(string $entryIdentifier) : bool

Parameters

$entryIdentifier : string

An identifier specifying the cache entry

Return values

bool —

TRUE if such an entry exists, FALSE if not

initializeObject()

Initializes the identifier prefix

public initializeObject() : mixed

Tags

throws
Exception

remove()

Removes all cache entries matching the specified identifier.

public remove(string $entryIdentifier) : bool

Usually this only affects one entry but if - for what reason ever - old entries for the identifier still exist, they are removed as well.

Parameters

$entryIdentifier : string

Specifies the cache entry to remove

Return values

bool —

TRUE if (at least) an entry could be removed or FALSE if no entry was found

set()

Saves data in the cache.

public set(string $entryIdentifier, string $data[, array $tags = [] ][, int $lifetime = null ]) : mixed

Parameters

$entryIdentifier : string

An identifier for this specific cache entry

$data : string

The data to be stored

$tags : array = []

Tags to associate with this cache entry

$lifetime : int = null

Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.

Tags

throws
Exception

if no cache frontend has been set.

throws
InvalidArgumentException

if the identifier is not valid or the final memcached key is longer than 250 characters

setDefaultLifetime()

Sets the default lifetime for this cache backend

public setDefaultLifetime(int $defaultLifetime) : mixed

Parameters

$defaultLifetime : int

Default lifetime of this cache backend in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.

Tags

throws
InvalidArgumentException

setPeclModule()

Sets the preferred PECL module

public setPeclModule(string $peclModule) : mixed

Parameters

$peclModule : string

Tags

throws
Exception

addIdentifierToTags()

Associates the identifier with the given tags

protected addIdentifierToTags(string $entryIdentifier, array $tags) : mixed

Parameters

$entryIdentifier : string
$tags : array

calculateExpiryTime()

Calculates the expiry time by the given lifetime. If no lifetime is specified, the default lifetime is used.

protected calculateExpiryTime([int $lifetime = null ]) : DateTime

Parameters

$lifetime : int = null

The lifetime in seconds

Return values

DateTime —

The expiry time

findTagsByIdentifier()

Finds all tags for the given identifier. This function uses reverse tag index to search for tags.

protected findTagsByIdentifier(string $identifier) : array

Parameters

$identifier : string

Identifier to find tags by

Return values

array

getCompression()

Getter for compression flag

protected getCompression() : bool

Return values

bool

removeIdentifierFromAllTags()

Removes association of the identifier with the given tags

protected removeIdentifierFromAllTags(string $entryIdentifier) : mixed

Parameters

$entryIdentifier : string

setCompression()

Setter for compression flags bit

protected setCompression(bool $useCompression) : mixed

Parameters

$useCompression : bool

setInternal()

Stores the actual data inside memcache/memcached

protected setInternal(string $entryIdentifier, mixed $data, int $expiration) : bool

Parameters

$entryIdentifier : string
$data : mixed
$expiration : int

Return values

bool

setServers()

Setter for servers to be used. Expects an array, the values are expected to be formatted like "<host>[:<port>]" or "unix://<path>"

protected setServers(array $servers) : mixed

Parameters

$servers : array

An array of servers to add.

On this page