Cache provides a consistent interface to Caching in your application. It allows you to use several different Cache engines, without coupling your application to a specific implementation. It also allows you to change out cache storage or configuration without effecting the rest of your application.
You can configure Cache engines in your application's bootstrap.php
file. A sample configuration would be
Cache::config('shared', array( 'engine' => 'Apc', 'prefix' => 'my_app_' ));
This would configure an APC cache engine to the 'shared' alias. You could then read and write to that cache alias by using it for the $config
parameter in the various Cache methods. In general all Cache operations are supported by all cache engines. However, Cache::increment() and Cache::decrement() are not supported by File caching.
array
Cache configuration stack Keeps the permanent/default settings for each cache engine. These settings are used to reset the engines after temporary modification.
array
array
array
Set the cache configuration to use. config() can both create new configurations, return the settings for already configured configurations.
Drops a cache engine. Deletes the cache configuration information If the deleted configuration is the last configuration using a certain engine, the Engine instance is also unset.
Temporarily change the settings on a cache config. The settings will persist for the next write operation (write, decrement, increment, clear). Any reads that are done before the write, will use the modified settings. If $settings
is empty, the settings will be reset to the original configuration.
_buildEngine( string $name )
Finds and builds the instance of the required engine class.
$name
CacheException
add( string $key , mixed $value , string $config 'default' )
Write data for key into a cache engine if it doesn't exist already.
Writing to the active cache config:
Cache::add('cached_data', $data);
Writing to a specific cache config:
Cache::add('cached_data', $data, 'long_term');
$key
$value
$config
optional 'default' True if the data was successfully cached, false on failure. Or if the key existed already.
clear( boolean $check false , string $config 'default' )
Delete all keys from the cache.
$check
optional false $config
optional 'default' clearGroup( string $group , string $config 'default' )
Delete all keys from the cache belonging to the same group.
$group
$config
optional 'default' config( string $name null , array $settings array() )
Set the cache configuration to use. config() can both create new configurations, return the settings for already configured configurations.
To create a new configuration, or to modify an existing configuration permanently:
Cache::config('my_config', array('engine' => 'File', 'path' => TMP));
If you need to modify a configuration temporarily, use Cache::set(). To get the settings for a configuration:
Cache::config('default');
There are 5 built-in caching engines:
FileEngine
- Uses simple files to store content. Poor performance, but good for storing large objects, or things that are not IO sensitive.ApcEngine
- Uses the APC object cache, one of the fastest caching engines.MemcacheEngine
- Uses the PECL::Memcache extension and Memcached for storage. Fast reads/writes, and benefits from memcache being distributed.XcacheEngine
- Uses the Xcache extension, an alternative to APC.WincacheEngine
- Uses Windows Cache Extension for PHP. Supports wincache 1.1.0 and higher.The following keys are used in core cache engines:
duration
Specify how long items in this cache configuration last.groups
List of groups or 'tags' associated to every key stored in this config. handy for deleting a complete group from cache.prefix
Prefix appended to all entries. Good for when you need to share a keyspace with either another cache config or another application.probability
Probability of hitting a cache gc cleanup. Setting to 0 will disable cache::gc from ever being called automatically.compress
Used by memcache. Enables memcache's compressed format.serialize
Used by FileCache. Should cache objects be serialized first.path
Used by FileCache. Path to where cachefiles should be saved.lock
Used by FileCache. Should files be locked before writing to them?user
Used by Xcache. Username for XCachepassword
Used by Xcache/Redis. Password for XCache/Redis$name
optional null $settings
optional array() CacheException
configured( )
Returns an array containing the currently configured Cache settings.
decrement( string $key , integer $offset 1 , string $config 'default' )
Decrement a number under the key and return decremented value.
$key
$offset
optional 1 $config
optional 'default' new value, or false if the data doesn't exist, is not integer, or if there was an error fetching it
delete( string $key , string $config 'default' )
Delete a key from the cache.
Deleting from the active cache configuration.
Cache::delete('my_data');
Deleting from a specific cache configuration.
Cache::delete('my_data', 'long_term');
$key
$config
optional 'default' drop( string $name )
Drops a cache engine. Deletes the cache configuration information If the deleted configuration is the last configuration using a certain engine, the Engine instance is also unset.
$name
gc( string $config 'default' , integer $expires null )
Garbage collection
Permanently remove all expired and deleted data
$config
optional 'default' $expires
optional null groupConfigs( string $group null )
Retrieve group names to config mapping.
Cache::config('daily', array( 'duration' => '1 day', 'groups' => array('posts') )); Cache::config('weekly', array( 'duration' => '1 week', 'groups' => array('posts', 'archive') )); $configs = Cache::groupConfigs('posts');
$config will equal to array('posts' => array('daily', 'weekly'))
$group
optional null CacheException
increment( string $key , integer $offset 1 , string $config 'default' )
Increment a number under the key and return incremented value.
$key
$offset
optional 1 $config
optional 'default' new value, or false if the data doesn't exist, is not integer, or if there was an error fetching it.
isInitialized( string $config 'default' )
Check if Cache has initialized a working config for the given name.
$config
optional 'default' read( string $key , string $config 'default' )
Read a key from a cache config.
Reading from the active cache configuration.
Cache::read('my_data');
Reading from a specific cache configuration.
Cache::read('my_data', 'long_term');
$key
$config
optional 'default' remember( string $key , callable $callable , string $config 'default' )
Provides the ability to easily do read-through caching.
When called if the $key is not set in $config, the $callable function will be invoked. The results will then be stored into the cache config at key.
Examples:
Using a Closure to provide data, assume $this is a Model:
$model = $this; $results = Cache::remember('all_articles', function() use ($model) { return $model->find('all'); });
$key
$callable
The callable that provides data in the case when the cache key is empty. Can be any callable type supported by your PHP.
$config
optional 'default' The cache configuration to use for this operation. Defaults to default.
set( string|array $settings array() , string $value null , string $config 'default' )
Temporarily change the settings on a cache config. The settings will persist for the next write operation (write, decrement, increment, clear). Any reads that are done before the write, will use the modified settings. If $settings
is empty, the settings will be reset to the original configuration.
Can be called with 2 or 3 parameters. To set multiple values at once.
Cache::set(array('duration' => '+30 minutes'), 'my_config');
Or to set one value.
Cache::set('duration', '+30 minutes', 'my_config');
To reset a config back to the originally configured values.
Cache::set(null, 'my_config');
$settings
optional array() $value
optional null $config
optional 'default' settings( string $name 'default' )
Return the settings for the named cache engine.
$name
optional 'default' write( string $key , mixed $value , string $config 'default' )
Write data for key into a cache engine.
Writing to the active cache config:
Cache::write('cached_data', $data);
Writing to a specific cache config:
Cache::write('cached_data', $data, 'long_term');
$key
$value
$config
optional 'default' protected static array
Cache configuration stack Keeps the permanent/default settings for each cache engine. These settings are used to reset the engines after temporary modification.
array()
protected static array
Whether to reset the settings with the next call to Cache::set();
false
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
http://api.cakephp.org/2.8/class-Cache.html