Class yii\redis\Cache
Inheritance | yii\redis\Cache » yii\caching\Cache |
---|---|
Available since version | 2.0 |
Redis Cache implements a cache application component based on redis key-value store.
Redis Cache requires redis version 2.6.12 or higher to work properly.
It needs to be configured with a redis yii\redis\Connection that is also configured as an application component.
By default it will use the redis
application component.
See yii\redis\Cache manual for common cache operations that redis Cache supports.
Unlike the yii\redis\Cache, redis Cache allows the expire parameter of \yii\redis\set, \yii\redis\add, \yii\redis\mset and \yii\redis\madd to be a floating point number, so you may specify the time in milliseconds (e.g. 0.1 will be 100 milliseconds).
To use redis Cache as the cache application component, configure the application as follows,
[
'components' => [
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
]
],
],
]
Or if you have configured the redis yii\redis\Connection as an application component, the following is sufficient:
[
'components' => [
'cache' => [
'class' => 'yii\redis\Cache',
// 'redis' => 'redis' // id of the connection application component
],
],
]
If you have multiple redis replicas (e.g. AWS ElasticCache Redis) you can configure the cache to send read operations to the replicas. If no replicas are configured, all operations will be performed on the master connection configured via the $redis property.
[
'components' => [
'cache' => [
'class' => 'yii\redis\Cache',
'enableReplicas' => true,
'replicas' => [
// config for replica redis connections, (default class will be yii\redis\Connection if not provided)
// you can optionally put in master as hostname as well, as all GET operation will use replicas
'redis',//id of Redis [[Connection]] Component
['hostname' => 'redis-slave-002.xyz.0001.apse1.cache.amazonaws.com'],
['hostname' => 'redis-slave-003.xyz.0001.apse1.cache.amazonaws.com'],
],
],
],
]
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$enableReplicas | boolean | Whether to enable read / get from redis replicas. | yii\redis\Cache |
$redis | yii\redis\Connection|string|array | The Redis yii\redis\Connection object or the application component ID of the Redis yii\redis\Connection. | yii\redis\Cache |
$replicas | array | The Redis yii\redis\Connection configurations for redis replicas. | yii\redis\Cache |
Public Methods
Method | Description | Defined By |
---|---|---|
exists() | Checks whether a specified key exists in the cache. | yii\redis\Cache |
init() | Initializes the redis Cache component. | yii\redis\Cache |
Protected Methods
Method | Description | Defined By |
---|---|---|
addValue() | yii\redis\Cache | |
deleteValue() | yii\redis\Cache | |
flushValues() | yii\redis\Cache | |
getReplica() | It will return the current Replica Redis yii\redis\Connection, and fall back to default $redis yii\redis\Connection defined in this instance. Only used in getValue() and getValues(). | yii\redis\Cache |
getValue() | yii\redis\Cache | |
getValues() | yii\redis\Cache | |
setValue() | yii\redis\Cache | |
setValues() | yii\redis\Cache |
Property Details
Whether to enable read / get from redis replicas.
See also $replicas.
The Redis yii\redis\Connection object or the application component ID of the Redis yii\redis\Connection. This can also be an array that is used to create a redis yii\redis\Connection instance in case you do not want do configure redis connection as an application component. After the Cache object is created, if you want to change this property, you should only assign it with a Redis yii\redis\Connection object.
The Redis yii\redis\Connection configurations for redis replicas. Each entry is a class configuration, which will be used to instantiate a replica connection. The default class is yii\redis\Connection. You should at least provide a hostname.
Configuration example:
'replicas' => [
'redis',
['hostname' => 'redis-slave-002.xyz.0001.apse1.cache.amazonaws.com'],
['hostname' => 'redis-slave-003.xyz.0001.apse1.cache.amazonaws.com'],
],
See also $enableReplicas.
Method Details
protected void addValue ( $key, $value, $expire ) | ||
$key | ||
$value | ||
$expire |
protected void deleteValue ( $key ) | ||
$key |
Checks whether a specified key exists in the cache.
This can be faster than getting the value from the cache if the data is big. Note that this method does not check whether the dependency associated with the cached data, if there is any, has changed. So a call to \yii\redis\get may return false while exists returns true.
public boolean exists ( $key ) | ||
$key | mixed | A key identifying the cached value. This can be a simple string or a complex data structure consisting of factors representing the key. |
return | boolean | True if a value exists in cache, false if the value is not in the cache or expired. |
---|
protected void flushValues ( ) |
It will return the current Replica Redis yii\redis\Connection, and fall back to default $redis yii\redis\Connection defined in this instance. Only used in getValue() and getValues().
protected array|string|yii\redis\Connection getReplica ( ) | ||
throws | \yii\base\InvalidConfigException |
---|
protected void getValue ( $key ) | ||
$key |
protected void getValues ( $keys ) | ||
$keys |
Initializes the redis Cache component.
This method will initialize the $redis property to make sure it refers to a valid redis connection.
public void init ( ) | ||
throws | \yii\base\InvalidConfigException | if $redis is invalid. |
---|
protected void setValue ( $key, $value, $expire ) | ||
$key | ||
$value | ||
$expire |
protected void setValues ( $data, $expire ) | ||
$data | ||
$expire |