PHP Class lithium\storage\cache\adapter\Memcache
The Memcache cache adapter is meant to be used through the
Cache interface,
which abstracts away key generation, adapter instantiation and filter
implementation.
A simple configuration of this adapter can be accomplished in
app/config/bootstrap.php
as follows:
{{{
Cache::config(array(
'cache-config-name' => array(
'adapter' => 'Memcached',
'servers' => array(
array('127.0.0.1', 11211, 100)
)
)
));
}}}
The 'servers' key accepts entries as arrays, where the format is
array(server, port, [weight]),
with the weight being optional.
This Memcache adapter provides basic support for
write,
read,
delete
and
clear cache functionality, as well as allowing the first four
methods to be filtered as per the Lithium filtering system. Additionally,
This adapter defines several methods that are _not_ implemented in other
adapters, and are thus non-portable - see the documentation for
Cache
as to how these methods should be accessed.
This adapter stores two keys for each written value - one which consists
of the data to be cached, and the other being a cache of the expiration time.
Show file
Open project: unionofrad/lithium
Class Usage Examples
Public Properties
Property |
Type |
Description |
|
$connection |
object |
Memcached object instance used by this adapter. |
|
Public Methods
Method |
Description |
|
__call ( string $method, array $params = [] ) : mixed |
Dispatches a not-found method to the connection object. That way, one can
easily use a custom method on the adapter. If you want to know, what methods
are available, have a look at the documentation of memcached. |
|
__construct ( array $config = [] ) : void |
Constructor. Instantiates the Memcached object, adds appropriate servers to the pool,
and configures any optional settings passed (see the _init() method). When adding
servers, the following formats are valid for the 'host' key: |
|
clear ( ) : boolean |
Clears entire cache by flushing it. All cache keys using the
configuration but *without* honoring the scope are removed. |
|
decrement ( string $key, integer $offset = 1 ) : integer | boolean |
Performs an atomic decrement operation on specified numeric cache item. |
|
delete ( array $keys ) : boolean |
Will attempt to remove specified keys from the user space cache. |
|
enabled ( ) : boolean |
Determines if the Memcached extension has been installed. |
|
increment ( string $key, integer $offset = 1 ) : integer | boolean |
Performs an atomic increment operation on specified numeric cache item. |
|
read ( array $keys ) : array |
Read values from the cache. Will attempt to return an array of data
containing key/value pairs of the requested data. |
|
respondsTo ( string $method, boolean $internal = false ) : boolean |
Determines if a given method can be called. |
|
write ( array $keys, string | integer $expiry = null ) : boolean |
Write values to the cache. All items to be cached will receive an
expiration time of $expiry. |
|
Protected Methods
Method |
Description |
|
_formatHostList ( mixed $host ) : array |
Formats standard 'host:port' strings into arrays used by Memcached. |
|
_init ( ) : void |
Handles the actual Memcached connection and server connection
adding for the adapter constructor and sets prefix using the scope
if provided. |
|
Method Details
Cache::adapter('memcache')->methodName($argument);
public __call ( string $method, array $params = [] ) : mixed |
$method |
string |
Name of the method to call. |
$params |
array |
Parameter list to use when calling $method. |
return |
mixed |
Returns the result of the method call. |
__construct()
public method
- '127.0.0.1'
Configure the adapter to connect to one Memcache server on the default port.
- '127.0.0.1:11222'
Configure the adapter to connect to one Memcache server on a custom port.
- array('167.221.1.5:11222' => 200, '167.221.1.6')
Connect to one server on a custom port with a high selection weight, and
a second server on the default port with the default selection weight.
public __construct ( array $config = [] ) : void |
$config |
array |
Configuration for this cache adapter. These settings are queryable
through `Cache::config('name')`. The available options are as follows:
- `'scope'` _string_: Scope which will prefix keys; per default not set.
- `'expiry'` _mixed_: The default expiration time for cache values, if no value
is otherwise set. Can be either a `strtotime()` compatible tring or TTL in
seconds. To indicate items should not expire use `Cache::PERSIST`. Defaults
to `+1 hour`.
- `'host'` _mixed_: Specifies one or more Memcache servers to connect to, with
optional server selection weights. See above for example values. |
return |
void |
|
Handles the actual Memcached connection and server connection
adding for the adapter constructor and sets prefix using the scope
if provided.
Internally keys are not removed but invalidated. Thus this
operation doesn't actually free memory on the instance.
The behavior and result when removing a single key
during this process fails is unknown.
decrement()
public method
Note that, as per the Memcached specification:
"If the item's value is not numeric, it is treated as if the value were 0.
If the operation would decrease the value below 0, the new value will be 0."
Will attempt to remove specified keys from the user space cache.
public delete ( array $keys ) : boolean |
$keys |
array |
Keys to uniquely identify the cached items. |
return |
boolean |
`true` on successful delete, `false` otherwise. |
enabled()
public static method
Determines if the Memcached extension has been installed.
public static enabled ( ) : boolean |
return |
boolean |
Returns `true` if the `Memcached` extension is installed and enabled, `false`
otherwise. |
increment()
public method
Note that, as per the Memcached specification:
"If the item's value is not numeric, it is treated as if the value were 0."
Read values from the cache. Will attempt to return an array of data
containing key/value pairs of the requested data.
public read ( array $keys ) : array |
$keys |
array |
Keys to uniquely identify the cached items. |
return |
array |
Cached values keyed by cache keys on successful read,
keys which could not be read will not be included in
the results array. |
respondsTo()
public method
Determines if a given method can be called.
public respondsTo ( string $method, boolean $internal = false ) : boolean |
$method |
string |
Name of the method. |
$internal |
boolean |
Provide `true` to perform check from inside the
class/object. When `false` checks also for public visibility;
defaults to `false`. |
return |
boolean |
Returns `true` if the method can be called, `false` otherwise. |
Expiration is always based off the current unix time in order to gurantee we never
exceed the TTL limit of 30 days when specifying the TTL directly.
public write ( array $keys, string | integer $expiry = null ) : boolean |
$keys |
array |
Key/value pairs with keys to uniquely identify the to-be-cached item. |
$expiry |
string | integer |
A `strtotime()` compatible cache time or TTL in seconds.
To persist an item use `\lithium\storage\Cache::PERSIST`. |
return |
boolean |
`true` on successful write, `false` otherwise. |
Property Details
$connection public property
Memcached object instance used by this adapter.
public object $connection |
return |
object |
|