Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/kernel/utility/cache.php
Show First 20 Lines • Show All 85 Lines • ▼ Show 20 Line(s) | |||||
* Site key value | * Site key value | ||||
* Prepended to each cached key name | * Prepended to each cached key name | ||||
* | * | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
var $siteKeyValue = null; | var $siteKeyValue = null; | ||||
/** | /** | ||||
* Cache prefix. | |||||
* | |||||
* @var string | |||||
*/ | |||||
protected $cachePrefix = ''; | |||||
/** | |||||
* Maximum cache duration. | |||||
* | |||||
* @var integer | |||||
*/ | |||||
protected $maxCacheDuration; | |||||
/** | |||||
* Creates cache manager | * Creates cache manager | ||||
* | * | ||||
* @access public | * @param integer $max_cache_duration Maximum cache duration. | ||||
*/ | */ | ||||
public function __construct() | public function __construct($max_cache_duration) | ||||
{ | { | ||||
parent::__construct(); | parent::__construct(); | ||||
$this->maxCacheDuration = $max_cache_duration; | |||||
$this->siteKeyName = 'site_serial:' . crc32(SQL_TYPE . '://' . SQL_USER . ':' . SQL_PASS . '@' . SQL_SERVER . ':' . TABLE_PREFIX . ':' . SQL_DB); | $this->siteKeyName = 'site_serial:' . crc32(SQL_TYPE . '://' . SQL_USER . ':' . SQL_PASS . '@' . SQL_SERVER . ':' . TABLE_PREFIX . ':' . SQL_DB); | ||||
// get cache handler class to use | // get cache handler class to use | ||||
$handler_class = kUtil::getSystemConfig()->get('CacheHandler', '') . 'CacheHandler'; | $handler_class = kUtil::getSystemConfig()->get('CacheHandler', '') . 'CacheHandler'; | ||||
// defined cache handler doesn't exist -> use default | // defined cache handler doesn't exist -> use default | ||||
if ( !class_exists($handler_class) ) { | if ( !class_exists($handler_class) ) { | ||||
$handler_class = 'FakeCacheHandler'; | $handler_class = 'FakeCacheHandler'; | ||||
Show All 10 Lines | |||||
elseif ( $this->Application->isDebugMode() && ($handler->getCachingType() == CACHING_TYPE_MEMORY) ) { | elseif ( $this->Application->isDebugMode() && ($handler->getCachingType() == CACHING_TYPE_MEMORY) ) { | ||||
$this->Application->Debugger->appendHTML('Memory Caching: "<strong>' . $handler_class . '</strong>"'); | $this->Application->Debugger->appendHTML('Memory Caching: "<strong>' . $handler_class . '</strong>"'); | ||||
} | } | ||||
$this->_handler = $handler; | $this->_handler = $handler; | ||||
$this->cachingType = $handler->getCachingType(); | $this->cachingType = $handler->getCachingType(); | ||||
$this->debugCache = $handler->getCachingType() == CACHING_TYPE_MEMORY && $this->Application->isDebugMode(); | $this->debugCache = $handler->getCachingType() == CACHING_TYPE_MEMORY && $this->Application->isDebugMode(); | ||||
$this->_storeStatistics = defined('DBG_CACHE') && DBG_CACHE; | $this->_storeStatistics = defined('DBG_CACHE') && DBG_CACHE; | ||||
$this->cachePrefix = $this->_cachePrefix(); | |||||
if ( $this->_storeStatistics ) { | if ( $this->_storeStatistics ) { | ||||
// don't use FileHelper, since kFactory isn't ready yet | // don't use FileHelper, since kFactory isn't ready yet | ||||
if ( !file_exists(RESTRICTED . DIRECTORY_SEPARATOR . 'cache_usage') ) { | if ( !file_exists(RESTRICTED . DIRECTORY_SEPARATOR . 'cache_usage') ) { | ||||
mkdir(RESTRICTED . DIRECTORY_SEPARATOR . 'cache_usage'); | mkdir(RESTRICTED . DIRECTORY_SEPARATOR . 'cache_usage'); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Returns caching type of current storage engine | * Returns caching type of current storage engine | ||||
* | * | ||||
* @return int | * @return int | ||||
*/ | */ | ||||
function getCachingType() | function getCachingType() | ||||
{ | { | ||||
return $this->cachingType; | return $this->cachingType; | ||||
} | } | ||||
/** | /** | ||||
* Stores value to cache | * Stores new $value in cache with $name name. | ||||
* | * | ||||
* @param string $name | * @param string $name Key name to add to cache. | ||||
* @param mixed $value | * @param mixed $value Value of cached record. | ||||
* @param int $expiration cache record expiration time in seconds | * @param integer|null $expiration When value expires (0 - doesn't expire). | ||||
* @return bool | * | ||||
* @return boolean | |||||
*/ | */ | ||||
function setCache($name, $value, $expiration) | function setCache($name, $value, $expiration = null) | ||||
{ | { | ||||
if ( $expiration === null ) { | |||||
$expiration = $this->maxCacheDuration; | |||||
} | |||||
// 1. stores current version of serial for given cache key | // 1. stores current version of serial for given cache key | ||||
$this->_setCache($name . '_serials', $this->replaceSerials($name), $expiration); | $this->_setCache($name . '_serials', $this->replaceSerials($name), $expiration); | ||||
$this->storeStatistics($name, 'W'); | $this->storeStatistics($name, 'W'); | ||||
// 2. don't replace serials within the key | // 2. don't replace serials within the key | ||||
$saved = $this->_setCache($name, $value, $expiration); | $saved = $this->_setCache($name, $value, $expiration); | ||||
$this->storeStatistics($name, 'U'); | $this->storeStatistics($name, 'U'); | ||||
Show All 15 Lines | |||||
{ | { | ||||
$prepared_name = $this->prepareKeyName($name); | $prepared_name = $this->prepareKeyName($name); | ||||
$this->_localStorage[$prepared_name] = $value; | $this->_localStorage[$prepared_name] = $value; | ||||
return $this->_handler->set($prepared_name, $value, $expiration); | return $this->_handler->set($prepared_name, $value, $expiration); | ||||
} | } | ||||
/** | /** | ||||
* Stores value to cache (only if it's not there already) | * Stores value to cache (only if it's not there already). | ||||
* | * | ||||
* @param string $name | * @param string $name Key name to add to cache. | ||||
* @param mixed $value | * @param mixed $value Value of cached record. | ||||
* @param int $expiration cache record expiration time in seconds | * @param integer|null $expiration When value expires (0 - doesn't expire). | ||||
* @return bool | * | ||||
* @return boolean | |||||
*/ | */ | ||||
function addCache($name, $value, $expiration) | function addCache($name, $value, $expiration = null) | ||||
{ | { | ||||
if ( $expiration === null ) { | |||||
$expiration = $this->maxCacheDuration; | |||||
} | |||||
// 1. stores current version of serial for given cache key | // 1. stores current version of serial for given cache key | ||||
$this->_setCache($name . '_serials', $this->replaceSerials($name), $expiration); | $this->_setCache($name . '_serials', $this->replaceSerials($name), $expiration); | ||||
// 2. remove rebuilding mark | // 2. remove rebuilding mark | ||||
$this->delete($name . '_rebuilding'); | $this->delete($name . '_rebuilding'); | ||||
// 3. don't replace serials within the key | // 3. don't replace serials within the key | ||||
return $this->_addCache($name, $value, $expiration); | return $this->_addCache($name, $value, $expiration); | ||||
▲ Show 20 Lines • Show All 247 Lines • ▼ Show 20 Line(s) | |||||
{ | { | ||||
// don't check for enabled, because we maybe need to reset cache anyway | // don't check for enabled, because we maybe need to reset cache anyway | ||||
if ($this->cachingType == CACHING_TYPE_TEMPORARY) { | if ($this->cachingType == CACHING_TYPE_TEMPORARY) { | ||||
return ; | return ; | ||||
} | } | ||||
$site_key = $this->_cachePrefix(true); | $site_key = $this->_cachePrefix(true); | ||||
$this->_handler->set($site_key, $this->_handler->get($site_key) + 1); | $this->_handler->set($site_key, $this->_handler->get($site_key) + 1, 0); | ||||
} | } | ||||
/** | /** | ||||
* Replaces serials and adds unique site prefix to cache variable name | * Replaces serials and adds unique site prefix to cache variable name | ||||
* | * | ||||
* @param string $name | * @param string $name | ||||
* @return string | * @return string | ||||
*/ | */ | ||||
protected function prepareKeyName($name) | protected function prepareKeyName($name) | ||||
{ | { | ||||
if ( $this->cachingType == CACHING_TYPE_TEMPORARY ) { | if ( $this->cachingType == CACHING_TYPE_TEMPORARY ) { | ||||
return $name; | return $name; | ||||
} | } | ||||
// add site-wide prefix to key | // add site-wide prefix to key | ||||
return $this->_cachePrefix() . $name; | return $this->cachePrefix . $name; | ||||
} | } | ||||
/** | /** | ||||
* Replaces serials within given string | * Replaces serials within given string | ||||
* | * | ||||
* @param string $value | * @param string $value | ||||
* @return string | * @return string | ||||
* @access protected | * @access protected | ||||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Line(s) | |||||
* @return mixed | * @return mixed | ||||
* @access public | * @access public | ||||
*/ | */ | ||||
abstract public function get($names); | abstract public function get($names); | ||||
/** | /** | ||||
* Stores value in cache | * Stores value in cache | ||||
* | * | ||||
* @param string $name | * @param string $name Name. | ||||
* @param mixed $value | * @param mixed $value Value. | ||||
* @param int $expiration | * @param integer|null $expiration Expiration. | ||||
* @return bool | * | ||||
* @access public | * @return boolean | ||||
*/ | */ | ||||
abstract public function set($name, $value, $expiration = 0); | abstract public function set($name, $value, $expiration = null); | ||||
/** | /** | ||||
* Stores value in cache (only if it's not there already) | * Stores value in cache (only if it's not there already) | ||||
* | * | ||||
* @param string $name | * @param string $name Name. | ||||
* @param mixed $value | * @param mixed $value Value. | ||||
* @param int $expiration | * @param integer|null $expiration Expiration. | ||||
* @return bool | * | ||||
* @access public | * @return boolean | ||||
*/ | */ | ||||
abstract public function add($name, $value, $expiration = 0); | abstract public function add($name, $value, $expiration = null); | ||||
/** | /** | ||||
* Deletes key from cach | * Deletes key from cach | ||||
* | * | ||||
* @param string $name | * @param string $name | ||||
* @return bool | * @return bool | ||||
* @access public | * @access public | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Line(s) | |||||
} | } | ||||
return $this->parent->getFromLocalStorage($names); | return $this->parent->getFromLocalStorage($names); | ||||
} | } | ||||
/** | /** | ||||
* Stores value in cache | * Stores value in cache | ||||
* | * | ||||
* @param string $name | * @param string $name Name. | ||||
* @param mixed $value | * @param mixed $value Value. | ||||
* @param int $expiration | * @param integer|null $expiration Expiration. | ||||
* @return bool | * | ||||
* @access public | * @return boolean | ||||
*/ | */ | ||||
public function set($name, $value, $expiration = 0) | public function set($name, $value, $expiration = null) | ||||
{ | { | ||||
return true; | return true; | ||||
} | } | ||||
/** | /** | ||||
* Stores value in cache (only if it's not there already) | * Stores value in cache (only if it's not there already) | ||||
* | * | ||||
* @param string $name | * @param string $name Name. | ||||
* @param mixed $value | * @param mixed $value Value. | ||||
* @param int $expiration | * @param integer|null $expiration Expiration. | ||||
* @return bool | * | ||||
* @access public | * @return boolean | ||||
*/ | */ | ||||
public function add($name, $value, $expiration = 0) | public function add($name, $value, $expiration = null) | ||||
{ | { | ||||
return true; | return true; | ||||
} | } | ||||
/** | /** | ||||
* Deletes key from cach | * Deletes key from cach | ||||
* | * | ||||
* @param string $name | * @param string $name | ||||
Show All 38 Lines | |||||
} | } | ||||
else { | else { | ||||
$port = 11211; | $port = 11211; | ||||
} | } | ||||
$this->_handler->addServer($server, $port); | $this->_handler->addServer($server, $port); | ||||
} | } | ||||
// verify, that memcache server is working | // Verify, that memcache server is working. | ||||
if ( !$this->_handler->set('test', 1) ) { | if ( !$this->set('test', 1) ) { | ||||
$this->_enabled = false; | $this->_enabled = false; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Retrieves value from cache | * Retrieves value from cache | ||||
* | * | ||||
* @param string $name | * @param string $name | ||||
* @return mixed | * @return mixed | ||||
* @access public | * @access public | ||||
*/ | */ | ||||
public function get($name) | public function get($name) | ||||
{ | { | ||||
return $this->_handler->get($name); | return $this->_handler->get($name); | ||||
} | } | ||||
/** | /** | ||||
* Stores value in cache | * Stores value in cache | ||||
* | * | ||||
* @param string $name | * @param string $name Name. | ||||
* @param mixed $value | * @param mixed $value Value. | ||||
* @param int $expiration | * @param integer|null $expiration Expiration. | ||||
* @return bool | * | ||||
* @access public | * @return boolean | ||||
*/ | */ | ||||
public function set($name, $value, $expiration = 0) | public function set($name, $value, $expiration = null) | ||||
{ | { | ||||
// 0 - don't use compression | // 0 - don't use compression | ||||
return $this->_handler->set($name, $value, 0, $expiration); | return $this->_handler->set($name, $value, 0, $expiration); | ||||
} | } | ||||
/** | /** | ||||
* Stores value in cache (only if it's not there already) | * Stores value in cache (only if it's not there already) | ||||
* | * | ||||
* @param string $name | * @param string $name Name. | ||||
* @param mixed $value | * @param mixed $value Value. | ||||
* @param int $expiration | * @param integer|null $expiration Expiration. | ||||
* @return bool | * | ||||
* @access public | * @return boolean | ||||
*/ | */ | ||||
public function add($name, $value, $expiration = 0) | public function add($name, $value, $expiration = null) | ||||
{ | { | ||||
// 0 - don't use compression | // 0 - don't use compression | ||||
return $this->_handler->add($name, $value, 0, $expiration); | return $this->_handler->add($name, $value, 0, $expiration); | ||||
} | } | ||||
/** | /** | ||||
* Deletes key from cache | * Deletes key from cache | ||||
* | * | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Line(s) | |||||
else { | else { | ||||
$port = 11211; | $port = 11211; | ||||
} | } | ||||
$this->_handler->addServer($server, $port); | $this->_handler->addServer($server, $port); | ||||
} | } | ||||
// Verify, that memcache server is working. | // Verify, that memcache server is working. | ||||
if ( !$this->_handler->set('test', 1) ) { | if ( !$this->set('test', 1) ) { | ||||
$this->_enabled = false; | $this->_enabled = false; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Retrieves value from cache | * Retrieves value from cache | ||||
* | * | ||||
Show All 9 Lines | |||||
} | } | ||||
return $this->_handler->get($name); | return $this->_handler->get($name); | ||||
} | } | ||||
/** | /** | ||||
* Stores value in cache | * Stores value in cache | ||||
* | * | ||||
* @param string $name Name. | * @param string $name Name. | ||||
* @param mixed $value Value. | * @param mixed $value Value. | ||||
* @param integer $expiration Expiration. | * @param integer|null $expiration Expiration. | ||||
* | * | ||||
* @return boolean | * @return boolean | ||||
* @access public | |||||
*/ | */ | ||||
public function set($name, $value, $expiration = 0) | public function set($name, $value, $expiration = null) | ||||
{ | { | ||||
return $this->_handler->set($name, $value, $expiration); | return $this->_handler->set($name, $value, $expiration); | ||||
} | } | ||||
/** | /** | ||||
* Stores value in cache (only if it's not there already) | * Stores value in cache (only if it's not there already) | ||||
* | * | ||||
* @param string $name Name. | * @param string $name Name. | ||||
* @param mixed $value Value. | * @param mixed $value Value. | ||||
* @param integer $expiration Expiration. | * @param integer|null $expiration Expiration. | ||||
* | * | ||||
* @return boolean | * @return boolean | ||||
* @access public | |||||
*/ | */ | ||||
public function add($name, $value, $expiration = 0) | public function add($name, $value, $expiration = null) | ||||
{ | { | ||||
return $this->_handler->add($name, $value, $expiration); | return $this->_handler->add($name, $value, $expiration); | ||||
} | } | ||||
/** | /** | ||||
* Deletes key from cache | * Deletes key from cache | ||||
* | * | ||||
* @param string $name Name. | * @param string $name Name. | ||||
Show All 33 Lines | |||||
public function get($name) | public function get($name) | ||||
{ | { | ||||
return apc_fetch($name); | return apc_fetch($name); | ||||
} | } | ||||
/** | /** | ||||
* Stores value in cache | * Stores value in cache | ||||
* | * | ||||
* @param string $name | * @param string $name Name. | ||||
* @param mixed $value | * @param mixed $value Value. | ||||
* @param int $expiration | * @param integer|null $expiration Expiration. | ||||
* @return bool | * | ||||
* @access public | * @return boolean | ||||
*/ | */ | ||||
public function set($name, $value, $expiration = 0) | public function set($name, $value, $expiration = null) | ||||
{ | { | ||||
return apc_store($name, $value, $expiration); | return apc_store($name, $value, $expiration); | ||||
} | } | ||||
/** | /** | ||||
* Stores value in cache (only if it's not there already) | * Stores value in cache (only if it's not there already) | ||||
* | * | ||||
* @param string $name | * @param string $name Name. | ||||
* @param mixed $value | * @param mixed $value Value. | ||||
* @param int $expiration | * @param integer|null $expiration Expiration. | ||||
* @return bool | * | ||||
* @access public | * @return boolean | ||||
*/ | */ | ||||
public function add($name, $value, $expiration = 0) | public function add($name, $value, $expiration = null) | ||||
{ | { | ||||
return apc_add($name, $value, $expiration); | return apc_add($name, $value, $expiration); | ||||
} | } | ||||
/** | /** | ||||
* Deletes key from cache | * Deletes key from cache | ||||
* | * | ||||
* @param string $name | * @param string $name | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Line(s) | |||||
} | } | ||||
return xcache_isset($names) ? xcache_get($names) : false; | return xcache_isset($names) ? xcache_get($names) : false; | ||||
} | } | ||||
/** | /** | ||||
* Stores value in cache | * Stores value in cache | ||||
* | * | ||||
* @param string $name | * @param string $name Name. | ||||
* @param mixed $value | * @param mixed $value Value. | ||||
* @param int $expiration | * @param integer|null $expiration Expiration. | ||||
* @return bool | * | ||||
* @access public | * @return boolean | ||||
*/ | */ | ||||
public function set($name, $value, $expiration = 0) | public function set($name, $value, $expiration = null) | ||||
{ | { | ||||
return xcache_set($name, $value, $expiration); | return xcache_set($name, $value, $expiration); | ||||
} | } | ||||
/** | /** | ||||
* Stores value in cache (only if it's not there already) | * Stores value in cache (only if it's not there already) | ||||
* | * | ||||
* @param string $name | * @param string $name Name. | ||||
* @param mixed $value | * @param mixed $value Value. | ||||
* @param int $expiration | * @param integer|null $expiration Expiration. | ||||
* @return bool | * | ||||
* @access public | * @return boolean | ||||
*/ | */ | ||||
public function add($name, $value, $expiration = 0) | public function add($name, $value, $expiration = null) | ||||
{ | { | ||||
// not atomic operation, like in Memcached and may fail | // not atomic operation, like in Memcached and may fail | ||||
if ( xcache_isset($name) ) { | if ( xcache_isset($name) ) { | ||||
return false; | return false; | ||||
} | } | ||||
return $this->set($name, $value, $expiration); | return $this->set($name, $value, $expiration); | ||||
} | } | ||||
Show All 13 Lines |