diff options
author | Bart Visscher <bartv@thisnet.nl> | 2012-06-06 21:58:36 +0200 |
---|---|---|
committer | Bart Visscher <bartv@thisnet.nl> | 2012-06-06 22:41:36 +0200 |
commit | 8dba47d466d3c5f411c4abe42f292c277cf8267f (patch) | |
tree | 4e451e50284c7f4ee4357973d21c315796ab8abc /lib/cache | |
parent | a90089c79200c4665e28acb07215f08c2039c1a1 (diff) | |
download | nextcloud-server-8dba47d466d3c5f411c4abe42f292c277cf8267f.tar.gz nextcloud-server-8dba47d466d3c5f411c4abe42f292c277cf8267f.zip |
Add layer to select fast or slow cache for storing values
Diffstat (limited to 'lib/cache')
-rw-r--r-- | lib/cache/broker.php | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/cache/broker.php b/lib/cache/broker.php new file mode 100644 index 00000000000..62a7cd96d15 --- /dev/null +++ b/lib/cache/broker.php @@ -0,0 +1,55 @@ +<?php +/** + * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> + * 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(); + } +} |