PHP Class lithium\storage\Cache
The Cache layer of Lithium inherits from the common
Adaptable class, which provides the generic
configuration setting & retrieval logic, as well as the logic required to locate & instantiate
the proper adapter class.
In most cases, you will configure various named cache configurations in your bootstrap process,
which will then be available to you in all other parts of your application.
A simple example configuration:
{{{Cache::config(array(
'local' => array('adapter' => 'Apc'),
'distributed' => array(
'adapter' => 'Memcached',
'host' => '127.0.0.1:11211',
),
'default' => array('adapter' => 'File')
));}}}
Each adapter provides a consistent interface for the basic cache operations of
write,
read,
delete and
clear, which can be used interchangeably between all adapters. Some adapters
may provide additional methods that are not consistently available across other adapters.
To make use of these, it is always possible to call:
{{{Cache::adapter('named-configuration')->methodName($argument);}}}
This allows a very wide range of flexibility, at the cost of portability.
Some cache adapters (e.g.
File) do _not_ provide the functionality for increment/decrement.
Additionally, some cache adapters support multi-key operations for
write,
read and
delete
— please see the individual documentation for cache adapters and the operations that they support.
Datei anzeigen
Open project: unionofrad/lithium
Class Usage Examples
Protected Properties
Property |
Type |
Description |
|
$_adapters |
|
Libraries::locate() compatible path to adapters for this class. |
|
$_configurations |
array |
Stores configurations for cache adapters. |
|
$_strategies |
|
Libraries::locate() compatible path to strategies for this class. |
|
Public Methods
Method |
Description |
|
clean ( string $name ) : boolean |
Perform garbage collection on specified cache configuration. All invalidated cache
keys - *without* honoring a configured scope - from the specified configuration are
removed. |
|
clear ( string $name ) : boolean |
Clears entire cache by flushing it. All cache keys - *without* honoring
a configured scope - from the specified configuration are removed. |
|
decrement ( string $name, string $key, integer $offset = 1, array $options = [] ) : integer | boolean |
Performs a decrement operation on specified numeric cache item
from the given cache configuration. |
|
delete ( string $name, mixed $key, array $options = [] ) : boolean |
Deletes using the specified cache configuration. |
|
increment ( string $name, string $key, integer $offset = 1, array $options = [] ) : integer | boolean |
Performs a increment operation on specified numeric cache item
from the given cache configuration. |
|
key ( mixed $key, array $data = [] ) : string |
Generates the cache key. |
|
read ( string $name, mixed $key, array $options = [] ) : mixed |
Reads from the specified cache configuration. |
|
write ( string $name, mixed $key, mixed $data = null, string | integer $expiry = null, array $options = [] ) : boolean |
Writes to the specified cache configuration. |
|
Method Details
clean()
public static method
Perform garbage collection on specified cache configuration. All invalidated cache
keys - *without* honoring a configured scope - from the specified configuration are
removed.
public static clean ( string $name ) : boolean |
$name |
string |
The cache configuration to be cleaned. |
return |
boolean |
`true` on successful cleaning, `false` if failed partially or entirely. |
clear()
public static method
Clears entire cache by flushing it. All cache keys - *without* honoring
a configured scope - from the specified configuration are removed.
public static clear ( string $name ) : boolean |
$name |
string |
The cache configuration to be cleared. |
return |
boolean |
`true` on successful clearing, `false` if failed partially or entirely. |
decrement()
public static method
Performs a decrement operation on specified numeric cache item
from the given cache configuration.
public static decrement ( string $name, string $key, integer $offset = 1, array $options = [] ) : integer | boolean |
$name |
string |
Name of the cache configuration to use. |
$key |
string |
Key of numeric cache item to decrement |
$offset |
integer |
Offset to decrement - defaults to 1. |
$options |
array |
Options for this method.
- `'conditions'`: A function or item that must return or evaluate to
`true` in order to continue operation. |
return |
integer | boolean |
Item's new value on successful decrement, false otherwise. |
delete()
public static method
Can handle single- and multi-key deletes.
public static delete ( string $name, mixed $key, array $options = [] ) : boolean |
$name |
string |
The cache configuration to delete from. |
$key |
mixed |
Key to be deleted or an array of keys to delete. |
$options |
array |
Options for the method and strategies.
- `'conditions'` _mixed_: A function or item that must return or
evaluate to `true` in order to continue write operation. |
return |
boolean |
`true` on successful cache delete, `false` otherwise. When deleting
multiple items and an error occurs deleting any of the items the
whole operation fails and this method will return `false`. |
increment()
public static method
Performs a increment operation on specified numeric cache item
from the given cache configuration.
public static increment ( string $name, string $key, integer $offset = 1, array $options = [] ) : integer | boolean |
$name |
string |
Name of the cache configuration to use. |
$key |
string |
Key of numeric cache item to increment |
$offset |
integer |
Offset to increment - defaults to 1. |
$options |
array |
Options for this method.
- `'conditions'`: A function or item that must return or evaluate to
`true` in order to continue operation. |
return |
integer | boolean |
Item's new value on successful increment, false otherwise. |
key()
public static method
public static key ( mixed $key, array $data = [] ) : string |
$key |
mixed |
A string (or lambda/closure that evaluates to a string)
that will be used as the cache key. |
$data |
array |
If a lambda/closure is used as a key and requires arguments,
pass them in here. |
return |
string |
The generated cache key. |
read()
public static method
Can handle single- and multi-key reads.
Read-through caching can be used by passing expiry and the to-be-cached value
in the write option. Following three ways to achieve this.
Cache::read('default', 'foo', array(
'write' => array('+5 days' => 'bar')
)); // returns 'bar'
Cache::read('default', 'foo', array(
'write' => array('+5 days' => function() { return 'bar'; })
));
Cache::read('default', 'foo', array(
'write' => function() { return array('+5 days' => 'bar'); }
));
public static read ( string $name, mixed $key, array $options = [] ) : mixed |
$name |
string |
Configuration to be used for reading. |
$key |
mixed |
Key to uniquely identify the cache entry or an array of keys
for multikey-reads. |
$options |
array |
Options for the method and strategies.
- `'write'`: Allows for read-through caching see description for usage.
- `'strategies'` _boolean_: Indicates if strategies should be used,
defaults to `true`.
- `'conditions'` _mixed_: A function or item that must return or
evaluate to `true` in order to continue write operation. |
return |
mixed |
For single-key reads will return the result if the cache
key has been found otherwise returns `null`. When reading
multiple keys a results array is returned mapping keys to
retrieved values. Keys where the value couldn't successfully
been read will not be contained in the results array. |
write()
public static method
Can handle single- and multi-key writes.
This method has two valid syntaxes depending on if you're storing
data using a single key or multiple keys as outlined below.
To write data to a single-key use the following syntax.
Cache::write('default', 'foo', 'bar', '+1 minute');
For multi-key writes the $data parameter's role becomes
the one of the $expiry parameter.
Cache::write('default', array('foo' => 'bar', ... ), '+1 minute');
These two calls are synonymical and demonstrate the two
possible ways to specify the expiration time.
Cache::write('default', 'foo', 'bar', '+1 minute');
Cache::write('default', 'foo', 'bar', 60);
public static write ( string $name, mixed $key, mixed $data = null, string | integer $expiry = null, array $options = [] ) : boolean |
$name |
string |
Configuration to be used for writing. |
$key |
mixed |
Key to uniquely identify the cache entry or an array of key/value pairs
for multi-key writes mapping cache keys to the data to be cached. |
$data |
mixed |
Data to be cached. |
$expiry |
string | integer |
A `strtotime()` compatible cache time. Alternatively an integer
denoting the seconds until the item expires (TTL). If no expiry time is
set, then the default cache expiration time set with the cache adapter
configuration will be used. To persist an item use `Cache::PERSIST`. |
$options |
array |
Options for the method and strategies.
- `'strategies'` _boolean_: Indicates if strategies should be used,
defaults to `true`.
- `'conditions'` _mixed_: A function or item that must return or
evaluate to `true` in order to continue write operation. |
return |
boolean |
`true` on successful cache write, `false` otherwise. When writing
multiple items and an error occurs writing any of the items the
whole operation fails and this method will return `false`. |
Property Details
$_adapters protected_oe static_oe property
Libraries::locate() compatible path to adapters for this class.
protected static $_adapters |
$_configurations protected_oe static_oe property
Stores configurations for cache adapters.
protected static array $_configurations |
return |
array |
|
$_strategies protected_oe static_oe property
Libraries::locate() compatible path to strategies for this class.
protected static $_strategies |