PHP Class yii\caching\MemCache
MemCache supports both
memcache and
memcached. By setting [[useMemcached]] to be true or false,
one can let MemCache to use either memcached or memcache, respectively.
MemCache can be configured with a list of memcache servers by settings its [[servers]] property.
By default, MemCache assumes there is a memcache server running on localhost at port 11211.
See
Cache for common cache operations that MemCache supports.
Note, there is no security measure to protected data in memcache.
All data in memcache can be accessed by any process running in the system.
To use MemCache as the cache application component, configure the application as follows,
php
[
'components' => [
'cache' => [
'class' => 'yii\caching\MemCache',
'servers' => [
[
'host' => 'server1',
'port' => 11211,
'weight' => 60,
],
[
'host' => 'server2',
'port' => 11211,
'weight' => 40,
],
],
],
],
]
In the above, two memcache servers are used: server1 and server2. You can configure more properties of
each server, such as
persistent,
weight,
timeout. Please see
MemCacheServer for available options.
For more details and usage information on Cache, see the
guide article on caching.
Afficher le fichier
Open project: yiisoft/yii2
Class Usage Examples
Méthodes publiques
Свойство |
Type |
Description |
|
$options |
|
options for Memcached. This property is used only when [[useMemcached]] is true. |
|
$password |
|
memcached sasl password. This property is used only when [[useMemcached]] is true. |
|
$persistentId |
|
an ID that identifies a Memcached instance. This property is used only when [[useMemcached]] is true.
By default the Memcached instances are destroyed at the end of the request. To create an instance that
persists between requests, you may specify a unique ID for the instance. All instances created with the
same ID will share the same connection. |
|
$useMemcached |
|
whether to use memcached or memcache as the underlying caching extension.
If true, memcached will be used.
If false, memcache will be used.
Defaults to false. |
|
$username |
|
memcached sasl username. This property is used only when [[useMemcached]] is true. |
|
Méthodes publiques
Méthode |
Description |
|
getMemcache ( ) : Memcache | Memcached |
Returns the underlying memcache (or memcached) object. |
|
getServers ( ) : MemCacheServer[] |
Returns the memcache or memcached server configurations. |
|
init ( ) |
Initializes this application component. |
|
setServers ( array $config ) |
|
|
Méthodes protégées
Méthode |
Description |
|
addMemcacheServers ( Memcache $cache, MemCacheServer[] $servers ) |
Add servers to the server pool of the cache specified
Used for memcache PECL extension. |
|
addMemcachedServers ( Memcached $cache, MemCacheServer[] $servers ) |
Add servers to the server pool of the cache specified
Used for memcached PECL extension. |
|
addServers ( Memcache | Memcached $cache, MemCacheServer[] $servers ) |
Add servers to the server pool of the cache specified |
|
addValue ( string $key, mixed $value, integer $duration ) : boolean |
Stores a value identified by a key into cache if the cache does not contain this key. |
|
deleteValue ( string $key ) : boolean |
Deletes a value with the specified key from cache
This is the implementation of the method declared in the parent class. |
|
flushValues ( ) : boolean |
Deletes all values from cache. |
|
getValue ( string $key ) : mixed | false |
Retrieves a value from cache with a specified key. |
|
getValues ( array $keys ) : array |
Retrieves multiple values from cache with the specified keys. |
|
setValue ( string $key, mixed $value, integer $duration ) : boolean |
Stores a value identified by a key in cache. |
|
setValues ( array $data, integer $duration ) : array |
Stores multiple key-value pairs in cache. |
|
Method Details
addMemcacheServers()
protected méthode
Add servers to the server pool of the cache specified
Used for memcache PECL extension.
protected addMemcacheServers ( Memcache $cache, MemCacheServer[] $servers ) |
$cache |
Memcache |
|
$servers |
MemCacheServer[] |
|
addMemcachedServers()
protected méthode
Add servers to the server pool of the cache specified
Used for memcached PECL extension.
protected addMemcachedServers ( Memcached $cache, MemCacheServer[] $servers ) |
$cache |
Memcached |
|
$servers |
MemCacheServer[] |
|
addServers()
protected méthode
Add servers to the server pool of the cache specified
protected addServers ( Memcache | Memcached $cache, MemCacheServer[] $servers ) |
$cache |
Memcache | Memcached |
|
$servers |
MemCacheServer[] |
|
addValue()
protected méthode
This is the implementation of the method declared in the parent class.
protected addValue ( string $key, mixed $value, integer $duration ) : boolean |
$key |
string |
the key identifying the value to be cached |
$value |
mixed |
the value to be cached |
$duration |
integer |
the number of seconds in which the cached value will expire. 0 means never expire. |
Résultat |
boolean |
true if the value is successfully stored into cache, false otherwise |
deleteValue()
protected méthode
Deletes a value with the specified key from cache
This is the implementation of the method declared in the parent class.
flushValues()
protected méthode
This is the implementation of the method declared in the parent class.
getMemcache()
public méthode
Returns the underlying memcache (or memcached) object.
public getMemcache ( ) : Memcache | Memcached |
Résultat |
Memcache | Memcached |
the memcache (or memcached) object used by this cache component. |
getServers()
public méthode
Returns the memcache or memcached server configurations.
public getServers ( ) : MemCacheServer[] |
Résultat |
MemCacheServer[] |
list of memcache server configurations. |
getValue()
protected méthode
This is the implementation of the method declared in the parent class.
protected getValue ( string $key ) : mixed | false |
$key |
string |
a unique key identifying the cached value |
Résultat |
mixed | false |
the value stored in cache, false if the value is not in the cache or expired. |
getValues()
protected méthode
Retrieves multiple values from cache with the specified keys.
protected getValues ( array $keys ) : array |
$keys |
array |
a list of keys identifying the cached values |
Résultat |
array |
a list of cached values indexed by the keys |
It creates the memcache instance and adds memcache servers.
setServers()
public méthode
public setServers ( array $config ) |
$config |
array |
list of memcache or memcached server configurations. Each element must be an array
with the following keys: host, port, persistent, weight, timeout, retryInterval, status. |
setValue()
protected méthode
This is the implementation of the method declared in the parent class.
protected setValue ( string $key, mixed $value, integer $duration ) : boolean |
$key |
string |
the key identifying the value to be cached |
$value |
mixed |
the value to be cached. |
$duration |
integer |
the number of seconds in which the cached value will expire. 0 means never expire. |
Résultat |
boolean |
true if the value is successfully stored into cache, false otherwise |
setValues()
protected méthode
Stores multiple key-value pairs in cache.
protected setValues ( array $data, integer $duration ) : array |
$data |
array |
array where key corresponds to cache key while value is the value stored |
$duration |
integer |
the number of seconds in which the cached values will expire. 0 means never expire. |
Résultat |
array |
array of failed keys. Always empty in case of using memcached. |
Property Details
$options public_oe property
options for Memcached. This property is used only when [[useMemcached]] is true.
$password public_oe property
memcached sasl password. This property is used only when [[useMemcached]] is true.
$persistentId public_oe property
an ID that identifies a Memcached instance. This property is used only when [[useMemcached]] is true.
By default the Memcached instances are destroyed at the end of the request. To create an instance that
persists between requests, you may specify a unique ID for the instance. All instances created with the
same ID will share the same connection.
$useMemcached public_oe property
whether to use memcached or memcache as the underlying caching extension.
If true,
memcached will be used.
If false,
memcache will be used.
Defaults to false.
$username public_oe property
memcached sasl username. This property is used only when [[useMemcached]] is true.