PHP Класс 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.
Показать файл
Открыть проект
Примеры использования класса
Открытые свойства
Свойство |
Тип |
Описание |
|
$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. |
|
Открытые методы
Метод |
Описание |
|
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 ) |
|
|
Защищенные методы
Метод |
Описание |
|
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. |
|
Описание методов
addMemcacheServers()
защищенный Метод
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()
защищенный Метод
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()
защищенный Метод
Add servers to the server pool of the cache specified
protected addServers ( Memcache | Memcached $cache, MemCacheServer[] $servers ) |
$cache |
Memcache | Memcached |
|
$servers |
MemCacheServer[] |
|
addValue()
защищенный Метод
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. |
Результат |
boolean |
true if the value is successfully stored into cache, false otherwise |
deleteValue()
защищенный Метод
Deletes a value with the specified key from cache
This is the implementation of the method declared in the parent class.
flushValues()
защищенный Метод
This is the implementation of the method declared in the parent class.
getMemcache()
публичный Метод
Returns the underlying memcache (or memcached) object.
public getMemcache ( ) : Memcache | Memcached |
Результат |
Memcache | Memcached |
the memcache (or memcached) object used by this cache component. |
getServers()
публичный Метод
Returns the memcache or memcached server configurations.
public getServers ( ) : MemCacheServer[] |
Результат |
MemCacheServer[] |
list of memcache server configurations. |
getValue()
защищенный Метод
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 |
Результат |
mixed | false |
the value stored in cache, false if the value is not in the cache or expired. |
getValues()
защищенный Метод
Retrieves multiple values from cache with the specified keys.
protected getValues ( array $keys ) : array |
$keys |
array |
a list of keys identifying the cached values |
Результат |
array |
a list of cached values indexed by the keys |
It creates the memcache instance and adds memcache servers.
setServers()
публичный Метод
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()
защищенный Метод
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. |
Результат |
boolean |
true if the value is successfully stored into cache, false otherwise |
setValues()
защищенный Метод
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. |
Результат |
array |
array of failed keys. Always empty in case of using memcached. |
Описание свойств
$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.