Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added back v1/v2 adapter abstract and added new PsrCompatible adapter
- Loading branch information
1 parent
44eb344
commit 2dc10cf
Showing
3 changed files
with
359 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
namespace CacheBundle\Service; | ||
|
||
use CacheBundle\Exception\CacheException; | ||
use Predis\Client; | ||
|
||
abstract class AbstractCache | ||
{ | ||
/** | ||
* Tries to add new key | ||
* @param $key | ||
* @param $value | ||
* @param int $ttl | ||
* @return bool | ||
* @throws CacheException | ||
*/ | ||
abstract public function add($key, $value, $ttl = 600); | ||
/** | ||
* @param $key | ||
* @param $value | ||
* @param int $ttl | ||
* @return bool | ||
* @throws CacheException | ||
*/ | ||
abstract public function set($key, $value, $ttl = 600); | ||
|
||
/** | ||
* Checks if a cache key exists | ||
* @param $key | ||
* @return bool | ||
*/ | ||
abstract public function has($key); | ||
|
||
/** | ||
* Retrieves info from a cache key | ||
* @param $key | ||
* @return mixed | ||
*/ | ||
abstract public function get($key); | ||
|
||
/** | ||
* Retrieves the ttl of the specified cache key | ||
* @param $key | ||
* @return int | ||
*/ | ||
abstract public function ttl($key); | ||
|
||
/** | ||
* Deletes the cache key. If key does not exists, it will throw CacheException | ||
* @param $key | ||
* @return mixed | ||
* @throws CacheException | ||
*/ | ||
abstract public function delete($key); | ||
|
||
/** | ||
* Increases the ttl of the cache | ||
* @param $key | ||
* @param int $ttl | ||
*/ | ||
abstract public function refreshTtl($key, $ttl = 3600); | ||
|
||
/** | ||
* @param $key | ||
* @param int $ttl | ||
* @return bool | ||
* @throws CacheException | ||
*/ | ||
final public function lock($key, $ttl = 3600) | ||
{ | ||
return $this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
|
||
|
||
namespace CacheBundle\Service; | ||
|
||
|
||
use CacheBundle\Exception\CacheException; | ||
|
||
class MultiLevelCache extends AbstractCache | ||
{ | ||
/** | ||
* @var AbstractCache[] | ||
*/ | ||
protected $engines; | ||
|
||
/** | ||
* @param AbstractCache[] $engines | ||
*/ | ||
public function setEngines(array $engines) | ||
{ | ||
$this->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]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
namespace CacheBundle\Service; | ||
|
||
use CacheBundle\Exception\CacheException; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
|
||
class PsrCompatible extends AbstractCache | ||
{ | ||
|
||
/** | ||
* @var CacheItemPoolInterface | ||
*/ | ||
protected $backend; | ||
|
||
/** | ||
* Tries to add new key | ||
* @param $key | ||
* @param $value | ||
* @param int $ttl | ||
* @return bool | ||
* @throws CacheException | ||
*/ | ||
public function add($key, $value, $ttl = 600) | ||
{ | ||
$item = $this->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); | ||
} | ||
} |