From 8dba47d466d3c5f411c4abe42f292c277cf8267f Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Wed, 6 Jun 2012 21:58:36 +0200 Subject: [PATCH] Add layer to select fast or slow cache for storing values --- lib/cache.php | 10 ++++++++ lib/cache/broker.php | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 lib/cache/broker.php diff --git a/lib/cache.php b/lib/cache.php index 70f11f3551..55d5b064c4 100644 --- a/lib/cache.php +++ b/lib/cache.php @@ -10,7 +10,17 @@ class OC_Cache { static protected $cache; static protected function init() { + $fast_cache = null; + if (!$fast_cache && function_exists('xcache_set')) { + $fast_cache = new OC_Cache_XCache(); + } + if (!$fast_cache && function_exists('apc_store')) { + $fast_cache = new OC_Cache_APC(); + } self::$cache = new OC_Cache_File(); + if ($fast_cache) { + self::$cache = new OC_Cache_Broker($fast_cache, self::$cache); + } } static public function get($key) { diff --git a/lib/cache/broker.php b/lib/cache/broker.php new file mode 100644 index 0000000000..62a7cd96d1 --- /dev/null +++ b/lib/cache/broker.php @@ -0,0 +1,55 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class OC_Cache_Broker { + protected $fast_cache; + protected $slow_cache; + + public function __construct($fast_cache, $slow_cache) { + $this->fast_cache = $fast_cache; + $this->slow_cache = $slow_cache; + } + + public function get($key) { + if ($r = $this->fast_cache->get($key)) { + return $r; + } + return $this->slow_cache->get($key); + } + + public function set($key, $value, $ttl=0) { + $set_slow = strlen($value) > 8192; + if ($set_slow) { + if ($this->fast_cache->hasKey($key)) { + $this->fast_cache->remove($key); + } + $this->slow_cache->set($key, $value, $ttl); + } else { + $this->fast_cache->set($key, $value, $ttl); + } + } + + public function hasKey($key) { + if ($this->fast_cache->hasKey($key)) { + return true; + } + return $this->slow_cache->hasKey($key); + } + + public function remove($key) { + if ($this->fast_cache->remove($key)) { + return true; + } + return $this->slow_cache->remove($key); + } + + public function clear(){ + $this->fast_cache->clear(); + $this->slow_cache->clear(); + } +}