2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Caching for CakePHP.
|
|
|
|
*
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-22 02:59:25 +00:00
|
|
|
* @package cake.libs
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4933
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-22 02:59:25 +00:00
|
|
|
* 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.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-12-22 02:59:25 +00:00
|
|
|
* @package cake.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-11-13 23:08:23 +00:00
|
|
|
class Cache {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Cache configuration stack
|
2009-11-17 05:05:14 +00:00
|
|
|
* Keeps the permanent/default settings for each cache engine.
|
|
|
|
* These settings are used to reset the engines after temporary modification.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-15 03:56:44 +00:00
|
|
|
protected static $_config = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-28 02:29:58 +00:00
|
|
|
/**
|
2009-11-17 05:05:14 +00:00
|
|
|
* Whether to reset the settings with the next call to Cache::set();
|
2008-07-28 02:29:58 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-15 03:56:44 +00:00
|
|
|
protected static $_reset = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-11-17 05:05:14 +00:00
|
|
|
/**
|
|
|
|
* Engine instances keyed by configuration name.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-15 03:56:44 +00:00
|
|
|
protected static $_engines = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-11-17 02:58:27 +00:00
|
|
|
* Set the cache configuration to use. config() can
|
|
|
|
* both create new configurations, return the settings for already configured
|
2010-09-18 04:45:29 +00:00
|
|
|
* configurations.
|
2009-11-17 02:58:27 +00:00
|
|
|
*
|
|
|
|
* To create a new configuration:
|
|
|
|
*
|
|
|
|
* `Cache::config('my_config', array('engine' => 'File', 'path' => TMP));`
|
|
|
|
*
|
|
|
|
* To get the settings for a configuration, and set it as the currently selected configuration
|
|
|
|
*
|
|
|
|
* `Cache::config('default');`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @see app/config/core.php for configuration settings
|
|
|
|
* @param string $name Name of the configuration
|
|
|
|
* @param array $settings Optional associative array of settings passed to the engine
|
|
|
|
* @return array(engine, settings) on success, false on failure
|
2010-12-12 00:01:07 +00:00
|
|
|
* @throws CacheException
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-15 03:56:44 +00:00
|
|
|
public static function config($name = null, $settings = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($name)) {
|
|
|
|
$settings = $name;
|
|
|
|
}
|
|
|
|
|
2008-07-28 02:29:58 +00:00
|
|
|
$current = array();
|
2010-04-15 03:56:44 +00:00
|
|
|
if (isset(self::$_config[$name])) {
|
|
|
|
$current = self::$_config[$name];
|
2008-07-28 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($settings)) {
|
2010-04-15 03:56:44 +00:00
|
|
|
self::$_config[$name] = array_merge($current, $settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 03:56:44 +00:00
|
|
|
if (empty(self::$_config[$name]['engine'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-15 03:56:44 +00:00
|
|
|
$engine = self::$_config[$name]['engine'];
|
2008-06-11 15:51:32 +00:00
|
|
|
|
2010-04-15 03:56:44 +00:00
|
|
|
if (!isset(self::$_engines[$name])) {
|
|
|
|
self::_buildEngine($name);
|
|
|
|
$settings = self::$_config[$name] = self::settings($name);
|
2010-09-18 04:45:29 +00:00
|
|
|
} elseif ($settings = self::set(self::$_config[$name], null, $name)) {
|
2010-04-15 03:56:44 +00:00
|
|
|
self::$_config[$name] = $settings;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return compact('engine', 'settings');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-11-17 05:05:14 +00:00
|
|
|
/**
|
|
|
|
* Finds and builds the instance of the required engine class.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the config array that needs an engine instance built
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-15 03:56:44 +00:00
|
|
|
protected static function _buildEngine($name) {
|
|
|
|
$config = self::$_config[$name];
|
2009-11-17 05:05:14 +00:00
|
|
|
|
|
|
|
list($plugin, $class) = pluginSplit($config['engine']);
|
2009-11-22 01:01:33 +00:00
|
|
|
$cacheClass = $class . 'Engine';
|
2010-04-15 03:56:44 +00:00
|
|
|
if (!class_exists($cacheClass) && self::_loadEngine($class, $plugin) === false) {
|
2009-11-17 05:05:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$cacheClass = $class . 'Engine';
|
2010-12-12 00:01:07 +00:00
|
|
|
if (!is_subclass_of($cacheClass, 'CacheEngine')) {
|
|
|
|
throw new CacheException(__('Cache engines must use CacheEngine as a base class.'));
|
2010-04-24 00:41:29 +00:00
|
|
|
}
|
2010-12-12 00:01:07 +00:00
|
|
|
self::$_engines[$name] = new $cacheClass();
|
2010-04-15 03:56:44 +00:00
|
|
|
if (self::$_engines[$name]->init($config)) {
|
|
|
|
if (time() % self::$_engines[$name]->settings['probability'] === 0) {
|
|
|
|
self::$_engines[$name]->gc();
|
2009-11-17 05:05:14 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-14 18:09:43 +00:00
|
|
|
/**
|
|
|
|
* Returns an array containing the currently configured Cache settings.
|
|
|
|
*
|
2010-01-22 22:54:28 +00:00
|
|
|
* @return array Array of configured Cache config names.
|
2009-11-15 01:24:18 +00:00
|
|
|
*/
|
2010-04-15 03:56:44 +00:00
|
|
|
public static function configured() {
|
|
|
|
return array_keys(self::$_config);
|
2009-11-14 18:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-11-15 01:18:58 +00:00
|
|
|
* Drops a cache engine. Deletes the cache configuration information
|
2009-11-14 18:09:43 +00:00
|
|
|
* If the deleted configuration is the last configuration using an certain engine,
|
|
|
|
* the Engine instance is also unset.
|
|
|
|
*
|
|
|
|
* @param string $name A currently configured cache config you wish to remove.
|
|
|
|
* @return boolen success of the removal, returns false when the config does not exist.
|
2009-11-15 01:24:18 +00:00
|
|
|
*/
|
2010-04-15 03:56:44 +00:00
|
|
|
public static function drop($name) {
|
|
|
|
if (!isset(self::$_config[$name])) {
|
2009-11-14 18:09:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-18 04:53:52 +00:00
|
|
|
unset(self::$_config[$name], self::$_engines[$name]);
|
2009-11-14 18:09:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-14 07:19:11 +00:00
|
|
|
/**
|
|
|
|
* Tries to find and include a file for a cache engine and returns object instance
|
|
|
|
*
|
2010-01-22 22:54:28 +00:00
|
|
|
* @param $name Name of the engine (without 'Engine')
|
2009-11-14 07:19:11 +00:00
|
|
|
* @return mixed $engine object or null
|
|
|
|
*/
|
2010-04-15 03:56:44 +00:00
|
|
|
protected static function _loadEngine($name, $plugin = null) {
|
2009-11-14 07:19:11 +00:00
|
|
|
if ($plugin) {
|
2009-11-22 01:05:19 +00:00
|
|
|
return App::import('Lib', $plugin . '.cache' . DS . $name, false);
|
2009-11-14 07:19:11 +00:00
|
|
|
} else {
|
2009-12-20 23:11:18 +00:00
|
|
|
$core = App::core();
|
|
|
|
$path = $core['libs'][0] . 'cache' . DS . strtolower($name) . '.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
require $path;
|
|
|
|
return true;
|
2009-11-14 07:19:11 +00:00
|
|
|
}
|
2009-12-20 23:11:18 +00:00
|
|
|
return App::import('Lib', 'cache' . DS . $name, false);
|
2009-11-14 07:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-28 02:29:58 +00:00
|
|
|
/**
|
2010-09-18 16:32:43 +00:00
|
|
|
* 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.
|
2008-07-28 02:29:58 +00:00
|
|
|
*
|
2010-09-18 15:11:28 +00:00
|
|
|
* 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');`
|
|
|
|
*
|
2010-09-18 16:32:43 +00:00
|
|
|
* To reset a config back to the originally configured values.
|
|
|
|
*
|
|
|
|
* `Cache::set(null, 'my_config');`
|
|
|
|
*
|
2008-07-28 02:29:58 +00:00
|
|
|
* @param mixed $settings Optional string for simple name-value pair or array
|
|
|
|
* @param string $value Optional for a simple name-value pair
|
2010-09-18 04:45:29 +00:00
|
|
|
* @param string $config The configuration name you are changing. Defaults to 'default'
|
2010-01-22 22:54:28 +00:00
|
|
|
* @return array Array of settings.
|
2008-07-28 02:29:58 +00:00
|
|
|
*/
|
2010-09-18 04:45:29 +00:00
|
|
|
public static function set($settings = array(), $value = null, $config = 'default') {
|
2010-09-18 15:11:28 +00:00
|
|
|
if (is_array($settings) && $value !== null) {
|
|
|
|
$config = $value;
|
|
|
|
}
|
2010-09-18 04:45:29 +00:00
|
|
|
if (!isset(self::$_config[$config]) || !isset(self::$_engines[$config])) {
|
2008-07-28 02:29:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!empty($settings)) {
|
2010-04-15 03:56:44 +00:00
|
|
|
self::$_reset = true;
|
2008-07-28 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 03:56:44 +00:00
|
|
|
if (self::$_reset === true) {
|
2008-07-28 02:29:58 +00:00
|
|
|
if (empty($settings)) {
|
2010-04-15 03:56:44 +00:00
|
|
|
self::$_reset = false;
|
2010-09-18 04:45:29 +00:00
|
|
|
$settings = self::$_config[$config];
|
2008-07-28 02:29:58 +00:00
|
|
|
} else {
|
|
|
|
if (is_string($settings) && $value !== null) {
|
|
|
|
$settings = array($settings => $value);
|
|
|
|
}
|
2010-09-18 04:45:29 +00:00
|
|
|
$settings = array_merge(self::$_config[$config], $settings);
|
2009-11-21 21:09:06 +00:00
|
|
|
if (isset($settings['duration']) && !is_numeric($settings['duration'])) {
|
|
|
|
$settings['duration'] = strtotime($settings['duration']) - time();
|
|
|
|
}
|
2008-07-28 02:29:58 +00:00
|
|
|
}
|
2010-09-18 04:45:29 +00:00
|
|
|
self::$_engines[$config]->settings = $settings;
|
2008-07-28 02:29:58 +00:00
|
|
|
}
|
2010-09-18 04:45:29 +00:00
|
|
|
return self::settings($config);
|
2008-07-28 02:29:58 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Garbage collection
|
|
|
|
*
|
|
|
|
* Permanently remove all expired and deleted data
|
|
|
|
*
|
2010-09-18 04:45:29 +00:00
|
|
|
* @param string $config The config name you wish to have garbage collected. Defaults to 'default'
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-09-18 04:45:29 +00:00
|
|
|
public static function gc($config = 'default') {
|
|
|
|
self::$_engines[$config]->gc();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-22 22:54:28 +00:00
|
|
|
* Write data for key into cache. Will automatically use the currently
|
|
|
|
* active cache configuration. To set the currently active configuration use
|
|
|
|
* Cache::config()
|
|
|
|
*
|
|
|
|
* ### Usage:
|
|
|
|
*
|
|
|
|
* Writing to the active cache config:
|
|
|
|
*
|
|
|
|
* `Cache::write('cached_data', $data);`
|
|
|
|
*
|
|
|
|
* Writing to a specific cache config:
|
|
|
|
*
|
|
|
|
* `Cache::write('cached_data', $data, 'long_term');`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param mixed $value Data to be cached - anything except a resource
|
2010-09-18 04:45:29 +00:00
|
|
|
* @param string $config Optional string configuration name to write to. Defaults to 'default'
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean True if the data was successfully cached, false on failure
|
|
|
|
*/
|
2010-09-18 04:45:29 +00:00
|
|
|
public static function write($key, $value, $config = 'default') {
|
2010-04-15 03:56:44 +00:00
|
|
|
$settings = self::settings($config);
|
2008-07-28 02:29:58 +00:00
|
|
|
|
2008-07-28 20:29:55 +00:00
|
|
|
if (empty($settings)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
if (!self::isInitialized($config)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
$key = self::$_engines[$config]->key($key);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-09-19 16:20:07 +00:00
|
|
|
if (!$key || is_resource($value)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-09-24 11:52:29 +00:00
|
|
|
|
2010-04-15 03:56:44 +00:00
|
|
|
$success = self::$_engines[$config]->write($settings['prefix'] . $key, $value, $settings['duration']);
|
2010-09-18 16:32:43 +00:00
|
|
|
self::set(null, $config);
|
2010-07-25 05:15:28 +00:00
|
|
|
if ($success === false && $value !== '') {
|
2010-07-24 01:40:57 +00:00
|
|
|
trigger_error(
|
2010-12-05 01:37:13 +00:00
|
|
|
__("%s cache was unable to write '%s' to cache", $config, $key),
|
2010-07-24 01:40:57 +00:00
|
|
|
E_USER_WARNING
|
|
|
|
);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
return $success;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-22 22:54:28 +00:00
|
|
|
* Read a key from the cache. Will automatically use the currently
|
|
|
|
* active cache configuration. To set the currently active configuration use
|
|
|
|
* Cache::config()
|
|
|
|
*
|
|
|
|
* ### Usage:
|
|
|
|
*
|
|
|
|
* Reading from the active cache configuration.
|
|
|
|
*
|
|
|
|
* `Cache::read('my_data');`
|
|
|
|
*
|
|
|
|
* Reading from a specific cache configuration.
|
|
|
|
*
|
|
|
|
* `Cache::read('my_data', 'long_term');`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
2010-09-18 04:45:29 +00:00
|
|
|
* @param string $config optional name of the configuration to use. Defaults to 'default'
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
|
|
|
|
*/
|
2010-09-18 04:45:29 +00:00
|
|
|
public static function read($key, $config = 'default') {
|
2010-04-15 03:56:44 +00:00
|
|
|
$settings = self::settings($config);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-07-28 20:29:55 +00:00
|
|
|
if (empty($settings)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
if (!self::isInitialized($config)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
$key = self::$_engines[$config]->key($key);
|
2009-11-17 05:05:14 +00:00
|
|
|
if (!$key) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-18 16:32:43 +00:00
|
|
|
return self::$_engines[$config]->read($settings['prefix'] . $key);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-01-21 16:41:44 +00:00
|
|
|
/**
|
2010-01-22 22:54:28 +00:00
|
|
|
* Increment a number under the key and return incremented value.
|
|
|
|
*
|
2010-01-21 16:41:44 +00:00
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param integer $offset How much to add
|
2010-09-18 04:45:29 +00:00
|
|
|
* @param string $config Optional string configuration name. Defaults to 'default'
|
2010-01-22 22:54:28 +00:00
|
|
|
* @return mixed new value, or false if the data doesn't exist, is not integer,
|
|
|
|
* or if there was an error fetching it.
|
2010-01-21 16:41:44 +00:00
|
|
|
*/
|
2010-09-18 04:45:29 +00:00
|
|
|
public static function increment($key, $offset = 1, $config = 'default') {
|
2010-04-15 03:56:44 +00:00
|
|
|
$settings = self::settings($config);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
|
|
|
if (empty($settings)) {
|
|
|
|
return null;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
if (!self::isInitialized($config)) {
|
2010-01-21 16:41:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
$key = self::$_engines[$config]->key($key);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
2010-01-21 18:57:42 +00:00
|
|
|
if (!$key || !is_integer($offset) || $offset < 0) {
|
2010-01-21 16:41:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
$success = self::$_engines[$config]->increment($settings['prefix'] . $key, $offset);
|
2010-09-18 16:32:43 +00:00
|
|
|
self::set(null, $config);
|
2010-01-21 16:41:44 +00:00
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
/**
|
2010-01-22 22:54:28 +00:00
|
|
|
* Decrement a number under the key and return decremented value.
|
|
|
|
*
|
2010-01-21 16:41:44 +00:00
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param integer $offset How much to substract
|
2010-09-18 04:45:29 +00:00
|
|
|
* @param string $config Optional string configuration name. Defaults to 'default'
|
2010-01-22 22:54:28 +00:00
|
|
|
* @return mixed new value, or false if the data doesn't exist, is not integer,
|
|
|
|
* or if there was an error fetching it
|
2010-01-21 16:41:44 +00:00
|
|
|
*/
|
2010-09-18 04:45:29 +00:00
|
|
|
public static function decrement($key, $offset = 1, $config = 'default') {
|
2010-04-15 03:56:44 +00:00
|
|
|
$settings = self::settings($config);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
|
|
|
if (empty($settings)) {
|
|
|
|
return null;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
if (!self::isInitialized($config)) {
|
2010-01-21 16:41:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
$key = self::$_engines[$config]->key($key);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
2010-01-21 18:57:42 +00:00
|
|
|
if (!$key || !is_integer($offset) || $offset < 0) {
|
2010-01-21 16:41:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
$success = self::$_engines[$config]->decrement($settings['prefix'] . $key, $offset);
|
2010-09-18 16:32:43 +00:00
|
|
|
self::set(null, $config);
|
2010-01-21 16:41:44 +00:00
|
|
|
return $success;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-09-18 04:45:29 +00:00
|
|
|
* Delete a key from the cache.
|
2010-01-22 22:54:28 +00:00
|
|
|
*
|
|
|
|
* ### Usage:
|
|
|
|
*
|
|
|
|
* Deleting from the active cache configuration.
|
|
|
|
*
|
|
|
|
* `Cache::delete('my_data');`
|
|
|
|
*
|
|
|
|
* Deleting from a specific cache configuration.
|
|
|
|
*
|
|
|
|
* `Cache::delete('my_data', 'long_term');`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
2010-09-18 04:45:29 +00:00
|
|
|
* @param string $config name of the configuration to use. Defaults to 'default'
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
|
|
|
|
*/
|
2010-09-18 04:45:29 +00:00
|
|
|
public static function delete($key, $config = 'default') {
|
2010-04-15 03:56:44 +00:00
|
|
|
$settings = self::settings($config);
|
2008-07-28 02:29:58 +00:00
|
|
|
|
2008-07-28 20:29:55 +00:00
|
|
|
if (empty($settings)) {
|
2008-07-28 02:29:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
if (!self::isInitialized($config)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
$key = self::$_engines[$config]->key($key);
|
2009-11-17 05:05:14 +00:00
|
|
|
if (!$key) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-15 03:56:44 +00:00
|
|
|
$success = self::$_engines[$config]->delete($settings['prefix'] . $key);
|
2010-09-18 16:32:43 +00:00
|
|
|
self::set(null, $config);
|
2008-05-30 11:40:08 +00:00
|
|
|
return $success;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-22 22:54:28 +00:00
|
|
|
* Delete all keys from the cache.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param boolean $check if true will check expiration, otherwise delete all
|
2010-09-18 04:45:29 +00:00
|
|
|
* @param string $config name of the configuration to use. Defaults to 'default'
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean True if the cache was succesfully cleared, false otherwise
|
|
|
|
*/
|
2010-09-18 04:45:29 +00:00
|
|
|
public static function clear($check = false, $config = 'default') {
|
2010-04-15 03:56:44 +00:00
|
|
|
if (!self::isInitialized($config)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
$success = self::$_engines[$config]->clear($check);
|
2010-09-18 16:32:43 +00:00
|
|
|
self::set(null, $config);
|
2008-05-30 11:40:08 +00:00
|
|
|
return $success;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-11-17 05:05:14 +00:00
|
|
|
* Check if Cache has initialized a working config for the given name.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-09-18 15:11:28 +00:00
|
|
|
* @param string $engine Name of the engine, Defaults to default
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $config Name of the configuration setting
|
2010-01-22 22:54:28 +00:00
|
|
|
* @return bool Whether or not the config name has been initialized.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-09-18 15:11:28 +00:00
|
|
|
public static function isInitialized($name = 'default') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (Configure::read('Cache.disable')) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-15 03:56:44 +00:00
|
|
|
return isset(self::$_engines[$name]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-09-18 04:45:29 +00:00
|
|
|
* Return the settings for the named cache engine.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-09-18 15:11:28 +00:00
|
|
|
* @param string $engine Name of the configuration to get settings for. Defaults to 'default'
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array list of settings for this engine
|
2009-11-17 05:05:14 +00:00
|
|
|
* @see Cache::config()
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-09-18 04:45:29 +00:00
|
|
|
public static function settings($name = 'default') {
|
2010-04-15 03:56:44 +00:00
|
|
|
if (!empty(self::$_engines[$name])) {
|
|
|
|
return self::$_engines[$name]->settings();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Storage engine for CakePHP caching
|
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-18 04:38:36 +00:00
|
|
|
abstract class CacheEngine {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-22 22:54:28 +00:00
|
|
|
* Settings of current engine instance
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $settings = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-11-21 20:19:11 +00:00
|
|
|
* Initialize the cache engine
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Called automatically by the cache frontend
|
|
|
|
*
|
|
|
|
* @param array $params Associative array of parameters for the engine
|
|
|
|
* @return boolean True if the engine has been succesfully initialized, false if not
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function init($settings = array()) {
|
2010-01-22 22:54:28 +00:00
|
|
|
$this->settings = array_merge(
|
|
|
|
array('prefix' => 'cake_', 'duration'=> 3600, 'probability'=> 100),
|
|
|
|
$this->settings,
|
|
|
|
$settings
|
|
|
|
);
|
2008-07-26 20:00:20 +00:00
|
|
|
if (!is_numeric($this->settings['duration'])) {
|
|
|
|
$this->settings['duration'] = strtotime($this->settings['duration']) - time();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Garbage collection
|
|
|
|
*
|
|
|
|
* Permanently remove all expired and deleted data
|
2010-04-18 04:38:36 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-19 04:04:34 +00:00
|
|
|
public function gc() { }
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Write value for a key into cache
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param mixed $value Data to be cached
|
2010-04-18 04:38:36 +00:00
|
|
|
* @param mixed $duration How long to cache for.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean True if the data was succesfully cached, false on failure
|
|
|
|
*/
|
2010-04-18 04:38:36 +00:00
|
|
|
abstract public function write($key, $value, $duration);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Read a key from the cache
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
|
|
|
|
*/
|
2010-04-18 04:38:36 +00:00
|
|
|
abstract public function read($key);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-01-21 16:41:44 +00:00
|
|
|
/**
|
|
|
|
* Increment a number under the key and return incremented value
|
2010-01-22 22:54:28 +00:00
|
|
|
*
|
2010-01-21 16:41:44 +00:00
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param integer $offset How much to add
|
|
|
|
* @return New incremented value, false otherwise
|
|
|
|
*/
|
2010-04-18 04:38:36 +00:00
|
|
|
abstract public function increment($key, $offset = 1);
|
|
|
|
|
2010-01-21 16:41:44 +00:00
|
|
|
/**
|
|
|
|
* Decrement a number under the key and return decremented value
|
2010-01-22 22:54:28 +00:00
|
|
|
*
|
2010-01-21 16:41:44 +00:00
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param integer $value How much to substract
|
|
|
|
* @return New incremented value, false otherwise
|
|
|
|
*/
|
2010-04-18 04:38:36 +00:00
|
|
|
abstract public function decrement($key, $offset = 1);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Delete a key from the cache
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
|
|
|
|
*/
|
2010-04-18 04:38:36 +00:00
|
|
|
abstract public function delete($key);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Delete all keys from the cache
|
|
|
|
*
|
|
|
|
* @param boolean $check if true will check expiration, otherwise delete all
|
|
|
|
* @return boolean True if the cache was succesfully cleared, false otherwise
|
|
|
|
*/
|
2010-04-18 04:38:36 +00:00
|
|
|
abstract public function clear($check);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Cache Engine settings
|
|
|
|
*
|
|
|
|
* @return array settings
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function settings() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->settings;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-08-12 21:08:51 +00:00
|
|
|
/**
|
2010-01-22 22:54:28 +00:00
|
|
|
* Generates a safe key for use with cache engine storage engines.
|
2008-08-12 21:08:51 +00:00
|
|
|
*
|
|
|
|
* @param string $key the key passed over
|
|
|
|
* @return mixed string $key or false
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function key($key) {
|
2008-08-12 21:08:51 +00:00
|
|
|
if (empty($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-13 18:59:30 +00:00
|
|
|
$key = Inflector::underscore(str_replace(array(DS, '/', '.'), '_', strval($key)));
|
2008-08-12 21:08:51 +00:00
|
|
|
return $key;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|