RedisBackend extends AbstractBackend implements TaggableBackendInterface

A caching backend which stores cache entries by using Redis with phpredis PHP module. Redis is a noSQL database with very good scaling characteristics in proportion to the amount of entries and data size.

Tags
see
https://redis.io/
see
https://github.com/phpredis/phpredis

Table of Contents

Interfaces

TaggableBackendInterface
A contract for a cache backend which supports tagging.

Constants

DATETIME_EXPIRYTIME_UNLIMITED  = '9999-12-31T23:59:59+0000'
FAKED_UNLIMITED_LIFETIME  = 31536000
Faked unlimited lifetime = 31536000 (1 Year).
IDENTIFIER_DATA_PREFIX  = 'identData:'
Key prefix for identifier->data entries
IDENTIFIER_TAGS_PREFIX  = 'identTags:'
Key prefix for identifier->tags sets
TAG_IDENTIFIERS_PREFIX  = 'tagIdents:'
Key prefix for tag->identifiers sets
UNLIMITED_LIFETIME  = 0

Properties

$cache  : FrontendInterface
Reference to the cache which uses this backend
$cacheIdentifier  : string
$compression  : bool
Indicates whether data is compressed or not (requires php zlib)
$compressionLevel  : int
-1 to 9, indicates zlib compression level: -1 = default level 6, 0 = no compression, 9 maximum compression
$connected  : bool
Indicates whether the server is connected
$connectionTimeout  : int
limit in seconds (default is 0 meaning unlimited)
$context  : string
The current application context
$database  : int
Number of selected database, defaults to 0
$defaultLifetime  : int
Default lifetime of a cache entry in seconds
$hostname  : string
Hostname / IP of the Redis server, defaults to 127.0.0.1.
$keyPrefix  : string
Used as prefix for all Redis keys/identifiers
$password  : string
Password for redis authentication
$persistentConnection  : bool
Persistent connection
$port  : int
Port of the Redis server, defaults to 6379
$redis  : Redis

Methods

__construct()  : mixed
Construct this backend
collectGarbage()  : void
With the current internal structure, only the identifier to data entries have a redis internal lifetime. If an entry expires, attached identifier to tags and tag to identifiers entries will be left over.
findIdentifiersByTag()  : array<string|int, mixed>
Finds and returns all cache entry identifiers which are tagged by the specified tag.
flush()  : void
Removes all cache entries of this cache.
flushByTag()  : void
Removes all cache entries of this cache which are tagged with 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()  : void
Initializes the redis backend
remove()  : bool
Removes all cache entries matching the specified identifier.
set()  : void
Save data in the cache
setCache()  : mixed
Sets a reference to the cache frontend which uses this backend
setCompression()  : void
Enable data compression
setCompressionLevel()  : void
Set data compression level.
setConnectionTimeout()  : void
Set connection timeout.
setDatabase()  : void
Setter for database number
setDefaultLifetime()  : mixed
Sets the default lifetime for this cache backend
setHostname()  : void
Setter for server hostname
setPassword()  : void
Setter for authentication password
setPersistentConnection()  : void
Setter for persistent connection
setPort()  : void
Setter for server port
calculateExpiryTime()  : DateTime
Calculates the expiry time by the given lifetime. If no lifetime is specified, the default lifetime is used.
canBeUsedInStringContext()  : bool
Helper method to catch invalid identifiers and tags
getDataIdentifier()  : string
getTagIdentifier()  : string
getTagsIdentifier()  : string
removeIdentifierEntriesAndRelations()  : void
Helper method for flushByTag() Gets list of identifiers and tags and removes all relations of those tags
setKeyPrefix()  : void

Constants

DATETIME_EXPIRYTIME_UNLIMITED

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

FAKED_UNLIMITED_LIFETIME

Faked unlimited lifetime = 31536000 (1 Year).

public int FAKED_UNLIMITED_LIFETIME = 31536000

In redis an entry does not have a lifetime by default (it's not "volatile"). Entries can be made volatile either with EXPIRE after it has been SET, or with SETEX, which is a combined SET and EXPIRE command. But an entry can not be made "unvolatile" again. To set a volatile entry to not volatile again, it must be DELeted and SET without a following EXPIRE. To save these additional calls on every set(), we just make every entry volatile and treat a high number as "unlimited"

Faked unlimited lifetime

Tags
see
https://redis.io/commands/expire

IDENTIFIER_DATA_PREFIX

Key prefix for identifier->data entries

public string IDENTIFIER_DATA_PREFIX = 'identData:'

IDENTIFIER_TAGS_PREFIX

Key prefix for identifier->tags sets

public string IDENTIFIER_TAGS_PREFIX = 'identTags:'

TAG_IDENTIFIERS_PREFIX

Key prefix for tag->identifiers sets

public string TAG_IDENTIFIERS_PREFIX = 'tagIdents:'

UNLIMITED_LIFETIME

public mixed UNLIMITED_LIFETIME = 0

Properties

$cacheIdentifier

protected string $cacheIdentifier

$compression

Indicates whether data is compressed or not (requires php zlib)

protected bool $compression = false

$compressionLevel

-1 to 9, indicates zlib compression level: -1 = default level 6, 0 = no compression, 9 maximum compression

protected int $compressionLevel = -1

$connected

Indicates whether the server is connected

protected bool $connected = false

$connectionTimeout

limit in seconds (default is 0 meaning unlimited)

protected int $connectionTimeout = 0

$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.

$database

Number of selected database, defaults to 0

protected int $database = 0

$defaultLifetime

Default lifetime of a cache entry in seconds

protected int $defaultLifetime = 3600

$hostname

Hostname / IP of the Redis server, defaults to 127.0.0.1.

protected string $hostname = '127.0.0.1'

$keyPrefix

Used as prefix for all Redis keys/identifiers

protected string $keyPrefix = ''

$password

Password for redis authentication

protected string $password = ''

$persistentConnection

Persistent connection

protected bool $persistentConnection = false

$port

Port of the Redis server, defaults to 6379

protected int $port = 6379

Methods

__construct()

Construct this backend

public __construct(string $context[, array<string|int, mixed> $options = [] ]) : mixed
Parameters
$context : string

Unused, for backward compatibility only

$options : array<string|int, mixed> = []

Configuration options

Tags
throws
Exception

if php redis module is not loaded

collectGarbage()

With the current internal structure, only the identifier to data entries have a redis internal lifetime. If an entry expires, attached identifier to tags and tag to identifiers entries will be left over.

public collectGarbage() : void

This method finds those entries and cleans them up.

Scales O(n*m) with number of cache entries (n) and number of tags (m)

findIdentifiersByTag()

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

public findIdentifiersByTag(string $tag) : array<string|int, mixed>

Scales O(1) with number of cache entries Scales O(n) with number of tag entries

Parameters
$tag : string

The tag to search for

Tags
throws
InvalidArgumentException

if tag is not a string

Return values
array<string|int, mixed>

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

flush()

Removes all cache entries of this cache.

public flush() : void

flushByTag()

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

public flushByTag(string $tag) : void

Scales O(1) with number of cache entries Scales O(n^2) with number of tag entries

Parameters
$tag : string

Tag the entries must have

Tags
throws
InvalidArgumentException

if identifier is not a string

flushByTags()

Backwards compatibility safeguard since re-introducing flushByTags as API.

public flushByTags(array<string|int, 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 : array<string|int, string>

get()

Loads data from the cache.

public get(string $entryIdentifier) : mixed

Scales O(1) with number of cache entries

Parameters
$entryIdentifier : string

An identifier which describes the cache entry to load

Tags
throws
InvalidArgumentException

if identifier is not a string

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

Scales O(1) with number of cache entries

Parameters
$entryIdentifier : string

Identifier specifying the cache entry

Tags
throws
InvalidArgumentException

if identifier is not a string

Return values
bool

TRUE if such an entry exists, FALSE if not

initializeObject()

Initializes the redis backend

public initializeObject() : void
Tags
throws
Exception

if access to redis with password is denied or if database selection fails

remove()

Removes all cache entries matching the specified identifier.

public remove(string $entryIdentifier) : bool

Scales O(1) with number of cache entries Scales O(n) with number of tags

Parameters
$entryIdentifier : string

Specifies the cache entry to remove

Tags
throws
InvalidArgumentException

if identifier is not a string

Return values
bool

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

set()

Save data in the cache

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

Scales O(1) with number of cache entries Scales O(n) with number of tags

Parameters
$entryIdentifier : string

Identifier for this specific cache entry

$data : string

Data to be stored

$tags : array<string|int, mixed> = []

Tags to associate with this cache entry

$lifetime : int = null

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

Tags
throws
InvalidArgumentException

if identifier is not valid

throws
InvalidDataException

if data is not a string

setCompression()

Enable data compression

public setCompression(bool $compression) : void
Parameters
$compression : bool

TRUE to enable compression

Tags
throws
InvalidArgumentException

if compression parameter is not of type boolean

setCompressionLevel()

Set data compression level.

public setCompressionLevel(int $compressionLevel) : void

If compression is enabled and this is not set, gzcompress default level will be used.

Parameters
$compressionLevel : int

-1 to 9: Compression level

Tags
throws
InvalidArgumentException

if compressionLevel parameter is not within allowed bounds

setConnectionTimeout()

Set connection timeout.

public setConnectionTimeout(int $connectionTimeout) : void

This value in seconds is used as a maximum number of seconds to wait if a connection can be established.

Parameters
$connectionTimeout : int

limit in seconds, a value greater or equal than 0

Tags
throws
InvalidArgumentException

if compressionLevel parameter is not within allowed bounds

setDatabase()

Setter for database number

public setDatabase(int $database) : void
Parameters
$database : int

Database

Tags
throws
InvalidArgumentException

if database number is not valid

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

setHostname()

Setter for server hostname

public setHostname(string $hostname) : void
Parameters
$hostname : string

Hostname

setPassword()

Setter for authentication password

public setPassword(string $password) : void
Parameters
$password : string

Password

setPersistentConnection()

Setter for persistent connection

public setPersistentConnection(bool $persistentConnection) : void
Parameters
$persistentConnection : bool

setPort()

Setter for server port

public setPort(int $port) : void
Parameters
$port : int

Port

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

canBeUsedInStringContext()

Helper method to catch invalid identifiers and tags

protected canBeUsedInStringContext(mixed $variable) : bool
Parameters
$variable : mixed

Variable to be checked

Return values
bool

getDataIdentifier()

protected getDataIdentifier(string $identifier) : string
Parameters
$identifier : string
Return values
string

getTagIdentifier()

protected getTagIdentifier(string $tag) : string
Parameters
$tag : string
Return values
string

getTagsIdentifier()

protected getTagsIdentifier(string $identifier) : string
Parameters
$identifier : string
Return values
string

removeIdentifierEntriesAndRelations()

Helper method for flushByTag() Gets list of identifiers and tags and removes all relations of those tags

protected removeIdentifierEntriesAndRelations(array<string|int, mixed> $identifiers, array<string|int, mixed> $tags) : void

Scales O(1) with number of cache entries Scales O(n^2) with number of tags

Parameters
$identifiers : array<string|int, mixed>

List of identifiers to remove

$tags : array<string|int, mixed>

List of tags to be handled

setKeyPrefix()

protected setKeyPrefix(string $keyPrefix) : void
Parameters
$keyPrefix : string

        
On this page

Search results