cakephp2-php8/lib/Cake/Cache/CacheEngine.php

181 lines
4.7 KiB
PHP
Raw Normal View History

<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Cache
* @since CakePHP(tm) v 1.2.0.4933
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Storage engine for CakePHP caching
*
* @package Cake.Cache
*/
abstract class CacheEngine {
/**
* Settings of current engine instance
*
* @var array
*/
public $settings = array();
/**
* 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
*
* @var string
2013-01-11 14:06:54 +00:00
*/
2012-04-23 01:03:07 +00:00
protected $_groupPrefix = null;
/**
* Initialize the cache engine
*
* Called automatically by the cache frontend
*
* @param array $settings Associative array of parameters for the engine
* @return boolean True if the engine has been successfully initialized, false if not
*/
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-04-24 04:12:57 +00:00
$this->settings = $settings;
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']));
}
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
*
2013-03-05 07:05:14 +00:00
* @param integer $expires [optional] An expires timestamp, invalidating all data before.
* @return void
*/
public function gc($expires = null) {
2012-03-03 22:34:13 +00:00
}
/**
* Write value for a key into cache
*
* @param string $key Identifier for the data
* @param mixed $value Data to be cached
* @param integer $duration How long to cache for.
* @return boolean True if the data was successfully cached, false on failure
*/
abstract public function write($key, $value, $duration);
/**
* 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
* @param integer $offset How much to add
* @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
* @param integer $offset How much to subtract
* @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
* @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
*/
abstract public function delete($key);
/**
* Delete all keys from the cache
*
* @param boolean $check if true will check expiration, otherwise delete all
* @return boolean True if the cache was successfully cleared, false otherwise
*/
abstract public function clear($check);
/**
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
* the same result.
*
* @param string $groups name of the group to be cleared
* @return boolean
2012-04-24 02:27:27 +00:00
*/
public function clearGroup($group) {
return false;
}
/**
* 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'];
}
/**
* 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;
}
$prefix = '';
2012-04-23 01:03:07 +00:00
if (!empty($this->_groupPrefix)) {
$prefix = vsprintf($this->_groupPrefix, $this->groups());
}
2012-04-24 03:59:32 +00:00
$key = preg_replace('/[\s]+/', '_', strtolower(trim(str_replace(array(DS, '/', '.'), '_', strval($key)))));
return $prefix . $key;
}
2012-03-03 22:34:13 +00:00
}