class MemcachedBackend extends AbstractBackend implements TaggableBackendInterface, PhpCapableBackendInterface (View source)

A caching backend which stores cache entries by using Memcache/Memcached.

This backend uses the following types of cache 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
  • tagIndex Value is a List of all tags (array)

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:

  • "Flow"
  • MD5 of script path and filename and SAPI name This prefix makes sure that keys from the different installations do not conflict.

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

Traits

RequireOnceFromValueTrait

Constants

DATETIME_EXPIRYTIME_UNLIMITED

UNLIMITED_LIFETIME

MAX_BUCKET_SIZE

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

Properties

protected FrontendInterface $cache

Reference to the cache frontend which uses this backend

from  AbstractBackend
protected string $cacheIdentifier from  AbstractBackend
protected string $identifierPrefix

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

protected int $defaultLifetime

Default lifetime of a cache entry in seconds

from  AbstractBackend
protected EnvironmentConfiguration $environmentConfiguration from  AbstractBackend
protected array $_requiredEntryIdentifiers from  RequireOnceFromValueTrait
protected Memcache|Memcached $memcache

Instance of the PHP Memcache/Memcached class

protected array $servers

Array of Memcache server configurations

protected int $flags

Indicates whether the memcache uses compression or not (requires zlib), either 0 or MEMCACHE_COMPRESSED

Methods

__construct(EnvironmentConfiguration $environmentConfiguration, array $options = [])

Constructs this backend

void
setProperties(array $properties, bool $throwExceptionIfPropertyNotSettable = true)

No description

bool
setProperty(string $propertyName, mixed $propertyValue)

No description

void
setCache(FrontendInterface $cache)

Initializes the identifier prefix when setting the cache.

void
setDefaultLifetime(int|string $defaultLifetime)

Sets the default lifetime for this cache backend

calculateExpiryTime(int $lifetime = null)

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

string
getPrefixedIdentifier(string $entryIdentifier)

Returns the internally used, prefixed entry identifier for the given public entry identifier.

mixed
requireOnce(string $entryIdentifier)

Loads PHP code from the cache and require_onces it right away.

void
setServers(array $servers)

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

void
setCompression(bool $useCompression)

Setter for compression flags bit

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

Saves data in the cache.

bool
setItem(string $key, string $value, int $expiration)

Stores an item on the server

mixed
get(string $entryIdentifier)

Loads data from the cache.

bool
has(string $entryIdentifier)

Checks if a cache entry with the specified identifier exists.

bool
remove(string $entryIdentifier)

Removes all cache entries matching the specified identifier.

array
findIdentifiersByTag(string $tag)

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

array
findTagsByIdentifier(string $identifier)

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

void
flush()

Removes all cache entries of this cache.

int
flushByTag(string $tag)

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

int
flushByTags(array $tags)

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

void
addIdentifierToTags(string $entryIdentifier, array $tags)

Associates the identifier with the given tags

void
removeIdentifierFromAllTags(string $entryIdentifier)

Removes association of the identifier with the given tags

void
collectGarbage()

Does nothing, as memcache/memcached does GC itself

Details

__construct(EnvironmentConfiguration $environmentConfiguration, array $options = [])

Constructs this backend

Parameters

EnvironmentConfiguration $environmentConfiguration
array $options

Configuration options - depends on the actual backend

protected void setProperties(array $properties, bool $throwExceptionIfPropertyNotSettable = true)

No description

Parameters

array $properties
bool $throwExceptionIfPropertyNotSettable

Return Value

void

Exceptions

InvalidArgumentException

protected bool setProperty(string $propertyName, mixed $propertyValue)

No description

Parameters

string $propertyName
mixed $propertyValue

Return Value

bool

void setCache(FrontendInterface $cache)

Initializes the identifier prefix when setting the cache.

Parameters

FrontendInterface $cache

The frontend for this backend

Return Value

void

void setDefaultLifetime(int|string $defaultLifetime)

Sets the default lifetime for this cache backend

Parameters

int|string $defaultLifetime

Default lifetime of this cache backend in seconds. 0 means unlimited lifetime.

Return Value

void

Exceptions

InvalidArgumentException

protected DateTime calculateExpiryTime(int $lifetime = null)

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

Parameters

int $lifetime

The lifetime in seconds

Return Value

DateTime

The expiry time

string getPrefixedIdentifier(string $entryIdentifier)

Returns the internally used, prefixed entry identifier for the given public entry identifier.

While Flow applications will mostly refer to the simple entry identifier, it may be necessary to know the actual identifier used by the cache backend in order to share cache entries with other applications. This method allows for retrieving it.

Parameters

string $entryIdentifier

Return Value

string

mixed requireOnce(string $entryIdentifier)

Loads PHP code from the cache and require_onces it right away.

Parameters

string $entryIdentifier

An identifier which describes the cache entry to load

Return Value

mixed

Potential return value from the include operation

protected void setServers(array $servers)

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

Parameters

array $servers

An array of servers to add.

Return Value

void

Exceptions

Exception

protected void setCompression(bool $useCompression)

Setter for compression flags bit

Parameters

bool $useCompression

Return Value

void

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

Saves data in the cache.

Parameters

string $entryIdentifier

An identifier for this specific cache entry

string $data

The data to be stored

array $tags

Tags to associate with this cache entry. If the backend does not support tags, this option can be ignored.

int $lifetime

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

Return Value

void

Exceptions

Exception
InvalidArgumentException

protected bool setItem(string $key, string $value, int $expiration)

Stores an item on the server

Parameters

string $key
string $value
int $expiration

Return Value

bool

mixed get(string $entryIdentifier)

Loads data from the cache.

Parameters

string $entryIdentifier

An identifier which describes the cache entry to load

Return Value

mixed

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

bool has(string $entryIdentifier)

Checks if a cache entry with the specified identifier exists.

Parameters

string $entryIdentifier

An identifier specifying the cache entry

Return Value

bool

true if such an entry exists, false if not

bool remove(string $entryIdentifier)

Removes all cache entries matching the specified identifier.

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

string $entryIdentifier

Specifies the cache entry to remove

Return Value

bool

true if (at least) an entry could be removed or false if no entry was found

array findIdentifiersByTag(string $tag)

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

Parameters

string $tag

The tag to search for

Return Value

array

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

protected array findTagsByIdentifier(string $identifier)

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

Parameters

string $identifier

Identifier to find tags by

Return Value

array

Array with tags

void flush()

Removes all cache entries of this cache.

Return Value

void

Exceptions

Exception

int flushByTag(string $tag)

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

Parameters

string $tag

The tag the entries must have

Return Value

int

The number of entries which have been affected by this flush

int flushByTags(array $tags)

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

Parameters

array $tags

The tags the entries must have

Return Value

int

The number of entries which have been affected by this flush

protected void addIdentifierToTags(string $entryIdentifier, array $tags)

Associates the identifier with the given tags

Parameters

string $entryIdentifier
array $tags

Return Value

void

protected void removeIdentifierFromAllTags(string $entryIdentifier)

Removes association of the identifier with the given tags

Parameters

string $entryIdentifier

Return Value

void

void collectGarbage()

Does nothing, as memcache/memcached does GC itself

Return Value

void