Class \Prado\Caching\TRedisCache
TRedisCache implements a cache application module based on redis key-value store.
TRedisCache can be configured with the \Prado\Caching\setHost and \Prado\Caching\setPort properties, which specify the host and port of the redis server to be used. By default, they take the value 'localhost' and 6379, respectively.
It is also possible to use a unix socket for connection, it can be set using setSocket. Be sure that the socket is readable/writeable by the webserver/php user. By default, this value is left empty. If both, server/port and socket are set, the latter takes precedence.
Use the \Prado\Caching\setIndex property to change the database to the given database index. Defaults to 0.
The following basic cache operations are implemented:
- get : retrieve the value with a key (if any) from cache
- set : store the value with a key into cache
- add : store the value only if cache does not have this key
- delete : delete the value with the specified key from cache
- flush : delete all values from cache
Each value is associated with an expiration time. The get operation ensures that any expired value will not be returned. The expiration time can be specified by the number of seconds. A expiration time 0 represents never expire.
By definition, cache does not ensure the existence of a value even if it never expires. Cache is not meant to be an persistent storage.
Also note, there is no security measure to protected data in redis cache. All data in redis cache can be accessed by any process running in the system.
To use this module, the php-redis extension must be loaded.
Some usage examples of TRedisCache are as follows,
$cache=new TRedisCache; // TRedisache may also be loaded as a Prado application module
$cache->init(null);
$cache->add('object',$object);
$object2=$cache->get('object');
If loaded as module, TRedisCache will register itself with TApplication as the default cache module. It can be accessed via TApplication::getCache().
TRedisCache may be configured in application configuration file as follows
<module id="cache" class="Prado\Caching\TRedisCache" Host="localhost" Port="6379" />
or
<module id="cache" class="Prado\Caching\TRedisCache" Socket="var/run/redis/redis.sock" Index="2" />
where \Prado\Caching\setHost and \Prado\Caching\setPort or \Prado\Caching\setSocket are configurable properties of TRedisCache.
Class hierarchy
- \Prado\Caching\TRedisCache
- \Prado\Caching\TCache implements ICache, ArrayAccess
- \Prado\TModule implements IModule
- \Prado\TApplicationComponent
- \Prado\TComponent
Author: LANDWEHR Computer und Software GmbH
Since: 4.0
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
init(TXmlElement $config) : mixed
Initializes this module.
This method is required by the IModule interface. It creates a Redis instance and connects to the redis server. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
protected
|
addValue(string $key, string $value, int $expire) : bool
Stores a value identified by a key into cache if the cache does not contain this key.
This is the implementation of the method declared in the parent class. |
protected
|
deleteValue(string $key) : bool
Deletes a value with the specified key from cache
This is the implementation of the method declared in the parent class.
|
protected
|
getValue(string $key) : false|string
Retrieves a value from cache with a specified key.
This is the implementation of the method declared in the parent class. |
protected
|
setValue(string $key, string $value, int $expire) : bool
Stores a value identified by a key in cache.
This is the implementation of the method declared in the parent class. |
\Prado\TApplicationComponent::FX_CACHE_FILE, \Prado\TComponent::GLOBAL_RAISE_EVENT_LISTENER |