From 2dc10cf8c656940f8fabefbb26b207be3982b18f Mon Sep 17 00:00:00 2001 From: Alexandru Bumbacea Date: Wed, 7 Dec 2016 21:55:29 +0200 Subject: [PATCH] added back v1/v2 adapter abstract and added new PsrCompatible adapter --- src/CacheBundle/Service/AbstractCache.php | 102 +++++++++++++ src/CacheBundle/Service/MultiLevelCache.php | 157 ++++++++++++++++++++ src/CacheBundle/Service/PsrCompatible.php | 100 +++++++++++++ 3 files changed, 359 insertions(+) create mode 100644 src/CacheBundle/Service/AbstractCache.php create mode 100644 src/CacheBundle/Service/MultiLevelCache.php create mode 100644 src/CacheBundle/Service/PsrCompatible.php diff --git a/src/CacheBundle/Service/AbstractCache.php b/src/CacheBundle/Service/AbstractCache.php new file mode 100644 index 0000000..bc0d5a3 --- /dev/null +++ b/src/CacheBundle/Service/AbstractCache.php @@ -0,0 +1,102 @@ +add('lock:' . $key, 1, $ttl); + } + + + /** + * @param $key + * @return bool + * @throws CacheException + */ + final public function unlock($key) + { + return $this->delete('lock:' . $key); + } + + /** + * @param $key + * @return bool + */ + final public function hasLock($key) + { + return ($this->get('lock:' . $key) == 1); + } + + /** + * @param $key + * @param int $ttl + */ + final public function heartBeatLock($key, $ttl = 3600) + { + $this->refreshTtl('lock:' . $key, $ttl); + } +} \ No newline at end of file diff --git a/src/CacheBundle/Service/MultiLevelCache.php b/src/CacheBundle/Service/MultiLevelCache.php new file mode 100644 index 0000000..703a609 --- /dev/null +++ b/src/CacheBundle/Service/MultiLevelCache.php @@ -0,0 +1,157 @@ +engines = $engines; + } + + + /** + * @param $key + * @param $value + * @param int $ttl + * + * @return bool + * @throws CacheException + */ + public function set($key, $value, $ttl = 600) + { + $ttl = $this->explodeTtl($ttl); + + foreach ($this->engines as $alias => $engine) { + $engine->set($key, $value, $ttl[$alias]); + } + } + + /** + * Checks if a cache key exists + * + * @param $key + * + * @return bool + */ + public function has($key) + { + foreach ($this->engines as $engine) { + if ($engine->has($key)) { + return true; + } + } + + return false; + } + + /** + * Retrieves info from a cache key + * + * @param $key + * + * @return mixed + */ + public function get($key) + { + foreach ($this->engines as $engine) { + $data = $engine->get($key); + if ($data) { + return $data; + } + } + + return false; + } + + /** + * Retrieves the ttl of the specified cache key + * + * @param $key + * + * @return int + */ + public function ttl($key) + { + $max = 0; + foreach ($this->engines as $engine) { + $max = max($max, $engine->ttl($key)); + } + return $max; + } + + /** + * Deletes the cache key. If key does not exists, it will throw CacheException + * + * @param $key + * + * @return mixed + * @throws CacheException + */ + public function delete($key) + { + foreach ($this->engines as $engine) { + $engine->delete($key); + } + } + + /** + * Increases the ttl of the cache + * + * @param $key + * @param int $ttl + */ + public function refreshTtl($key, $ttl = 3600) + { + $ttl = $this->explodeTtl($ttl); + + foreach ($this->engines as $alias => $engine) { + $engine->refreshTtl($key, $ttl[$alias]); + } + } + + /** + * @param $ttl + * + * @return array + */ + protected function explodeTtl($ttl) + { + if (is_numeric($ttl)) { + $ttl = array_combine(array_keys($this->engines), array_fill(0, count($this->engines), $ttl)); + + return $ttl; + } + + return $ttl; + } + + /** + * Tries to add new key + * @param $key + * @param $value + * @param int $ttl + * @return bool + * @throws CacheException + */ + public function add($key, $value, $ttl = 600) + { + $ttl = $this->explodeTtl($ttl); + + foreach ($this->engines as $alias => $engine) { + $engine->add($key, $value, $ttl[$alias]); + } + } +} \ No newline at end of file diff --git a/src/CacheBundle/Service/PsrCompatible.php b/src/CacheBundle/Service/PsrCompatible.php new file mode 100644 index 0000000..76310c6 --- /dev/null +++ b/src/CacheBundle/Service/PsrCompatible.php @@ -0,0 +1,100 @@ +backend->getItem($key); + if ($item->isHit()) { + throw new CacheException('Lock already present'); + } + $item->set($value); + $item->expiresAfter($ttl); + $this->backend->save($item); + } + + /** + * @param $key + * @param $value + * @param int $ttl + * @return bool + * @throws CacheException + */ + public function set($key, $value, $ttl = 600) + { + $item = $this->backend->getItem($key); + $item->set($value); + $item->expiresAfter($ttl); + $this->backend->save($item); + } + + /** + * Checks if a cache key exists + * @param $key + * @return bool + */ + public function has($key) + { + return $this->backend->hasItem($key); + } + + /** + * Retrieves info from a cache key + * @param $key + * @return mixed + */ + public function get($key) + { + return $this->backend->getItem($key)->get(); + } + + /** + * Retrieves the ttl of the specified cache key + * @param $key + * @return int + */ + public function ttl($key) + { + throw new CacheException('Unable to determine TTL!'); + } + + /** + * Deletes the cache key. If key does not exists, it will throw CacheException + * @param $key + * @return mixed + * @throws CacheException + */ + public function delete($key) + { + $this->backend->deleteItem($key); + } + + /** + * Increases the ttl of the cache + * @param $key + * @param int $ttl + */ + public function refreshTtl($key, $ttl = 3600) + { + $item = $this->backend->getItem($key)->expiresAfter($ttl); + $this->backend->save($item); + } +} \ No newline at end of file