Index: branches/RC/core/kernel/utility/cache.php =================================================================== diff -u -N -r10494 -r10516 --- branches/RC/core/kernel/utility/cache.php (.../cache.php) (revision 10494) +++ branches/RC/core/kernel/utility/cache.php (.../cache.php) (revision 10516) @@ -17,7 +17,17 @@ $this->debugCache = $this->Application->isDebugMode() && constOn('DBG_CACHE'); - $this->_storage = new CacheStorage(); + $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(); + } } /** @@ -27,9 +37,9 @@ * @param int $key key name to add to cache * @param mixed $value value of chached record */ - function setCache($cache_name, $key, $value) + function setCache($cache_name, $key, $value, $expires = 3600) { - $this->_storage->set($cache_name, $key, $value); + $this->_storage->set($cache_name, $key, $value, $expires); } /** @@ -91,14 +101,29 @@ class CacheStorage extends Params { - function set($cache_name, $key, $value) + /** + * 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()); @@ -107,5 +132,111 @@ 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); + } + } + + /** + * 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 (); + } + } + + /** + * 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