class RedisBackend extends AbstractBackend implements TaggableBackendInterface, IterableBackendInterface, FreezableBackendInterface, PhpCapableBackendInterface, WithStatusInterface (View source)

A caching backend which stores cache entries in Redis using the phpredis PHP extension.

Redis is a noSQL database with very good scaling characteristics in proportion to the amount of entries and data size.

Traits

RequireOnceFromValueTrait

Constants

DATETIME_EXPIRYTIME_UNLIMITED

UNLIMITED_LIFETIME

MIN_REDIS_VERSION

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 seperate stored by appliaction context and cache

from  AbstractBackend
protected int $defaultLifetime

Default lifetime of a cache entry in seconds

from  AbstractBackend
protected EnvironmentConfiguration $environmentConfiguration from  AbstractBackend
protected array $_requiredEntryIdentifiers from  RequireOnceFromValueTrait
protected Redis $redis
protected int $entryCursor
protected bool|null $frozen
protected string $hostname
protected int $port
protected int $database
protected string $password
protected int $compressionLevel

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)

Sets a reference to the cache frontend which uses this backend

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
set(string $entryIdentifier, string $data, array $tags = [], int $lifetime = null)

Saves data in the cache.

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.

void
flush()

Removes all cache entries of this cache

void
collectGarbage()

This backend does not need an externally triggered garbage collection

int
flushByTag(string $tag)

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

array
findIdentifiersByTag(string $tag)

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

current()

{@inheritdoc}

next()

{@inheritdoc}

key()

{@inheritdoc}

bool
valid()

{@inheritdoc}

rewind()

{@inheritdoc}

void
freeze()

Freezes this cache backend.

bool
isFrozen()

Tells if this backend is frozen.

void
setHostname(string $hostname)

Sets the hostname or the socket of the Redis server

void
setPort(int|string $port)

Sets the port of the Redis server.

void
setDatabase(int|string $database)

Sets the database that will be used for this backend

void
setPassword(string $password)

No description

void
setCompressionLevel(int|string $compressionLevel)

No description

void
setRedis(Redis $redis = null)

No description

void
getStatus()

Validates that the configured redis backend is accessible and returns some details about its configuration if that's the case

Details

__construct(EnvironmentConfiguration $environmentConfiguration, array $options)

Constructs this backend

Parameters

EnvironmentConfiguration $environmentConfiguration
array $options

Configuration options - depends on the actual backend

Exceptions

Exception

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)

Sets a reference to the cache frontend which uses this backend

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.

Note that, in case of the AbstractBackend, this method is returns just the given entry identifier.

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

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

RuntimeException
Exception

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

Exceptions

RuntimeException

void flush()

Removes all cache entries of this cache

The flush method will use the EVAL command to flush all entries and tags for this cache in an atomic way.

Return Value

void

Exceptions

RuntimeException

void collectGarbage()

This backend does not need an externally triggered garbage collection

Return Value

void

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 or NULL if the number is unknown

Exceptions

RuntimeException

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

current()

{@inheritdoc}

next()

{@inheritdoc}

key()

{@inheritdoc}

bool valid()

{@inheritdoc}

Return Value

bool

rewind()

{@inheritdoc}

void freeze()

Freezes this cache backend.

All data in a frozen backend remains unchanged and methods which try to add or modify data result in an exception thrown. Possible expiry times of individual cache entries are ignored.

A frozen backend can only be thawn by calling the flush() method.

Return Value

void

Exceptions

RuntimeException

bool isFrozen()

Tells if this backend is frozen.

Return Value

bool

void setHostname(string $hostname)

Sets the hostname or the socket of the Redis server

Parameters

string $hostname

Hostname of the Redis server

Return Value

void

void setPort(int|string $port)

Sets the port of the Redis server.

Unused if you want to connect to a socket (i.e. hostname contains a /)

Parameters

int|string $port

Port of the Redis server

Return Value

void

void setDatabase(int|string $database)

Sets the database that will be used for this backend

Parameters

int|string $database

Database that will be used

Return Value

void

void setPassword(string $password)

No description

Parameters

string $password

Return Value

void

void setCompressionLevel(int|string $compressionLevel)

No description

Parameters

int|string $compressionLevel

Return Value

void

void setRedis(Redis $redis = null)

No description

Parameters

Redis $redis

Return Value

void

protected void verifyRedisVersionIsSupported()

No description

Return Value

void

Exceptions

Exception

Result getStatus()

Validates that the configured redis backend is accessible and returns some details about its configuration if that's the case

Return Value

Result