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 Config/cache.php
file. A sample configuration would be:
Cache::config('shared', [ 'className' => 'Cake\Cache\Engine\ApcEngine', '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.
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. Well suited to development as it is an easy cache to inspect and manually flush.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. This engine is recommended to people deploying on windows with IIS.RedisEngine
- Uses redis and php-redis extension to store cache data.See Cache engine documentation for expected configuration keys.
array
An array mapping url schemes to fully qualified caching engine class names.
boolean
array
Returns the Cache Registry instance used for creating and using cache adapters. Also allows for injecting of a new registry instance.
_buildEngine( string $name )
Finds and builds the instance of the required engine class.
$name
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' clearAll( boolean $check false )
Delete all keys from the cache from all configurations.
$check
optional false clearGroup( string $group , string $config 'default' )
Delete all keys from the cache belonging to the same group.
$group
$config
optional 'default' 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' deleteMany( array $keys , string $config 'default' )
Delete many keys from the cache.
Deleting multiple keys from the active cache configuration.
Cache::deleteMany(['my_data_1', 'my_data_2']);
Deleting from a specific cache configuration.
Cache::deleteMany(['my_data_1', 'my_data_2], 'long_term');
$keys
$config
optional 'default' of boolean values that are true if the value was successfully deleted, false if it didn't exist or couldn't be removed
disable( )
Disable caching.
When disabled all cache operations will return null.
enable( )
Re-enable caching.
If caching has been disabled with Cache::disable() this method will reverse that effect.
engine( string $config )
Fetch the engine attached to a specific configuration name.
If the cache engine & configuration are missing an error will be triggered.
$config
Cake\Cache\CacheEngine
gc( string $config 'default' , integer|null $expires null )
Garbage collection
Permanently remove all expired and deleted data
$config
optional 'default' $expires
optional null groupConfigs( string|null $group null )
Retrieve group names to config mapping.
Cache::config('daily', ['duration' => '1 day', 'groups' => ['posts']]); Cache::config('weekly', ['duration' => '1 week', 'groups' => ['posts', 'archive']]); $configs = Cache::groupConfigs('posts');
$configs will equal to ['posts' => ['daily', 'weekly']]
Calling this method will load all the configured engines.
$group
optional null 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.
read( string $key , string $config 'default' )
Read a key from the cache.
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' readMany( array $keys , string $config 'default' )
Read multiple keys from the cache.
Reading multiple keys from the active cache configuration.
Cache::readMany(['my_data_1', 'my_data_2]);
Reading from a specific cache configuration.
Cache::readMany(['my_data_1', 'my_data_2], 'long_term');
$keys
$config
optional 'default' An array containing, for each of the given $keys, the cached data or false if cached data could not be retrieved.
registry( Cake\Core\ObjectRegistry $registry null )
Returns the Cache Registry instance used for creating and using cache adapters. Also allows for injecting of a new registry instance.
Cake\Core\ObjectRegistry
$registry
optional null Cake\Core\ObjectRegistry
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 Table object:
$results = Cache::remember('all_articles', function () { return $this->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.
If the key is found: the cached data, false if the data missing/expired, or an error. If the key is not found: boolean of the success of the write
write( string $key , mixed $value , string $config 'default' )
Write data for key into cache.
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' writeMany( array $data , string $config 'default' )
Write data for many keys into cache.
Writing to the active cache config:
Cache::writeMany(['cached_data_1' => 'data 1', 'cached_data_2' => 'data 2']);
Writing to a specific cache config:
Cache::writeMany(['cached_data_1' => 'data 1', 'cached_data_2' => 'data 2'], 'long_term');
$data
$config
optional 'default' config( string|array $key , array|null $config null )
This method can be used to define configuration adapters for an application or read existing configuration.
To change an adapter's configuration at runtime, first drop the adapter and then reconfigure it.
Adapters will not be constructed until the first operation is done.
Assuming that the class' name is Cache
the following scenarios are supported:
Reading config data back:
Cache::config('default');
Setting a cache engine up.
Cache::config('default', $settings);
Injecting a constructed adapter in:
Cache::config('default', $instance);
Configure multiple adapters at once:
Cache::config($arrayOfConfig);
$key
$config
optional null configured( )
Returns an array containing the named configurations
drop( string $config )
Drops a constructed adapter.
If you wish to modify an existing configuration, you should drop it, change configuration and then re-add it.
If the implementing objects supports a $_registry
object the named configuration will also be unloaded from the registry.
$config
dsnClassMap( array $map null )
Returns or updates the DSN class map for this class.
$map
optional null getConfig( string $key )
Reads existing configuration.
$key
getDsnClassMap( )
Returns the DSN class map for this class.
parseDsn( string $dsn )
Parses a DSN into a valid connection configuration
This method allows setting a DSN using formatting similar to that used by PEAR::DB. The following is an example of its usage:
$dsn = 'mysql://user:pass@localhost/database?'; $config = ConnectionManager::parseDsn($dsn); $dsn = 'Cake\Log\Engine\FileLog://?types=notice,info,debug&file=debug&path=LOGS'; $config = Log::parseDsn($dsn); $dsn = 'smtp://user:secret@localhost:25?timeout=30&client=null&tls=null'; $config = Email::parseDsn($dsn); $dsn = 'file:///?className=\My\Cache\Engine\FileEngine'; $config = Cache::parseDsn($dsn); $dsn = 'File://?prefix=myapp_cake_core_&serialize=true&duration=+2 minutes&path=/tmp/persistent/'; $config = Cache::parseDsn($dsn);
For all classes, the value of scheme
is set as the value of both the className
unless they have been otherwise specified.
Note that querystring arguments are also parsed and set as values in the returned configuration.
$dsn
setConfig( string|array $key , array $config null )
This method can be used to define configuration adapters for an application.
To change an adapter's configuration at runtime, first drop the adapter and then reconfigure it.
Adapters will not be constructed until the first operation is done.
Assuming that the class' name is Cache
the following scenarios are supported:
Setting a cache engine up.
Cache::setConfig('default', $settings);
Injecting a constructed adapter in:
Cache::setConfig('default', $instance);
Configure multiple adapters at once:
Cache::setConfig($arrayOfConfig);
$key
$config
optional null setDsnClassMap( array $map )
Updates the DSN class map for this class.
$map
protected static array
An array mapping url schemes to fully qualified caching engine class names.
[ 'apc' => 'Cake\Cache\Engine\ApcEngine', 'file' => 'Cake\Cache\Engine\FileEngine', 'memcached' => 'Cake\Cache\Engine\MemcachedEngine', 'null' => 'Cake\Cache\Engine\NullEngine', 'redis' => 'Cake\Cache\Engine\RedisEngine', 'wincache' => 'Cake\Cache\Engine\WincacheEngine', 'xcache' => 'Cake\Cache\Engine\XcacheEngine', ]
protected static Cake\Core\ObjectRegistry
Cache Registry used for creating and using cache adapters.
© 2005–2017 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.
https://api.cakephp.org/3.4/class-Cake.Cache.Cache.html