a grand renaming so that the most significant portion of the name comes first
This commit is contained in:
123
php-practice/web-cake/html/cake/libs/cache/apc.php
vendored
Normal file
123
php-practice/web-cake/html/cake/libs/cache/apc.php
vendored
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
/**
|
||||
* APC storage engine for cache.
|
||||
*
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.cache
|
||||
* @since CakePHP(tm) v 1.2.0.4933
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
/**
|
||||
* APC storage engine for cache
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.cache
|
||||
*/
|
||||
class ApcEngine extends CacheEngine {
|
||||
|
||||
/**
|
||||
* Initialize the Cache Engine
|
||||
*
|
||||
* Called automatically by the cache frontend
|
||||
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
||||
*
|
||||
* @param array $setting array of setting for the engine
|
||||
* @return boolean True if the engine has been successfully initialized, false if not
|
||||
* @see CacheEngine::__defaults
|
||||
* @access public
|
||||
*/
|
||||
function init($settings = array()) {
|
||||
parent::init(array_merge(array('engine' => 'Apc', 'prefix' => Inflector::slug(APP_DIR) . '_'), $settings));
|
||||
return function_exists('apc_cache_info');
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data for key into cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param mixed $value Data to be cached
|
||||
* @param integer $duration How long to cache the data, in seconds
|
||||
* @return boolean True if the data was succesfully cached, false on failure
|
||||
* @access public
|
||||
*/
|
||||
function write($key, &$value, $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
|
||||
* @access public
|
||||
*/
|
||||
function read($key) {
|
||||
$time = time();
|
||||
$cachetime = intval(apc_fetch($key.'_expires'));
|
||||
if ($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 integer $offset How much to increment
|
||||
* @param integer $duration How long to cache the data, in seconds
|
||||
* @return New incremented value, false otherwise
|
||||
* @access 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 integer $offset How much to substract
|
||||
* @param integer $duration How long to cache the data, in seconds
|
||||
* @return New decremented value, false otherwise
|
||||
* @access 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 boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
|
||||
* @access public
|
||||
*/
|
||||
function delete($key) {
|
||||
return apc_delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all keys from the cache
|
||||
*
|
||||
* @return boolean True if the cache was succesfully cleared, false otherwise
|
||||
* @access public
|
||||
*/
|
||||
function clear() {
|
||||
return apc_clear_cache('user');
|
||||
}
|
||||
}
|
272
php-practice/web-cake/html/cake/libs/cache/file.php
vendored
Normal file
272
php-practice/web-cake/html/cake/libs/cache/file.php
vendored
Normal file
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
/**
|
||||
* File Storage engine for cache
|
||||
*
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.cache
|
||||
* @since CakePHP(tm) v 1.2.0.4933
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
if (!class_exists('File')) {
|
||||
require LIBS . 'file.php';
|
||||
}
|
||||
/**
|
||||
* File Storage engine for cache
|
||||
*
|
||||
* @todo use the File and Folder classes (if it's not a too big performance hit)
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.cache
|
||||
*/
|
||||
class FileEngine extends CacheEngine {
|
||||
|
||||
/**
|
||||
* Instance of File class
|
||||
*
|
||||
* @var File
|
||||
* @access protected
|
||||
*/
|
||||
var $_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 => false
|
||||
* - serialize = serialize the data, default => true
|
||||
*
|
||||
* @var array
|
||||
* @see CacheEngine::__defaults
|
||||
* @access public
|
||||
*/
|
||||
var $settings = array();
|
||||
|
||||
/**
|
||||
* True unless FileEngine::__active(); fails
|
||||
*
|
||||
* @var boolean
|
||||
* @access protected
|
||||
*/
|
||||
var $_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 $setting array of setting for the engine
|
||||
* @return boolean True if the engine has been successfully initialized, false if not
|
||||
* @access public
|
||||
*/
|
||||
function init($settings = array()) {
|
||||
parent::init(array_merge(
|
||||
array(
|
||||
'engine' => 'File', 'path' => CACHE, 'prefix'=> 'cake_', 'lock'=> false,
|
||||
'serialize'=> true, 'isWindows' => false
|
||||
),
|
||||
$settings
|
||||
));
|
||||
if (!isset($this->_File)) {
|
||||
$this->_File =& new File($this->settings['path'] . DS . 'cake');
|
||||
}
|
||||
|
||||
if (DIRECTORY_SEPARATOR === '\\') {
|
||||
$this->settings['isWindows'] = true;
|
||||
}
|
||||
|
||||
$path = $this->_File->Folder->cd($this->settings['path']);
|
||||
if ($path) {
|
||||
$this->settings['path'] = $path;
|
||||
}
|
||||
return $this->__active();
|
||||
}
|
||||
|
||||
/**
|
||||
* Garbage collection. Permanently remove all expired and deleted data
|
||||
*
|
||||
* @return boolean True if garbage collection was succesful, false on failure
|
||||
* @access public
|
||||
*/
|
||||
function gc() {
|
||||
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 mixed $duration How long to cache the data, in seconds
|
||||
* @return boolean True if the data was succesfully cached, false on failure
|
||||
* @access public
|
||||
*/
|
||||
function write($key, &$data, $duration) {
|
||||
if ($data === '' || !$this->_init) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->_setKey($key) === 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);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->settings['lock']) {
|
||||
$this->_File->lock = true;
|
||||
}
|
||||
$expires = time() + $duration;
|
||||
$contents = $expires . $lineBreak . $data . $lineBreak;
|
||||
$success = $this->_File->write($contents);
|
||||
$this->_File->close();
|
||||
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
|
||||
* @access public
|
||||
*/
|
||||
function read($key) {
|
||||
if ($this->_setKey($key) === false || !$this->_init || !$this->_File->exists()) {
|
||||
return false;
|
||||
}
|
||||
if ($this->settings['lock']) {
|
||||
$this->_File->lock = true;
|
||||
}
|
||||
$time = time();
|
||||
$cachetime = intval($this->_File->read(11));
|
||||
|
||||
if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
|
||||
$this->_File->close();
|
||||
return false;
|
||||
}
|
||||
$data = $this->_File->read(true);
|
||||
|
||||
if ($data !== '' && !empty($this->settings['serialize'])) {
|
||||
if ($this->settings['isWindows']) {
|
||||
$data = str_replace('\\\\\\\\', '\\', $data);
|
||||
}
|
||||
$data = unserialize((string)$data);
|
||||
}
|
||||
$this->_File->close();
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @access public
|
||||
*/
|
||||
function delete($key) {
|
||||
if ($this->_setKey($key) === false || !$this->_init) {
|
||||
return false;
|
||||
}
|
||||
return $this->_File->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all values from the cache
|
||||
*
|
||||
* @param boolean $check Optional - only delete expired cache items
|
||||
* @return boolean True if the cache was succesfully cleared, false otherwise
|
||||
* @access public
|
||||
*/
|
||||
function clear($check) {
|
||||
if (!$this->_init) {
|
||||
return false;
|
||||
}
|
||||
$dir = dir($this->settings['path']);
|
||||
if ($check) {
|
||||
$now = time();
|
||||
$threshold = $now - $this->settings['duration'];
|
||||
}
|
||||
$prefixLength = strlen($this->settings['prefix']);
|
||||
while (($entry = $dir->read()) !== false) {
|
||||
if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) {
|
||||
continue;
|
||||
}
|
||||
if ($this->_setKey($entry) === false) {
|
||||
continue;
|
||||
}
|
||||
if ($check) {
|
||||
$mtime = $this->_File->lastChange();
|
||||
|
||||
if ($mtime === false || $mtime > $threshold) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$expires = $this->_File->read(11);
|
||||
$this->_File->close();
|
||||
|
||||
if ($expires > $now) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$this->_File->delete();
|
||||
}
|
||||
$dir->close();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get absolute file for a given key
|
||||
*
|
||||
* @param string $key The key
|
||||
* @return mixed Absolute cache file for the given key or false if erroneous
|
||||
* @access private
|
||||
*/
|
||||
function _setKey($key) {
|
||||
$this->_File->Folder->cd($this->settings['path']);
|
||||
if ($key !== $this->_File->name) {
|
||||
$this->_File->name = $key;
|
||||
$this->_File->path = null;
|
||||
}
|
||||
if (!$this->_File->Folder->inPath($this->_File->pwd(), true)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine is cache directory is writable
|
||||
*
|
||||
* @return boolean
|
||||
* @access private
|
||||
*/
|
||||
function __active() {
|
||||
if ($this->_init && !is_writable($this->settings['path'])) {
|
||||
$this->_init = false;
|
||||
trigger_error(sprintf(__('%s is not writable', true), $this->settings['path']), E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
217
php-practice/web-cake/html/cake/libs/cache/memcache.php
vendored
Normal file
217
php-practice/web-cake/html/cake/libs/cache/memcache.php
vendored
Normal file
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
/**
|
||||
* Memcache storage engine for cache
|
||||
*
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.cache
|
||||
* @since CakePHP(tm) v 1.2.0.4933
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @subpackage cake.cake.libs.cache
|
||||
*/
|
||||
class MemcacheEngine extends CacheEngine {
|
||||
|
||||
/**
|
||||
* Memcache wrapper.
|
||||
*
|
||||
* @var Memcache
|
||||
* @access private
|
||||
*/
|
||||
var $__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
|
||||
* @access public
|
||||
*/
|
||||
var $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 $setting array of setting for the engine
|
||||
* @return boolean True if the engine has been successfully initialized, false if not
|
||||
* @access public
|
||||
*/
|
||||
function init($settings = array()) {
|
||||
if (!class_exists('Memcache')) {
|
||||
return false;
|
||||
}
|
||||
parent::init(array_merge(array(
|
||||
'engine'=> 'Memcache',
|
||||
'prefix' => Inflector::slug(APP_DIR) . '_',
|
||||
'servers' => array('127.0.0.1'),
|
||||
'compress'=> false
|
||||
), $settings)
|
||||
);
|
||||
|
||||
if ($this->settings['compress']) {
|
||||
$this->settings['compress'] = MEMCACHE_COMPRESSED;
|
||||
}
|
||||
if (!is_array($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)) {
|
||||
$return = true;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the server address into the host/port. Handles both IPv6 and IPv4
|
||||
* addresses
|
||||
*
|
||||
* @param string $server The server address string.
|
||||
* @return array Array containing host, port
|
||||
*/
|
||||
function _parseServerString($server) {
|
||||
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. If you wish to create cache entries that do not expire, set the duration
|
||||
* to `0` in your cache configuration.
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param mixed $value Data to be cached
|
||||
* @param integer $duration How long to cache the data, in seconds
|
||||
* @return boolean True if the data was succesfully cached, false on failure
|
||||
* @see http://php.net/manual/en/memcache.set.php
|
||||
* @access public
|
||||
*/
|
||||
function write($key, &$value, $duration) {
|
||||
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
|
||||
* @access 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 integer $offset How much to increment
|
||||
* @param integer $duration How long to cache the data, in seconds
|
||||
* @return New incremented value, false otherwise
|
||||
* @access public
|
||||
*/
|
||||
function increment($key, $offset = 1) {
|
||||
if ($this->settings['compress']) {
|
||||
trigger_error(sprintf(__('Method increment() not implemented for compressed cache in %s', true), get_class($this)), E_USER_ERROR);
|
||||
}
|
||||
return $this->__Memcache->increment($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the value of an integer cached key
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param integer $offset How much to substract
|
||||
* @param integer $duration How long to cache the data, in seconds
|
||||
* @return New decremented value, false otherwise
|
||||
* @access public
|
||||
*/
|
||||
function decrement($key, $offset = 1) {
|
||||
if ($this->settings['compress']) {
|
||||
trigger_error(sprintf(__('Method decrement() not implemented for compressed cache in %s', true), get_class($this)), E_USER_ERROR);
|
||||
}
|
||||
return $this->__Memcache->decrement($key, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @access public
|
||||
*/
|
||||
function delete($key) {
|
||||
return $this->__Memcache->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all keys from the cache
|
||||
*
|
||||
* @return boolean True if the cache was succesfully cleared, false otherwise
|
||||
* @access public
|
||||
*/
|
||||
function clear() {
|
||||
return $this->__Memcache->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to a server in connection pool
|
||||
*
|
||||
* @param string $host host ip address or name
|
||||
* @param integer $port Server port
|
||||
* @return boolean True if memcache server was connected
|
||||
* @access 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;
|
||||
}
|
||||
}
|
183
php-practice/web-cake/html/cake/libs/cache/xcache.php
vendored
Normal file
183
php-practice/web-cake/html/cake/libs/cache/xcache.php
vendored
Normal file
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
/**
|
||||
* Xcache storage engine for cache.
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.cache
|
||||
* @since CakePHP(tm) v 1.2.0.4947
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Xcache storage engine for cache
|
||||
*
|
||||
* @link http://trac.lighttpd.net/xcache/ Xcache
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.cache
|
||||
*/
|
||||
class XcacheEngine extends CacheEngine {
|
||||
|
||||
/**
|
||||
* Settings
|
||||
*
|
||||
* - PHP_AUTH_USER = xcache.admin.user, default cake
|
||||
* - PHP_AUTH_PW = xcache.admin.password, default cake
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $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 $setting array of setting for the engine
|
||||
* @return boolean True if the engine has been successfully initialized, false if not
|
||||
* @access public
|
||||
*/
|
||||
function init($settings) {
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data for key into cache
|
||||
*
|
||||
* @param string $key Identifier for the data
|
||||
* @param mixed $value Data to be cached
|
||||
* @param integer $duration How long to cache the data, in seconds
|
||||
* @return boolean True if the data was succesfully cached, false on failure
|
||||
* @access 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
|
||||
* @access 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 integer $offset How much to increment
|
||||
* @param integer $duration How long to cache the data, in seconds
|
||||
* @return New incremented value, false otherwise
|
||||
* @access 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 integer $offset How much to substract
|
||||
* @param integer $duration How long to cache the data, in seconds
|
||||
* @return New decremented value, false otherwise
|
||||
* @access 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 boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
|
||||
* @access public
|
||||
*/
|
||||
function delete($key) {
|
||||
return xcache_unset($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all keys from the cache
|
||||
*
|
||||
* @return boolean True if the cache was succesfully cleared, false otherwise
|
||||
* @access public
|
||||
*/
|
||||
function clear() {
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 boolean Revert changes
|
||||
* @access private
|
||||
*/
|
||||
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];
|
||||
} else if (!empty($this->settings[$key])) {
|
||||
$_SERVER[$key] = $this->settings[$key];
|
||||
} else {
|
||||
$_SERVER[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user