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
Table of Contents
Interfaces
- TaggableBackendInterface
- A contract for a cache backend which supports tagging.
Constants
- 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
Properties
- $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)
- $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|null
- Password for authentication
- $persistentConnection : bool
- Persistent connection
- $port : int
- Port of the Redis server, defaults to 6379
- $redis : Redis
- $username : string|null
- Username for authentication
Methods
- __construct() : mixed
- 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() : void
- Removes all cache entries of this cache which are tagged by any of the specified tags.
- get() : mixed
- Loads data from the cache.
- has() : bool
- Checks if a cache entry with the specified identifier exists.
- initializeObject() : void
- remove() : bool
- Removes all cache entries matching the specified identifier.
- set() : void
- Save data in the cache
- setCache() : void
- Sets a reference to the cache frontend which uses this backend
- setDefaultLifetime() : void
- Sets the default lifetime for this cache backend
- getAuthentication() : array<string|int, mixed>|string|null
- Build the authentication value based on the configuration, returning an associative array in case `username` and `password` has been configured, the `password` as string if only password has been configured or `null` to indicate no-authentication configuration, which is also possible to be used with `redis`.
- 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
- setCompression() : void
- setCompressionLevel() : void
- Set data compression level.
- setConnectionTimeout() : void
- Set connection timeout.
- setDatabase() : void
- setHostname() : void
- setKeyPrefix() : void
- setPassword() : void
- Setter for authentication password
- setPersistentConnection() : void
- setPort() : void
- setUsername() : void
Constants
FAKED_UNLIMITED_LIFETIME
Faked unlimited lifetime = 31536000 (1 Year).
protected
mixed
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"
Tags
IDENTIFIER_DATA_PREFIX
Key prefix for identifier->data entries
protected
mixed
IDENTIFIER_DATA_PREFIX
= 'identData:'
IDENTIFIER_TAGS_PREFIX
Key prefix for identifier->tags sets
protected
mixed
IDENTIFIER_TAGS_PREFIX
= 'identTags:'
TAG_IDENTIFIERS_PREFIX
Key prefix for tag->identifiers sets
protected
mixed
TAG_IDENTIFIERS_PREFIX
= 'tagIdents:'
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
$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 authentication
protected
string|null
$password
= null
$persistentConnection
Persistent connection
protected
bool
$persistentConnection
= false
$port
Port of the Redis server, defaults to 6379
protected
int
$port
= 6379
$redis
protected
Redis
$redis
$username
Username for authentication
protected
string|null
$username
= null
Methods
__construct()
public
__construct([array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $options : array<string|int, mixed> = []
-
Configuration options - depends on the actual backend
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
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
-
The tag the entries must have
flushByTags()
Removes all cache entries of this cache which are tagged by any of the specified tags.
public
flushByTags(array<string|int, mixed> $tags) : void
Parameters
- $tags : array<string|int, mixed>
-
List of tags
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
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
-
An identifier specifying the cache entry
Return values
bool —TRUE if such an entry exists, FALSE if not
initializeObject()
public
initializeObject() : void
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
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|null $lifetime = null ]) : void
Scales O(1) with number of cache entries Scales O(n) with number of tags
Parameters
- $entryIdentifier : string
-
An identifier for this specific cache entry
- $data : string
-
The data to be stored
- $tags : array<string|int, mixed> = []
-
Tags to associate with this cache entry. If the backend does not support tags, this option can be ignored.
- $lifetime : int|null = null
-
Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
setCache()
Sets a reference to the cache frontend which uses this backend
public
setCache(FrontendInterface $cache) : void
Parameters
- $cache : FrontendInterface
-
The frontend for this backend
setDefaultLifetime()
Sets the default lifetime for this cache backend
public
setDefaultLifetime(int $defaultLifetime) : void
Parameters
- $defaultLifetime : int
-
Default lifetime of this cache backend in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
Misused for testing purposes.
Tags
getAuthentication()
Build the authentication value based on the configuration, returning an associative array in case `username` and `password` has been configured, the `password` as string if only password has been configured or `null` to indicate no-authentication configuration, which is also possible to be used with `redis`.
protected
getAuthentication() : array<string|int, mixed>|string|null
Return values
array<string|int, mixed>|string|nullgetDataIdentifier()
protected
getDataIdentifier(string $identifier) : string
Parameters
- $identifier : string
Return values
stringgetTagIdentifier()
protected
getTagIdentifier(string $tag) : string
Parameters
- $tag : string
Return values
stringgetTagsIdentifier()
protected
getTagsIdentifier(string $identifier) : string
Parameters
- $identifier : string
Return values
stringremoveIdentifierEntriesAndRelations()
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>
- $tags : array<string|int, mixed>
setCompression()
protected
setCompression(bool $compression) : void
Parameters
- $compression : bool
setCompressionLevel()
Set data compression level.
protected
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
setConnectionTimeout()
Set connection timeout.
protected
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
setDatabase()
protected
setDatabase(int $database) : void
Parameters
- $database : int
setHostname()
protected
setHostname(string $hostname) : void
Parameters
- $hostname : string
setKeyPrefix()
protected
setKeyPrefix(string $keyPrefix) : void
Parameters
- $keyPrefix : string
setPassword()
Setter for authentication password
protected
setPassword(array<string|int, mixed>|string $password) : void
Parameters
- $password : array<string|int, mixed>|string
Tags
setPersistentConnection()
protected
setPersistentConnection(bool $persistentConnection) : void
Parameters
- $persistentConnection : bool
setPort()
protected
setPort(int $port) : void
Parameters
- $port : int
setUsername()
protected
setUsername(string $username) : void
Parameters
- $username : string