2012-01-29 18:25:06 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2012-01-29 18:25:06 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2012-01-29 18:25:06 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2012-01-29 18:25:06 +00:00
|
|
|
* @package Cake.Cache
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4933
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2012-01-29 18:25:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage engine for CakePHP caching
|
|
|
|
*
|
|
|
|
* @package Cake.Cache
|
|
|
|
*/
|
|
|
|
abstract class CacheEngine {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Settings of current engine instance
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $settings = array();
|
|
|
|
|
2012-03-25 23:47:08 +00:00
|
|
|
/**
|
|
|
|
* Contains the compiled string with all groups
|
2013-03-05 07:05:14 +00:00
|
|
|
* prefixes to be prepended to every key in this cache engine
|
2012-03-25 23:47:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-04-23 01:03:07 +00:00
|
|
|
protected $_groupPrefix = null;
|
2012-03-25 23:47:08 +00:00
|
|
|
|
2012-01-29 18:25:06 +00:00
|
|
|
/**
|
|
|
|
* Initialize the cache engine
|
|
|
|
*
|
|
|
|
* Called automatically by the cache frontend
|
|
|
|
*
|
|
|
|
* @param array $settings Associative array of parameters for the engine
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if the engine has been successfully initialized, false if not
|
2012-01-29 18:25:06 +00:00
|
|
|
*/
|
|
|
|
public function init($settings = array()) {
|
2012-04-24 04:12:57 +00:00
|
|
|
$settings += $this->settings + array(
|
|
|
|
'prefix' => 'cake_',
|
|
|
|
'duration' => 3600,
|
|
|
|
'probability' => 100,
|
|
|
|
'groups' => array()
|
2012-01-29 18:25:06 +00:00
|
|
|
);
|
2012-04-24 04:12:57 +00:00
|
|
|
$this->settings = $settings;
|
2012-03-25 23:47:08 +00:00
|
|
|
if (!empty($this->settings['groups'])) {
|
|
|
|
sort($this->settings['groups']);
|
2012-04-23 01:03:07 +00:00
|
|
|
$this->_groupPrefix = str_repeat('%s_', count($this->settings['groups']));
|
2012-03-25 23:47:08 +00:00
|
|
|
}
|
2012-01-29 18:25:06 +00:00
|
|
|
if (!is_numeric($this->settings['duration'])) {
|
|
|
|
$this->settings['duration'] = strtotime($this->settings['duration']) - time();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Garbage collection
|
|
|
|
*
|
|
|
|
* Permanently remove all expired and deleted data
|
2012-07-18 01:55:29 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $expires [optional] An expires timestamp, invalidating all data before.
|
2012-01-29 18:25:06 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-03-24 20:32:31 +00:00
|
|
|
public function gc($expires = null) {
|
2012-03-03 22:34:13 +00:00
|
|
|
}
|
2012-01-29 18:25:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write value for a key into cache
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param mixed $value Data to be cached
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $duration How long to cache for.
|
|
|
|
* @return bool True if the data was successfully cached, false on failure
|
2012-01-29 18:25:06 +00:00
|
|
|
*/
|
|
|
|
abstract public function write($key, $value, $duration);
|
|
|
|
|
2015-07-27 21:42:00 +00:00
|
|
|
/**
|
|
|
|
* Write value for a key into cache if it doesn't already exist
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param mixed $value Data to be cached
|
|
|
|
* @param int $duration How long to cache for.
|
|
|
|
* @return bool True if the data was successfully cached, false on failure
|
|
|
|
*/
|
|
|
|
public function add($key, $value, $duration) {
|
|
|
|
}
|
|
|
|
|
2012-01-29 18:25:06 +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
|
|
|
|
*/
|
|
|
|
abstract public function read($key);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment a number under the key and return incremented value
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $offset How much to add
|
2012-01-29 18:25:06 +00:00
|
|
|
* @return New incremented value, false otherwise
|
|
|
|
*/
|
|
|
|
abstract public function increment($key, $offset = 1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrement a number under the key and return decremented value
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $offset How much to subtract
|
2012-01-29 18:25:06 +00:00
|
|
|
* @return New incremented value, false otherwise
|
|
|
|
*/
|
|
|
|
abstract public function decrement($key, $offset = 1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a key from the cache
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
|
2012-01-29 18:25:06 +00:00
|
|
|
*/
|
|
|
|
abstract public function delete($key);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all keys from the cache
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $check if true will check expiration, otherwise delete all
|
|
|
|
* @return bool True if the cache was successfully cleared, false otherwise
|
2012-01-29 18:25:06 +00:00
|
|
|
*/
|
|
|
|
abstract public function clear($check);
|
|
|
|
|
2012-03-26 00:15:32 +00:00
|
|
|
/**
|
2013-03-05 07:05:14 +00:00
|
|
|
* Clears all values belonging to a group. Is up to the implementing engine
|
2013-02-25 06:53:28 +00:00
|
|
|
* to decide whether actually delete the keys or just simulate it to achieve
|
2012-03-26 00:15:32 +00:00
|
|
|
* the same result.
|
|
|
|
*
|
2014-06-01 02:02:55 +00:00
|
|
|
* @param string $group name of the group to be cleared
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2012-04-24 02:27:27 +00:00
|
|
|
*/
|
2012-03-27 04:38:24 +00:00
|
|
|
public function clearGroup($group) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-26 00:15:32 +00:00
|
|
|
|
2012-03-25 23:47:08 +00:00
|
|
|
/**
|
|
|
|
* Does whatever initialization for each group is required
|
|
|
|
* and returns the `group value` for each of them, this is
|
|
|
|
* the token representing each group in the cache key
|
|
|
|
*
|
|
|
|
* @return array
|
2012-04-24 02:27:27 +00:00
|
|
|
*/
|
|
|
|
public function groups() {
|
|
|
|
return $this->settings['groups'];
|
|
|
|
}
|
2012-03-25 23:47:08 +00:00
|
|
|
|
2012-01-29 18:25:06 +00:00
|
|
|
/**
|
|
|
|
* Cache Engine settings
|
|
|
|
*
|
|
|
|
* @return array settings
|
|
|
|
*/
|
|
|
|
public function settings() {
|
|
|
|
return $this->settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a safe key for use with cache engine storage engines.
|
|
|
|
*
|
|
|
|
* @param string $key the key passed over
|
|
|
|
* @return mixed string $key or false
|
|
|
|
*/
|
|
|
|
public function key($key) {
|
|
|
|
if (empty($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-25 23:47:08 +00:00
|
|
|
|
|
|
|
$prefix = '';
|
2012-04-23 01:03:07 +00:00
|
|
|
if (!empty($this->_groupPrefix)) {
|
2017-12-28 15:15:14 +00:00
|
|
|
$prefix = md5(implode('_', $this->groups()));
|
2012-03-25 23:47:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-24 03:59:32 +00:00
|
|
|
$key = preg_replace('/[\s]+/', '_', strtolower(trim(str_replace(array(DS, '/', '.'), '_', strval($key)))));
|
2012-03-25 23:47:08 +00:00
|
|
|
return $prefix . $key;
|
2012-01-29 18:25:06 +00:00
|
|
|
}
|
|
|
|
}
|