mirror of
https://github.com/brmlab/brmbiolab_sklad.git
synced 2025-06-10 22:24:12 +02:00
Initial commit
This commit is contained in:
commit
3b93da31de
1004 changed files with 265840 additions and 0 deletions
191
lib/Cake/Cache/Engine/ApcEngine.php
Normal file
191
lib/Cake/Cache/Engine/ApcEngine.php
Normal file
|
@ -0,0 +1,191 @@
|
|||
<?php
|
||||
/**
|
||||
* APC storage engine for cache.
|
||||
*
|
||||
* 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.Engine
|
||||
* @since CakePHP(tm) v 1.2.0.4933
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
* APC storage engine for cache
|
||||
*
|
||||
* @package Cake.Cache.Engine
|
||||
*/
|
||||
class ApcEngine extends CacheEngine {
|
||||
|
||||
/**
|
||||
* Contains the compiled group names
|
||||
* (prefixed with the global configuration prefix)
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_compiledGroupNames = array();
|
||||
|
||||
/**
|
||||
* Initialize the Cache Engine
|
||||
*
|
||||
* Called automatically by the cache frontend
|
||||
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
||||
*
|
||||
* @param array $settings array of setting for the engine
|
||||
* @return bool True if the engine has been successfully initialized, false if not
|
||||
* @see CacheEngine::__defaults
|
||||
*/
|
||||
public function init($settings = array()) {
|
||||
if (!isset($settings['prefix'])) {
|
||||
$settings['prefix'] = Inflector::slug(APP_DIR) . '_';
|
||||
}
|
||||
$settings += array('engine' => 'Apc');
|
||||
parent::init($settings);
|
||||
return function_exists('apc_dec');
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data for key into cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param mixed $value Data to be cached
|
||||
* @param int $duration How long to cache the data, in seconds
|
||||
* @return bool True if the data was successfully cached, false on failure
|
||||
*/
|
||||
public function write($key, $value, $duration) {
|
||||
$expires = 0;
|
||||
if ($duration) {
|
||||
$expires = time() + $duration;
|
||||
}
|
||||
apc_store($key . '_expires', $expires, $duration);
|
||||
return apc_store($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
|
||||
*/
|
||||
public function read($key) {
|
||||
$time = time();
|
||||
$cachetime = intval(apc_fetch($key . '_expires'));
|
||||
if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
|
||||
return false;
|
||||
}
|
||||
return apc_fetch($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the value of an integer cached key
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to increment
|
||||
* @return New incremented value, false otherwise
|
||||
*/
|
||||
public function increment($key, $offset = 1) {
|
||||
return apc_inc($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the value of an integer cached key
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to subtract
|
||||
* @return New decremented value, false otherwise
|
||||
*/
|
||||
public function decrement($key, $offset = 1) {
|
||||
return apc_dec($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a key from the cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
|
||||
*/
|
||||
public function delete($key) {
|
||||
return apc_delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all keys from the cache. This will clear every cache config using APC.
|
||||
*
|
||||
* @param bool $check If true, nothing will be cleared, as entries are removed
|
||||
* from APC as they expired. This flag is really only used by FileEngine.
|
||||
* @return bool True Returns true.
|
||||
*/
|
||||
public function clear($check) {
|
||||
if ($check) {
|
||||
return true;
|
||||
}
|
||||
if (class_exists('APCIterator', false)) {
|
||||
$iterator = new APCIterator(
|
||||
'user',
|
||||
'/^' . preg_quote($this->settings['prefix'], '/') . '/',
|
||||
APC_ITER_NONE
|
||||
);
|
||||
apc_delete($iterator);
|
||||
return true;
|
||||
}
|
||||
$cache = apc_cache_info('user');
|
||||
foreach ($cache['cache_list'] as $key) {
|
||||
if (strpos($key['info'], $this->settings['prefix']) === 0) {
|
||||
apc_delete($key['info']);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the `group value` for each of the configured groups
|
||||
* If the group initial value was not found, then it initializes
|
||||
* the group accordingly.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function groups() {
|
||||
if (empty($this->_compiledGroupNames)) {
|
||||
foreach ($this->settings['groups'] as $group) {
|
||||
$this->_compiledGroupNames[] = $this->settings['prefix'] . $group;
|
||||
}
|
||||
}
|
||||
|
||||
$groups = apc_fetch($this->_compiledGroupNames);
|
||||
if (count($groups) !== count($this->settings['groups'])) {
|
||||
foreach ($this->_compiledGroupNames as $group) {
|
||||
if (!isset($groups[$group])) {
|
||||
apc_store($group, 1);
|
||||
$groups[$group] = 1;
|
||||
}
|
||||
}
|
||||
ksort($groups);
|
||||
}
|
||||
|
||||
$result = array();
|
||||
$groups = array_values($groups);
|
||||
foreach ($this->settings['groups'] as $i => $group) {
|
||||
$result[] = $group . $groups[$i];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the group value to simulate deletion of all keys under a group
|
||||
* old values will remain in storage until they expire.
|
||||
*
|
||||
* @param string $group The group to clear.
|
||||
* @return bool success
|
||||
*/
|
||||
public function clearGroup($group) {
|
||||
apc_inc($this->settings['prefix'] . $group, 1, $success);
|
||||
return $success;
|
||||
}
|
||||
|
||||
}
|
431
lib/Cake/Cache/Engine/FileEngine.php
Normal file
431
lib/Cake/Cache/Engine/FileEngine.php
Normal file
|
@ -0,0 +1,431 @@
|
|||
<?php
|
||||
/**
|
||||
* File Storage engine for cache. Filestorage is the slowest cache storage
|
||||
* to read and write. However, it is good for servers that don't have other storage
|
||||
* engine available, or have content which is not performance sensitive.
|
||||
*
|
||||
* You can configure a FileEngine cache, using Cache::config()
|
||||
*
|
||||
* 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
|
||||
* @since CakePHP(tm) v 1.2.0.4933
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
* File Storage engine for cache. Filestorage is the slowest cache storage
|
||||
* to read and write. However, it is good for servers that don't have other storage
|
||||
* engine available, or have content which is not performance sensitive.
|
||||
*
|
||||
* You can configure a FileEngine cache, using Cache::config()
|
||||
*
|
||||
* @package Cake.Cache.Engine
|
||||
*/
|
||||
class FileEngine extends CacheEngine {
|
||||
|
||||
/**
|
||||
* Instance of SplFileObject class
|
||||
*
|
||||
* @var File
|
||||
*/
|
||||
protected $_File = null;
|
||||
|
||||
/**
|
||||
* Settings
|
||||
*
|
||||
* - path = absolute path to cache directory, default => CACHE
|
||||
* - prefix = string prefix for filename, default => cake_
|
||||
* - lock = enable file locking on write, default => true
|
||||
* - serialize = serialize the data, default => true
|
||||
*
|
||||
* @var array
|
||||
* @see CacheEngine::__defaults
|
||||
*/
|
||||
public $settings = array();
|
||||
|
||||
/**
|
||||
* True unless FileEngine::__active(); fails
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $_init = true;
|
||||
|
||||
/**
|
||||
* Initialize the Cache Engine
|
||||
*
|
||||
* Called automatically by the cache frontend
|
||||
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
||||
*
|
||||
* @param array $settings array of setting for the engine
|
||||
* @return bool True if the engine has been successfully initialized, false if not
|
||||
*/
|
||||
public function init($settings = array()) {
|
||||
$settings += array(
|
||||
'engine' => 'File',
|
||||
'path' => CACHE,
|
||||
'prefix' => 'cake_',
|
||||
'lock' => true,
|
||||
'serialize' => true,
|
||||
'isWindows' => false,
|
||||
'mask' => 0664
|
||||
);
|
||||
parent::init($settings);
|
||||
|
||||
if (DS === '\\') {
|
||||
$this->settings['isWindows'] = true;
|
||||
}
|
||||
if (substr($this->settings['path'], -1) !== DS) {
|
||||
$this->settings['path'] .= DS;
|
||||
}
|
||||
if (!empty($this->_groupPrefix)) {
|
||||
$this->_groupPrefix = str_replace('_', DS, $this->_groupPrefix);
|
||||
}
|
||||
return $this->_active();
|
||||
}
|
||||
|
||||
/**
|
||||
* Garbage collection. Permanently remove all expired and deleted data
|
||||
*
|
||||
* @param int $expires [optional] An expires timestamp, invalidating all data before.
|
||||
* @return bool True if garbage collection was successful, false on failure
|
||||
*/
|
||||
public function gc($expires = null) {
|
||||
return $this->clear(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data for key into cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param mixed $data Data to be cached
|
||||
* @param int $duration How long to cache the data, in seconds
|
||||
* @return bool True if the data was successfully cached, false on failure
|
||||
*/
|
||||
public function write($key, $data, $duration) {
|
||||
if ($data === '' || !$this->_init) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->_setKey($key, true) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$lineBreak = "\n";
|
||||
|
||||
if ($this->settings['isWindows']) {
|
||||
$lineBreak = "\r\n";
|
||||
}
|
||||
|
||||
if (!empty($this->settings['serialize'])) {
|
||||
if ($this->settings['isWindows']) {
|
||||
$data = str_replace('\\', '\\\\\\\\', serialize($data));
|
||||
} else {
|
||||
$data = serialize($data);
|
||||
}
|
||||
}
|
||||
|
||||
$expires = time() + $duration;
|
||||
$contents = $expires . $lineBreak . $data . $lineBreak;
|
||||
|
||||
if ($this->settings['lock']) {
|
||||
$this->_File->flock(LOCK_EX);
|
||||
}
|
||||
|
||||
$this->_File->rewind();
|
||||
$success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush();
|
||||
|
||||
if ($this->settings['lock']) {
|
||||
$this->_File->flock(LOCK_UN);
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function read($key) {
|
||||
if (!$this->_init || $this->_setKey($key) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->settings['lock']) {
|
||||
$this->_File->flock(LOCK_SH);
|
||||
}
|
||||
|
||||
$this->_File->rewind();
|
||||
$time = time();
|
||||
$cachetime = intval($this->_File->current());
|
||||
|
||||
if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
|
||||
if ($this->settings['lock']) {
|
||||
$this->_File->flock(LOCK_UN);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = '';
|
||||
$this->_File->next();
|
||||
while ($this->_File->valid()) {
|
||||
$data .= $this->_File->current();
|
||||
$this->_File->next();
|
||||
}
|
||||
|
||||
if ($this->settings['lock']) {
|
||||
$this->_File->flock(LOCK_UN);
|
||||
}
|
||||
|
||||
$data = trim($data);
|
||||
|
||||
if ($data !== '' && !empty($this->settings['serialize'])) {
|
||||
if ($this->settings['isWindows']) {
|
||||
$data = str_replace('\\\\\\\\', '\\', $data);
|
||||
}
|
||||
$data = unserialize((string)$data);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a key from the cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
|
||||
*/
|
||||
public function delete($key) {
|
||||
if ($this->_setKey($key) === false || !$this->_init) {
|
||||
return false;
|
||||
}
|
||||
$path = $this->_File->getRealPath();
|
||||
$this->_File = null;
|
||||
|
||||
//@codingStandardsIgnoreStart
|
||||
return @unlink($path);
|
||||
//@codingStandardsIgnoreEnd
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all values from the cache
|
||||
*
|
||||
* @param bool $check Optional - only delete expired cache items
|
||||
* @return bool True if the cache was successfully cleared, false otherwise
|
||||
*/
|
||||
public function clear($check) {
|
||||
if (!$this->_init) {
|
||||
return false;
|
||||
}
|
||||
$this->_File = null;
|
||||
|
||||
$threshold = $now = false;
|
||||
if ($check) {
|
||||
$now = time();
|
||||
$threshold = $now - $this->settings['duration'];
|
||||
}
|
||||
|
||||
$this->_clearDirectory($this->settings['path'], $now, $threshold);
|
||||
|
||||
$directory = new RecursiveDirectoryIterator($this->settings['path']);
|
||||
$contents = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
|
||||
$cleared = array();
|
||||
foreach ($contents as $path) {
|
||||
if ($path->isFile()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$path = $path->getRealPath() . DS;
|
||||
if (!in_array($path, $cleared)) {
|
||||
$this->_clearDirectory($path, $now, $threshold);
|
||||
$cleared[] = $path;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to clear a directory of matching files.
|
||||
*
|
||||
* @param string $path The path to search.
|
||||
* @param int $now The current timestamp
|
||||
* @param int $threshold Any file not modified after this value will be deleted.
|
||||
* @return void
|
||||
*/
|
||||
protected function _clearDirectory($path, $now, $threshold) {
|
||||
$prefixLength = strlen($this->settings['prefix']);
|
||||
|
||||
if (!is_dir($path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$dir = dir($path);
|
||||
while (($entry = $dir->read()) !== false) {
|
||||
if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) {
|
||||
continue;
|
||||
}
|
||||
$filePath = $path . $entry;
|
||||
if (!file_exists($filePath) || is_dir($filePath)) {
|
||||
continue;
|
||||
}
|
||||
$file = new SplFileObject($path . $entry, 'r');
|
||||
|
||||
if ($threshold) {
|
||||
$mtime = $file->getMTime();
|
||||
|
||||
if ($mtime > $threshold) {
|
||||
continue;
|
||||
}
|
||||
$expires = (int)$file->current();
|
||||
|
||||
if ($expires > $now) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if ($file->isFile()) {
|
||||
$filePath = $file->getRealPath();
|
||||
$file = null;
|
||||
|
||||
//@codingStandardsIgnoreStart
|
||||
@unlink($filePath);
|
||||
//@codingStandardsIgnoreEnd
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Not implemented
|
||||
*
|
||||
* @param string $key The key to decrement
|
||||
* @param int $offset The number to offset
|
||||
* @return void
|
||||
* @throws CacheException
|
||||
*/
|
||||
public function decrement($key, $offset = 1) {
|
||||
throw new CacheException(__d('cake_dev', 'Files cannot be atomically decremented.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Not implemented
|
||||
*
|
||||
* @param string $key The key to decrement
|
||||
* @param int $offset The number to offset
|
||||
* @return void
|
||||
* @throws CacheException
|
||||
*/
|
||||
public function increment($key, $offset = 1) {
|
||||
throw new CacheException(__d('cake_dev', 'Files cannot be atomically incremented.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current cache key this class is managing, and creates a writable SplFileObject
|
||||
* for the cache file the key is referring to.
|
||||
*
|
||||
* @param string $key The key
|
||||
* @param bool $createKey Whether the key should be created if it doesn't exists, or not
|
||||
* @return bool true if the cache key could be set, false otherwise
|
||||
*/
|
||||
protected function _setKey($key, $createKey = false) {
|
||||
$groups = null;
|
||||
if (!empty($this->_groupPrefix)) {
|
||||
$groups = vsprintf($this->_groupPrefix, $this->groups());
|
||||
}
|
||||
$dir = $this->settings['path'] . $groups;
|
||||
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0775, true);
|
||||
}
|
||||
$path = new SplFileInfo($dir . $key);
|
||||
|
||||
if (!$createKey && !$path->isFile()) {
|
||||
return false;
|
||||
}
|
||||
if (empty($this->_File) || $this->_File->getBaseName() !== $key) {
|
||||
$exists = file_exists($path->getPathname());
|
||||
try {
|
||||
$this->_File = $path->openFile('c+');
|
||||
} catch (Exception $e) {
|
||||
trigger_error($e->getMessage(), E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
unset($path);
|
||||
|
||||
if (!$exists && !chmod($this->_File->getPathname(), (int)$this->settings['mask'])) {
|
||||
trigger_error(__d(
|
||||
'cake_dev', 'Could not apply permission mask "%s" on cache file "%s"',
|
||||
array($this->_File->getPathname(), $this->settings['mask'])), E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine is cache directory is writable
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function _active() {
|
||||
$dir = new SplFileInfo($this->settings['path']);
|
||||
if (Configure::read('debug')) {
|
||||
$path = $dir->getPathname();
|
||||
if (!is_dir($path)) {
|
||||
mkdir($path, 0775, true);
|
||||
}
|
||||
}
|
||||
if ($this->_init && !($dir->isDir() && $dir->isWritable())) {
|
||||
$this->_init = false;
|
||||
trigger_error(__d('cake_dev', '%s is not writable', $this->settings['path']), E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
$key = Inflector::underscore(str_replace(array(DS, '/', '.', '<', '>', '?', ':', '|', '*', '"'), '_', strval($key)));
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively deletes all files under any directory named as $group
|
||||
*
|
||||
* @param string $group The group to clear.
|
||||
* @return bool success
|
||||
*/
|
||||
public function clearGroup($group) {
|
||||
$this->_File = null;
|
||||
$directoryIterator = new RecursiveDirectoryIterator($this->settings['path']);
|
||||
$contents = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST);
|
||||
foreach ($contents as $object) {
|
||||
$containsGroup = strpos($object->getPathName(), DS . $group . DS) !== false;
|
||||
$hasPrefix = true;
|
||||
if (strlen($this->settings['prefix']) !== 0) {
|
||||
$hasPrefix = strpos($object->getBaseName(), $this->settings['prefix']) === 0;
|
||||
}
|
||||
if ($object->isFile() && $containsGroup && $hasPrefix) {
|
||||
$path = $object->getPathName();
|
||||
$object = null;
|
||||
//@codingStandardsIgnoreStart
|
||||
@unlink($path);
|
||||
//@codingStandardsIgnoreEnd
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
292
lib/Cake/Cache/Engine/MemcacheEngine.php
Normal file
292
lib/Cake/Cache/Engine/MemcacheEngine.php
Normal file
|
@ -0,0 +1,292 @@
|
|||
<?php
|
||||
/**
|
||||
* Memcache storage engine for cache
|
||||
*
|
||||
* 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.Engine
|
||||
* @since CakePHP(tm) v 1.2.0.4933
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
* Memcache storage engine for cache. Memcache has some limitations in the amount of
|
||||
* control you have over expire times far in the future. See MemcacheEngine::write() for
|
||||
* more information.
|
||||
*
|
||||
* @package Cake.Cache.Engine
|
||||
* @deprecated You should use the Memcached adapter instead.
|
||||
*/
|
||||
class MemcacheEngine extends CacheEngine {
|
||||
|
||||
/**
|
||||
* Contains the compiled group names
|
||||
* (prefixed with the global configuration prefix)
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_compiledGroupNames = array();
|
||||
|
||||
/**
|
||||
* Memcache wrapper.
|
||||
*
|
||||
* @var Memcache
|
||||
*/
|
||||
protected $_Memcache = null;
|
||||
|
||||
/**
|
||||
* Settings
|
||||
*
|
||||
* - servers = string or array of memcache servers, default => 127.0.0.1. If an
|
||||
* array MemcacheEngine will use them as a pool.
|
||||
* - compress = boolean, default => false
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $settings = array();
|
||||
|
||||
/**
|
||||
* Initialize the Cache Engine
|
||||
*
|
||||
* Called automatically by the cache frontend
|
||||
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
||||
*
|
||||
* @param array $settings array of setting for the engine
|
||||
* @return bool True if the engine has been successfully initialized, false if not
|
||||
*/
|
||||
public function init($settings = array()) {
|
||||
if (!class_exists('Memcache')) {
|
||||
return false;
|
||||
}
|
||||
if (!isset($settings['prefix'])) {
|
||||
$settings['prefix'] = Inflector::slug(APP_DIR) . '_';
|
||||
}
|
||||
$settings += array(
|
||||
'engine' => 'Memcache',
|
||||
'servers' => array('127.0.0.1'),
|
||||
'compress' => false,
|
||||
'persistent' => true
|
||||
);
|
||||
parent::init($settings);
|
||||
|
||||
if ($this->settings['compress']) {
|
||||
$this->settings['compress'] = MEMCACHE_COMPRESSED;
|
||||
}
|
||||
if (is_string($this->settings['servers'])) {
|
||||
$this->settings['servers'] = array($this->settings['servers']);
|
||||
}
|
||||
if (!isset($this->_Memcache)) {
|
||||
$return = false;
|
||||
$this->_Memcache = new Memcache();
|
||||
foreach ($this->settings['servers'] as $server) {
|
||||
list($host, $port) = $this->_parseServerString($server);
|
||||
if ($this->_Memcache->addServer($host, $port, $this->settings['persistent'])) {
|
||||
$return = true;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the server address into the host/port. Handles both IPv6 and IPv4
|
||||
* addresses and Unix sockets
|
||||
*
|
||||
* @param string $server The server address string.
|
||||
* @return array Array containing host, port
|
||||
*/
|
||||
protected function _parseServerString($server) {
|
||||
if ($server[0] === 'u') {
|
||||
return array($server, 0);
|
||||
}
|
||||
if (substr($server, 0, 1) === '[') {
|
||||
$position = strpos($server, ']:');
|
||||
if ($position !== false) {
|
||||
$position++;
|
||||
}
|
||||
} else {
|
||||
$position = strpos($server, ':');
|
||||
}
|
||||
$port = 11211;
|
||||
$host = $server;
|
||||
if ($position !== false) {
|
||||
$host = substr($server, 0, $position);
|
||||
$port = substr($server, $position + 1);
|
||||
}
|
||||
return array($host, $port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data for key into cache. When using memcache as your cache engine
|
||||
* remember that the Memcache pecl extension does not support cache expiry times greater
|
||||
* than 30 days in the future. Any duration greater than 30 days will be treated as never expiring.
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param mixed $value Data to be cached
|
||||
* @param int $duration How long to cache the data, in seconds
|
||||
* @return bool True if the data was successfully cached, false on failure
|
||||
* @see http://php.net/manual/en/memcache.set.php
|
||||
*/
|
||||
public function write($key, $value, $duration) {
|
||||
if ($duration > 30 * DAY) {
|
||||
$duration = 0;
|
||||
}
|
||||
return $this->_Memcache->set($key, $value, $this->settings['compress'], $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
|
||||
*/
|
||||
public function read($key) {
|
||||
return $this->_Memcache->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the value of an integer cached key
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to increment
|
||||
* @return New incremented value, false otherwise
|
||||
* @throws CacheException when you try to increment with compress = true
|
||||
*/
|
||||
public function increment($key, $offset = 1) {
|
||||
if ($this->settings['compress']) {
|
||||
throw new CacheException(
|
||||
__d('cake_dev', 'Method %s not implemented for compressed cache in %s', 'increment()', __CLASS__)
|
||||
);
|
||||
}
|
||||
return $this->_Memcache->increment($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the value of an integer cached key
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to subtract
|
||||
* @return New decremented value, false otherwise
|
||||
* @throws CacheException when you try to decrement with compress = true
|
||||
*/
|
||||
public function decrement($key, $offset = 1) {
|
||||
if ($this->settings['compress']) {
|
||||
throw new CacheException(
|
||||
__d('cake_dev', 'Method %s not implemented for compressed cache in %s', 'decrement()', __CLASS__)
|
||||
);
|
||||
}
|
||||
return $this->_Memcache->decrement($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a key from the cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
|
||||
*/
|
||||
public function delete($key) {
|
||||
return $this->_Memcache->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all keys from the cache
|
||||
*
|
||||
* @param bool $check If true no deletes will occur and instead CakePHP will rely
|
||||
* on key TTL values.
|
||||
* @return bool True if the cache was successfully cleared, false otherwise
|
||||
*/
|
||||
public function clear($check) {
|
||||
if ($check) {
|
||||
return true;
|
||||
}
|
||||
foreach ($this->_Memcache->getExtendedStats('slabs', 0) as $slabs) {
|
||||
foreach (array_keys($slabs) as $slabId) {
|
||||
if (!is_numeric($slabId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($this->_Memcache->getExtendedStats('cachedump', $slabId, 0) as $stats) {
|
||||
if (!is_array($stats)) {
|
||||
continue;
|
||||
}
|
||||
foreach (array_keys($stats) as $key) {
|
||||
if (strpos($key, $this->settings['prefix']) === 0) {
|
||||
$this->_Memcache->delete($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to a server in connection pool
|
||||
*
|
||||
* @param string $host host ip address or name
|
||||
* @param int $port Server port
|
||||
* @return bool True if memcache server was connected
|
||||
*/
|
||||
public function connect($host, $port = 11211) {
|
||||
if ($this->_Memcache->getServerStatus($host, $port) === 0) {
|
||||
if ($this->_Memcache->connect($host, $port)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the `group value` for each of the configured groups
|
||||
* If the group initial value was not found, then it initializes
|
||||
* the group accordingly.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function groups() {
|
||||
if (empty($this->_compiledGroupNames)) {
|
||||
foreach ($this->settings['groups'] as $group) {
|
||||
$this->_compiledGroupNames[] = $this->settings['prefix'] . $group;
|
||||
}
|
||||
}
|
||||
|
||||
$groups = $this->_Memcache->get($this->_compiledGroupNames);
|
||||
if (count($groups) !== count($this->settings['groups'])) {
|
||||
foreach ($this->_compiledGroupNames as $group) {
|
||||
if (!isset($groups[$group])) {
|
||||
$this->_Memcache->set($group, 1, false, 0);
|
||||
$groups[$group] = 1;
|
||||
}
|
||||
}
|
||||
ksort($groups);
|
||||
}
|
||||
|
||||
$result = array();
|
||||
$groups = array_values($groups);
|
||||
foreach ($this->settings['groups'] as $i => $group) {
|
||||
$result[] = $group . $groups[$i];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the group value to simulate deletion of all keys under a group
|
||||
* old values will remain in storage until they expire.
|
||||
*
|
||||
* @param string $group The group to clear.
|
||||
* @return bool success
|
||||
*/
|
||||
public function clearGroup($group) {
|
||||
return (bool)$this->_Memcache->increment($this->settings['prefix'] . $group);
|
||||
}
|
||||
}
|
325
lib/Cake/Cache/Engine/MemcachedEngine.php
Normal file
325
lib/Cake/Cache/Engine/MemcachedEngine.php
Normal file
|
@ -0,0 +1,325 @@
|
|||
<?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
|
||||
* @since CakePHP(tm) v 2.5.0
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
* Memcached storage engine for cache. Memcached has some limitations in the amount of
|
||||
* control you have over expire times far in the future. See MemcachedEngine::write() for
|
||||
* more information.
|
||||
*
|
||||
* Main advantage of this Memcached engine over the memcached engine is
|
||||
* support of binary protocol, and igbibnary serialization
|
||||
* (if memcached extension compiled with --enable-igbinary)
|
||||
* Compressed keys can also be incremented/decremented
|
||||
*
|
||||
* @package Cake.Cache.Engine
|
||||
*/
|
||||
class MemcachedEngine extends CacheEngine {
|
||||
|
||||
/**
|
||||
* memcached wrapper.
|
||||
*
|
||||
* @var Memcache
|
||||
*/
|
||||
protected $_Memcached = null;
|
||||
|
||||
/**
|
||||
* Settings
|
||||
*
|
||||
* - servers = string or array of memcached servers, default => 127.0.0.1. If an
|
||||
* array MemcacheEngine will use them as a pool.
|
||||
* - compress = boolean, default => false
|
||||
* - persistent = string The name of the persistent connection. All configurations using
|
||||
* the same persistent value will share a single underlying connection.
|
||||
* - serialize = string, default => php. The serializer engine used to serialize data.
|
||||
* Available engines are php, igbinary and json. Beside php, the memcached extension
|
||||
* must be compiled with the appropriate serializer support.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $settings = array();
|
||||
|
||||
/**
|
||||
* List of available serializer engines
|
||||
*
|
||||
* Memcached must be compiled with json and igbinary support to use these engines
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_serializers = array(
|
||||
'igbinary' => Memcached::SERIALIZER_IGBINARY,
|
||||
'json' => Memcached::SERIALIZER_JSON,
|
||||
'php' => Memcached::SERIALIZER_PHP
|
||||
);
|
||||
|
||||
/**
|
||||
* Initialize the Cache Engine
|
||||
*
|
||||
* Called automatically by the cache frontend
|
||||
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
||||
*
|
||||
* @param array $settings array of setting for the engine
|
||||
* @return bool True if the engine has been successfully initialized, false if not
|
||||
* @throws CacheException when you try use authentication without Memcached compiled with SASL support
|
||||
*/
|
||||
public function init($settings = array()) {
|
||||
if (!class_exists('Memcached')) {
|
||||
return false;
|
||||
}
|
||||
if (!isset($settings['prefix'])) {
|
||||
$settings['prefix'] = Inflector::slug(APP_DIR) . '_';
|
||||
}
|
||||
|
||||
if (defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK) {
|
||||
$this->_serializers['msgpack'] = Memcached::SERIALIZER_MSGPACK;
|
||||
}
|
||||
|
||||
$settings += array(
|
||||
'engine' => 'Memcached',
|
||||
'servers' => array('127.0.0.1'),
|
||||
'compress' => false,
|
||||
'persistent' => false,
|
||||
'login' => null,
|
||||
'password' => null,
|
||||
'serialize' => 'php'
|
||||
);
|
||||
parent::init($settings);
|
||||
|
||||
if (!is_array($this->settings['servers'])) {
|
||||
$this->settings['servers'] = array($this->settings['servers']);
|
||||
}
|
||||
|
||||
if (isset($this->_Memcached)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->_Memcached = new Memcached($this->settings['persistent'] ? (string)$this->settings['persistent'] : null);
|
||||
$this->_setOptions();
|
||||
|
||||
if (count($this->_Memcached->getServerList())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$servers = array();
|
||||
foreach ($this->settings['servers'] as $server) {
|
||||
$servers[] = $this->_parseServerString($server);
|
||||
}
|
||||
|
||||
if (!$this->_Memcached->addServers($servers)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->settings['login'] !== null && $this->settings['password'] !== null) {
|
||||
if (!method_exists($this->_Memcached, 'setSaslAuthData')) {
|
||||
throw new CacheException(
|
||||
__d('cake_dev', 'Memcached extension is not build with SASL support')
|
||||
);
|
||||
}
|
||||
$this->_Memcached->setSaslAuthData($this->settings['login'], $this->settings['password']);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings the memcached instance
|
||||
*
|
||||
* @throws CacheException when the Memcached extension is not built with the desired serializer engine
|
||||
* @return void
|
||||
*/
|
||||
protected function _setOptions() {
|
||||
$this->_Memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
|
||||
|
||||
$serializer = strtolower($this->settings['serialize']);
|
||||
if (!isset($this->_serializers[$serializer])) {
|
||||
throw new CacheException(
|
||||
__d('cake_dev', '%s is not a valid serializer engine for Memcached', $serializer)
|
||||
);
|
||||
}
|
||||
|
||||
if ($serializer !== 'php' && !constant('Memcached::HAVE_' . strtoupper($serializer))) {
|
||||
throw new CacheException(
|
||||
__d('cake_dev', 'Memcached extension is not compiled with %s support', $serializer)
|
||||
);
|
||||
}
|
||||
|
||||
$this->_Memcached->setOption(Memcached::OPT_SERIALIZER, $this->_serializers[$serializer]);
|
||||
|
||||
// Check for Amazon ElastiCache instance
|
||||
if (defined('Memcached::OPT_CLIENT_MODE') && defined('Memcached::DYNAMIC_CLIENT_MODE')) {
|
||||
$this->_Memcached->setOption(Memcached::OPT_CLIENT_MODE, Memcached::DYNAMIC_CLIENT_MODE);
|
||||
}
|
||||
|
||||
$this->_Memcached->setOption(Memcached::OPT_COMPRESSION, (bool)$this->settings['compress']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the server address into the host/port. Handles both IPv6 and IPv4
|
||||
* addresses and Unix sockets
|
||||
*
|
||||
* @param string $server The server address string.
|
||||
* @return array Array containing host, port
|
||||
*/
|
||||
protected function _parseServerString($server) {
|
||||
if ($server[0] === 'u') {
|
||||
return array($server, 0);
|
||||
}
|
||||
if (substr($server, 0, 1) === '[') {
|
||||
$position = strpos($server, ']:');
|
||||
if ($position !== false) {
|
||||
$position++;
|
||||
}
|
||||
} else {
|
||||
$position = strpos($server, ':');
|
||||
}
|
||||
$port = 11211;
|
||||
$host = $server;
|
||||
if ($position !== false) {
|
||||
$host = substr($server, 0, $position);
|
||||
$port = substr($server, $position + 1);
|
||||
}
|
||||
return array($host, (int)$port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data for key into cache. When using memcached as your cache engine
|
||||
* remember that the Memcached pecl extension does not support cache expiry times greater
|
||||
* than 30 days in the future. Any duration greater than 30 days will be treated as never expiring.
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param mixed $value Data to be cached
|
||||
* @param int $duration How long to cache the data, in seconds
|
||||
* @return bool True if the data was successfully cached, false on failure
|
||||
* @see http://php.net/manual/en/memcache.set.php
|
||||
*/
|
||||
public function write($key, $value, $duration) {
|
||||
if ($duration > 30 * DAY) {
|
||||
$duration = 0;
|
||||
}
|
||||
|
||||
return $this->_Memcached->set($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
|
||||
*/
|
||||
public function read($key) {
|
||||
return $this->_Memcached->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the value of an integer cached key
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to increment
|
||||
* @return New incremented value, false otherwise
|
||||
* @throws CacheException when you try to increment with compress = true
|
||||
*/
|
||||
public function increment($key, $offset = 1) {
|
||||
return $this->_Memcached->increment($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the value of an integer cached key
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to subtract
|
||||
* @return New decremented value, false otherwise
|
||||
* @throws CacheException when you try to decrement with compress = true
|
||||
*/
|
||||
public function decrement($key, $offset = 1) {
|
||||
return $this->_Memcached->decrement($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a key from the cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
|
||||
*/
|
||||
public function delete($key) {
|
||||
return $this->_Memcached->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all keys from the cache
|
||||
*
|
||||
* @param bool $check If true no deletes will occur and instead CakePHP will rely
|
||||
* on key TTL values.
|
||||
* @return bool True if the cache was successfully cleared, false otherwise
|
||||
*/
|
||||
public function clear($check) {
|
||||
if ($check) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$keys = $this->_Memcached->getAllKeys();
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (strpos($key, $this->settings['prefix']) === 0) {
|
||||
$this->_Memcached->delete($key);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the `group value` for each of the configured groups
|
||||
* If the group initial value was not found, then it initializes
|
||||
* the group accordingly.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function groups() {
|
||||
if (empty($this->_compiledGroupNames)) {
|
||||
foreach ($this->settings['groups'] as $group) {
|
||||
$this->_compiledGroupNames[] = $this->settings['prefix'] . $group;
|
||||
}
|
||||
}
|
||||
|
||||
$groups = $this->_Memcached->getMulti($this->_compiledGroupNames);
|
||||
if (count($groups) !== count($this->settings['groups'])) {
|
||||
foreach ($this->_compiledGroupNames as $group) {
|
||||
if (!isset($groups[$group])) {
|
||||
$this->_Memcached->set($group, 1, 0);
|
||||
$groups[$group] = 1;
|
||||
}
|
||||
}
|
||||
ksort($groups);
|
||||
}
|
||||
|
||||
$result = array();
|
||||
$groups = array_values($groups);
|
||||
foreach ($this->settings['groups'] as $i => $group) {
|
||||
$result[] = $group . $groups[$i];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the group value to simulate deletion of all keys under a group
|
||||
* old values will remain in storage until they expire.
|
||||
*
|
||||
* @param string $group The group to clear.
|
||||
* @return bool success
|
||||
*/
|
||||
public function clearGroup($group) {
|
||||
return (bool)$this->_Memcached->increment($this->settings['prefix'] . $group);
|
||||
}
|
||||
}
|
231
lib/Cake/Cache/Engine/RedisEngine.php
Normal file
231
lib/Cake/Cache/Engine/RedisEngine.php
Normal file
|
@ -0,0 +1,231 @@
|
|||
<?php
|
||||
/**
|
||||
* Redis storage engine for cache
|
||||
*
|
||||
* 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.Engine
|
||||
* @since CakePHP(tm) v 2.2
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
* Redis storage engine for cache.
|
||||
*
|
||||
* @package Cake.Cache.Engine
|
||||
*/
|
||||
class RedisEngine extends CacheEngine {
|
||||
|
||||
/**
|
||||
* Redis wrapper.
|
||||
*
|
||||
* @var Redis
|
||||
*/
|
||||
protected $_Redis = null;
|
||||
|
||||
/**
|
||||
* Settings
|
||||
*
|
||||
* - server = string URL or ip to the Redis server host
|
||||
* - database = integer database number to use for connection
|
||||
* - port = integer port number to the Redis server (default: 6379)
|
||||
* - timeout = float timeout in seconds (default: 0)
|
||||
* - persistent = boolean Connects to the Redis server with a persistent connection (default: true)
|
||||
* - unix_socket = path to the unix socket file (default: false)
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $settings = array();
|
||||
|
||||
/**
|
||||
* Initialize the Cache Engine
|
||||
*
|
||||
* Called automatically by the cache frontend
|
||||
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
||||
*
|
||||
* @param array $settings array of setting for the engine
|
||||
* @return bool True if the engine has been successfully initialized, false if not
|
||||
*/
|
||||
public function init($settings = array()) {
|
||||
if (!class_exists('Redis')) {
|
||||
return false;
|
||||
}
|
||||
parent::init(array_merge(array(
|
||||
'engine' => 'Redis',
|
||||
'prefix' => null,
|
||||
'server' => '127.0.0.1',
|
||||
'database' => 0,
|
||||
'port' => 6379,
|
||||
'password' => false,
|
||||
'timeout' => 0,
|
||||
'persistent' => true,
|
||||
'unix_socket' => false
|
||||
), $settings)
|
||||
);
|
||||
|
||||
return $this->_connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to a Redis server
|
||||
*
|
||||
* @return bool True if Redis server was connected
|
||||
*/
|
||||
protected function _connect() {
|
||||
$return = false;
|
||||
try {
|
||||
$this->_Redis = new Redis();
|
||||
if (!empty($this->settings['unix_socket'])) {
|
||||
$return = $this->_Redis->connect($this->settings['unix_socket']);
|
||||
} elseif (empty($this->settings['persistent'])) {
|
||||
$return = $this->_Redis->connect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
|
||||
} else {
|
||||
$persistentId = $this->settings['port'] . $this->settings['timeout'] . $this->settings['database'];
|
||||
$return = $this->_Redis->pconnect($this->settings['server'], $this->settings['port'], $this->settings['timeout'], $persistentId);
|
||||
}
|
||||
} catch (RedisException $e) {
|
||||
return false;
|
||||
}
|
||||
if ($return && $this->settings['password']) {
|
||||
$return = $this->_Redis->auth($this->settings['password']);
|
||||
}
|
||||
if ($return) {
|
||||
$return = $this->_Redis->select($this->settings['database']);
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data for key into cache.
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param mixed $value Data to be cached
|
||||
* @param int $duration How long to cache the data, in seconds
|
||||
* @return bool True if the data was successfully cached, false on failure
|
||||
*/
|
||||
public function write($key, $value, $duration) {
|
||||
if (!is_int($value)) {
|
||||
$value = serialize($value);
|
||||
}
|
||||
if ($duration === 0) {
|
||||
return $this->_Redis->set($key, $value);
|
||||
}
|
||||
|
||||
return $this->_Redis->setex($key, $duration, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function read($key) {
|
||||
$value = $this->_Redis->get($key);
|
||||
if (ctype_digit($value)) {
|
||||
$value = (int)$value;
|
||||
}
|
||||
if ($value !== false && is_string($value)) {
|
||||
$value = unserialize($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the value of an integer cached key
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to increment
|
||||
* @return New incremented value, false otherwise
|
||||
* @throws CacheException when you try to increment with compress = true
|
||||
*/
|
||||
public function increment($key, $offset = 1) {
|
||||
return (int)$this->_Redis->incrBy($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the value of an integer cached key
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to subtract
|
||||
* @return New decremented value, false otherwise
|
||||
* @throws CacheException when you try to decrement with compress = true
|
||||
*/
|
||||
public function decrement($key, $offset = 1) {
|
||||
return (int)$this->_Redis->decrBy($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a key from the cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
|
||||
*/
|
||||
public function delete($key) {
|
||||
return $this->_Redis->delete($key) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all keys from the cache
|
||||
*
|
||||
* @param bool $check Whether or not expiration keys should be checked. If
|
||||
* true, no keys will be removed as cache will rely on redis TTL's.
|
||||
* @return bool True if the cache was successfully cleared, false otherwise
|
||||
*/
|
||||
public function clear($check) {
|
||||
if ($check) {
|
||||
return true;
|
||||
}
|
||||
$keys = $this->_Redis->getKeys($this->settings['prefix'] . '*');
|
||||
$this->_Redis->del($keys);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the `group value` for each of the configured groups
|
||||
* If the group initial value was not found, then it initializes
|
||||
* the group accordingly.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function groups() {
|
||||
$result = array();
|
||||
foreach ($this->settings['groups'] as $group) {
|
||||
$value = $this->_Redis->get($this->settings['prefix'] . $group);
|
||||
if (!$value) {
|
||||
$value = 1;
|
||||
$this->_Redis->set($this->settings['prefix'] . $group, $value);
|
||||
}
|
||||
$result[] = $group . $value;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the group value to simulate deletion of all keys under a group
|
||||
* old values will remain in storage until they expire.
|
||||
*
|
||||
* @param string $group The group name to clear.
|
||||
* @return bool success
|
||||
*/
|
||||
public function clearGroup($group) {
|
||||
return (bool)$this->_Redis->incr($this->settings['prefix'] . $group);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the redis server
|
||||
*/
|
||||
public function __destruct() {
|
||||
if (!$this->settings['persistent']) {
|
||||
$this->_Redis->close();
|
||||
}
|
||||
}
|
||||
}
|
191
lib/Cake/Cache/Engine/WincacheEngine.php
Normal file
191
lib/Cake/Cache/Engine/WincacheEngine.php
Normal file
|
@ -0,0 +1,191 @@
|
|||
<?php
|
||||
/**
|
||||
* Wincache storage engine for cache.
|
||||
*
|
||||
* Supports wincache 1.1.0 and higher.
|
||||
*
|
||||
* 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.Engine
|
||||
* @since CakePHP(tm) v 1.2.0.4933
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
* Wincache storage engine for cache
|
||||
*
|
||||
* @package Cake.Cache.Engine
|
||||
*/
|
||||
class WincacheEngine extends CacheEngine {
|
||||
|
||||
/**
|
||||
* Contains the compiled group names
|
||||
* (prefixed with the global configuration prefix)
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_compiledGroupNames = array();
|
||||
|
||||
/**
|
||||
* Initialize the Cache Engine
|
||||
*
|
||||
* Called automatically by the cache frontend
|
||||
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
||||
*
|
||||
* @param array $settings array of setting for the engine
|
||||
* @return bool True if the engine has been successfully initialized, false if not
|
||||
* @see CacheEngine::__defaults
|
||||
*/
|
||||
public function init($settings = array()) {
|
||||
if (!isset($settings['prefix'])) {
|
||||
$settings['prefix'] = Inflector::slug(APP_DIR) . '_';
|
||||
}
|
||||
$settings += array('engine' => 'Wincache');
|
||||
parent::init($settings);
|
||||
return function_exists('wincache_ucache_info');
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data for key into cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param mixed $value Data to be cached
|
||||
* @param int $duration How long to cache the data, in seconds
|
||||
* @return bool True if the data was successfully cached, false on failure
|
||||
*/
|
||||
public function write($key, $value, $duration) {
|
||||
$expires = time() + $duration;
|
||||
|
||||
$data = array(
|
||||
$key . '_expires' => $expires,
|
||||
$key => $value
|
||||
);
|
||||
$result = wincache_ucache_set($data, null, $duration);
|
||||
return empty($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function read($key) {
|
||||
$time = time();
|
||||
$cachetime = intval(wincache_ucache_get($key . '_expires'));
|
||||
if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
|
||||
return false;
|
||||
}
|
||||
return wincache_ucache_get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the value of an integer cached key
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to increment
|
||||
* @return New incremented value, false otherwise
|
||||
*/
|
||||
public function increment($key, $offset = 1) {
|
||||
return wincache_ucache_inc($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the value of an integer cached key
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to subtract
|
||||
* @return New decremented value, false otherwise
|
||||
*/
|
||||
public function decrement($key, $offset = 1) {
|
||||
return wincache_ucache_dec($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a key from the cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
|
||||
*/
|
||||
public function delete($key) {
|
||||
return wincache_ucache_delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all keys from the cache. This will clear every
|
||||
* item in the cache matching the cache config prefix.
|
||||
*
|
||||
* @param bool $check If true, nothing will be cleared, as entries will
|
||||
* naturally expire in wincache..
|
||||
* @return bool True Returns true.
|
||||
*/
|
||||
public function clear($check) {
|
||||
if ($check) {
|
||||
return true;
|
||||
}
|
||||
$info = wincache_ucache_info();
|
||||
$cacheKeys = $info['ucache_entries'];
|
||||
unset($info);
|
||||
foreach ($cacheKeys as $key) {
|
||||
if (strpos($key['key_name'], $this->settings['prefix']) === 0) {
|
||||
wincache_ucache_delete($key['key_name']);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the `group value` for each of the configured groups
|
||||
* If the group initial value was not found, then it initializes
|
||||
* the group accordingly.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function groups() {
|
||||
if (empty($this->_compiledGroupNames)) {
|
||||
foreach ($this->settings['groups'] as $group) {
|
||||
$this->_compiledGroupNames[] = $this->settings['prefix'] . $group;
|
||||
}
|
||||
}
|
||||
|
||||
$groups = wincache_ucache_get($this->_compiledGroupNames);
|
||||
if (count($groups) !== count($this->settings['groups'])) {
|
||||
foreach ($this->_compiledGroupNames as $group) {
|
||||
if (!isset($groups[$group])) {
|
||||
wincache_ucache_set($group, 1);
|
||||
$groups[$group] = 1;
|
||||
}
|
||||
}
|
||||
ksort($groups);
|
||||
}
|
||||
|
||||
$result = array();
|
||||
$groups = array_values($groups);
|
||||
foreach ($this->settings['groups'] as $i => $group) {
|
||||
$result[] = $group . $groups[$i];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the group value to simulate deletion of all keys under a group
|
||||
* old values will remain in storage until they expire.
|
||||
*
|
||||
* @param string $group The group to clear.
|
||||
* @return bool success
|
||||
*/
|
||||
public function clearGroup($group) {
|
||||
$success = null;
|
||||
wincache_ucache_inc($this->settings['prefix'] . $group, 1, $success);
|
||||
return $success;
|
||||
}
|
||||
|
||||
}
|
210
lib/Cake/Cache/Engine/XcacheEngine.php
Normal file
210
lib/Cake/Cache/Engine/XcacheEngine.php
Normal file
|
@ -0,0 +1,210 @@
|
|||
<?php
|
||||
/**
|
||||
* Xcache storage engine for cache.
|
||||
*
|
||||
* 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.Engine
|
||||
* @since CakePHP(tm) v 1.2.0.4947
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
* Xcache storage engine for cache
|
||||
*
|
||||
* @link http://trac.lighttpd.net/xcache/ Xcache
|
||||
* @package Cake.Cache.Engine
|
||||
*/
|
||||
class XcacheEngine extends CacheEngine {
|
||||
|
||||
/**
|
||||
* Settings
|
||||
*
|
||||
* - PHP_AUTH_USER = xcache.admin.user, default cake
|
||||
* - PHP_AUTH_PW = xcache.admin.password, default cake
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $settings = array();
|
||||
|
||||
/**
|
||||
* Initialize the Cache Engine
|
||||
*
|
||||
* Called automatically by the cache frontend
|
||||
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
||||
*
|
||||
* @param array $settings array of setting for the engine
|
||||
* @return bool True if the engine has been successfully initialized, false if not
|
||||
*/
|
||||
public function init($settings = array()) {
|
||||
if (php_sapi_name() !== 'cli') {
|
||||
parent::init(array_merge(array(
|
||||
'engine' => 'Xcache',
|
||||
'prefix' => Inflector::slug(APP_DIR) . '_',
|
||||
'PHP_AUTH_USER' => 'user',
|
||||
'PHP_AUTH_PW' => 'password'
|
||||
), $settings)
|
||||
);
|
||||
return function_exists('xcache_info');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data for key into cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param mixed $value Data to be cached
|
||||
* @param int $duration How long to cache the data, in seconds
|
||||
* @return bool True if the data was successfully cached, false on failure
|
||||
*/
|
||||
public function write($key, $value, $duration) {
|
||||
$expires = time() + $duration;
|
||||
xcache_set($key . '_expires', $expires, $duration);
|
||||
return xcache_set($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
|
||||
*/
|
||||
public function read($key) {
|
||||
if (xcache_isset($key)) {
|
||||
$time = time();
|
||||
$cachetime = intval(xcache_get($key . '_expires'));
|
||||
if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
|
||||
return false;
|
||||
}
|
||||
return xcache_get($key);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the value of an integer cached key
|
||||
* If the cache key is not an integer it will be treated as 0
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to increment
|
||||
* @return New incremented value, false otherwise
|
||||
*/
|
||||
public function increment($key, $offset = 1) {
|
||||
return xcache_inc($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the value of an integer cached key.
|
||||
* If the cache key is not an integer it will be treated as 0
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param int $offset How much to subtract
|
||||
* @return New decremented value, false otherwise
|
||||
*/
|
||||
public function decrement($key, $offset = 1) {
|
||||
return xcache_dec($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a key from the cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
|
||||
*/
|
||||
public function delete($key) {
|
||||
return xcache_unset($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all keys from the cache
|
||||
*
|
||||
* @param bool $check If true no deletes will occur and instead CakePHP will rely
|
||||
* on key TTL values.
|
||||
* @return bool True if the cache was successfully cleared, false otherwise
|
||||
*/
|
||||
public function clear($check) {
|
||||
$this->_auth();
|
||||
$max = xcache_count(XC_TYPE_VAR);
|
||||
for ($i = 0; $i < $max; $i++) {
|
||||
xcache_clear_cache(XC_TYPE_VAR, $i);
|
||||
}
|
||||
$this->_auth(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the `group value` for each of the configured groups
|
||||
* If the group initial value was not found, then it initializes
|
||||
* the group accordingly.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function groups() {
|
||||
$result = array();
|
||||
foreach ($this->settings['groups'] as $group) {
|
||||
$value = xcache_get($this->settings['prefix'] . $group);
|
||||
if (!$value) {
|
||||
$value = 1;
|
||||
xcache_set($this->settings['prefix'] . $group, $value, 0);
|
||||
}
|
||||
$result[] = $group . $value;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the group value to simulate deletion of all keys under a group
|
||||
* old values will remain in storage until they expire.
|
||||
*
|
||||
* @param string $group The group to clear.
|
||||
* @return bool success
|
||||
*/
|
||||
public function clearGroup($group) {
|
||||
return (bool)xcache_inc($this->settings['prefix'] . $group, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates and reverses $_SERVER authentication values
|
||||
* Makes necessary changes (and reverting them back) in $_SERVER
|
||||
*
|
||||
* This has to be done because xcache_clear_cache() needs to pass Basic Http Auth
|
||||
* (see xcache.admin configuration settings)
|
||||
*
|
||||
* @param bool $reverse Revert changes
|
||||
* @return void
|
||||
*/
|
||||
protected function _auth($reverse = false) {
|
||||
static $backup = array();
|
||||
$keys = array('PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password');
|
||||
foreach ($keys as $key => $setting) {
|
||||
if ($reverse) {
|
||||
if (isset($backup[$key])) {
|
||||
$_SERVER[$key] = $backup[$key];
|
||||
unset($backup[$key]);
|
||||
} else {
|
||||
unset($_SERVER[$key]);
|
||||
}
|
||||
} else {
|
||||
$value = env($key);
|
||||
if (!empty($value)) {
|
||||
$backup[$key] = $value;
|
||||
}
|
||||
if (!empty($this->settings[$setting])) {
|
||||
$_SERVER[$key] = $this->settings[$setting];
|
||||
} elseif (!empty($this->settings[$key])) {
|
||||
$_SERVER[$key] = $this->settings[$key];
|
||||
} else {
|
||||
$_SERVER[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue