Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F1207828
in-portal
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Nov 9, 5:51 AM
Size
6 KB
Mime Type
text/x-diff
Expires
Tue, Nov 11, 5:51 AM (4 h, 27 m)
Engine
blob
Format
Raw Data
Handle
788094
Attached To
rINP In-Portal
in-portal
View Options
Index: branches/RC/core/kernel/utility/cache.php
===================================================================
--- branches/RC/core/kernel/utility/cache.php (revision 10565)
+++ branches/RC/core/kernel/utility/cache.php (revision 10566)
@@ -1,242 +1,277 @@
<?php
class kCache extends kBase {
/**
* Object, that represents cache storage
*
* @var CacheStorage
*/
var $_storage = null;
var $statistics = Array ();
var $debugCache = false;
function kCache() {
parent::kBase();
$this->debugCache = $this->Application->isDebugMode() && constOn('DBG_CACHE');
$memcached_servers = 'localhost:11211'; // $this->Application->ConfigValue('MemcachedServers');
if ($memcached_servers && class_exists('Memcache')) {
$this->_storage = new MemcacheCacheStorage($memcached_servers);
}
else if (false || $this->Application->ConfigValue('UseFileCache')) {
$this->_storage = new FileCacheStorage('file_cache.tmp');
}
else {
$this->_storage = new CacheStorage();
}
+
+ if (!$this->_storage->isWorking()) {
+ // when one of above cache storages fails to initialize fallback to memory cache
+ $this->_storage = new CacheStorage();
+ }
}
/**
* Adds new value to cache $cache_name and identified by key $key
*
* @param string $cache_name cache name
* @param int $key key name to add to cache
* @param mixed $value value of chached record
*/
function setCache($cache_name, $key, $value, $expires = 3600)
{
$this->_storage->set($cache_name, $key, $value, $expires);
}
/**
* Returns cached $key value from cache named $cache_name
*
* @param string $cache_name cache name
* @param int $key key name from cache
* @return mixed
*/
function getCache($cache_name, $key)
{
$ret = $this->_storage->get($cache_name, $key);
$this->setStatistics($cache_name, $key, $ret);
return $ret;
}
function setStatistics($cache_name, $key, $found)
{
if (!$this->debugCache) {
return true;
}
if (!array_key_exists($cache_name, $this->statistics)) {
$this->statistics[$cache_name] = Array ();
}
if (!array_key_exists($key, $this->statistics[$cache_name])) {
$this->statistics[$cache_name][$key] = Array ();
}
$status_key = $found ? 'found' : 'not_found';
if (!isset($this->statistics[$cache_name][$key][$status_key])) {
$this->statistics[$cache_name][$key][$status_key] = 0;
}
$this->statistics[$cache_name][$key][$status_key]++;
}
function printStatistics()
{
$cache_size = strlen(serialize($this->_storage));
$this->Application->Debugger->appendHTML('<strong>Cache Size:</strong> ' . formatSize($cache_size) . ' (' . $cache_size . ')');
foreach ($this->statistics as $cache_name => $cache_data) {
foreach ($cache_data as $key => $value) {
if (!array_key_exists('found', $value) || $value['found'] == 1) {
// remove cached records, that were used only 1 or 2 times
unset($this->statistics[$cache_name][$key]);
}
}
}
print_pre($this->statistics, 'Cache Statistics:');
}
}
class CacheStorage extends Params {
/**
+ * Determines, that cache storage is working fine
+ *
+ * @return bool
+ */
+ function isWorking()
+ {
+ return true;
+ }
+
+ /**
* Stores value to cache
*
* @param string $cache_name
* @param string $key
* @param mixed $value
* @param int $expires cache record expiration time in seconds
*/
function set($cache_name, $key, $value, $expires)
{
$cache = parent::Get($cache_name, Array());
$cache[$key] = $value;
parent::Set($cache_name, $cache);
}
/**
* Returns value from cache
*
* @param string $cache_name
* @param string $key
* @return mixed
*/
function get($cache_name, $key)
{
$cache = parent::Get($cache_name, Array());
$ret = array_key_exists($key, $cache) ? $cache[$key] : false;
return $ret;
}
}
class MemcacheCacheStorage {
/**
* Memcache connection
*
* @var Memcache
*/
var $_handler = null;
function MemcacheCacheStorage($servers)
{
$this->_handler = new Memcache;
$servers = explode(';', $servers);
foreach ($servers as $server) {
list ($server, $port) = strpos($server, ':') !== false ? explode(':', $server, 2) : Array ($server, 11211);
$this->_handler->addServer($server, $port);
}
}
/**
+ * Determines, that cache storage is working fine
+ *
+ * @return bool
+ */
+ function isWorking()
+ {
+ return $this->_handler->getVersion() !== false;
+ }
+
+ /**
* Stores value to cache
*
* @param string $cache_name
* @param string $key
* @param mixed $value
* @param int $expires cache record expiration time in seconds
*/
function set($cache_name, $key, $value, $expires)
{
$this->_handler->set($cache_name . '-' . $key, $value, false, $expires); // false could be MEMCACHE_COMPRESSED to compress values in memory
}
/**
* Returns value from cache
*
* @param string $cache_name
* @param string $key
* @return mixed
*/
function get($cache_name, $key)
{
return $this->_handler->get($cache_name . '-' . $key);
}
}
class FileCacheStorage extends Params {
/**
* Expiration time for each variable in cache
*
* @var resource
*/
var $_expiration = Array ();
/**
* Filename for storing cache
*
* @var string
*/
var $_filename = '';
function FileCacheStorage($filename = '')
{
$this->_filename = (defined('WRITEABLE') ? WRITEABLE.'/cache' : FULL_PATH.'/kernel/cache') . '/' . $filename;
if (file_exists($this->_filename)) {
$cache_data = unserialize(file_get_contents($this->_filename));
}
else {
$cache_data = Array ();
}
}
/**
+ * Determines, that cache storage is working fine
+ *
+ * @return bool
+ */
+ function isWorking()
+ {
+ return false;
+ }
+
+ /**
* Stores value to cache
*
* @param string $cache_name
* @param string $key
* @param mixed $value
* @param int $expires cache record expiration time in seconds
*/
function set($cache_name, $key, $value, $expires)
{
$cache = parent::Get($cache_name, Array());
$cache[$key] = $value;
parent::Set($cache_name, $cache);
}
/**
* Returns value from cache
*
* @param string $cache_name
* @param string $key
* @return mixed
*/
function get($cache_name, $key)
{
$cache = parent::Get($cache_name, Array());
$ret = array_key_exists($key, $cache) ? $cache[$key] : false;
return $ret;
}
}
?>
\ No newline at end of file
Property changes on: branches/RC/core/kernel/utility/cache.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.3.30.2
\ No newline at end of property
+1.3.30.3
\ No newline at end of property
Event Timeline
Log In to Comment