-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a basic Cache driver for rollout
- Loading branch information
Showing
3 changed files
with
157 additions
and
2 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
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,87 @@ | ||
<?php | ||
|
||
namespace Jaspaul\LaravelRollout\Drivers; | ||
|
||
use Illuminate\Contracts\Cache\Store; | ||
use Opensoft\Rollout\Storage\StorageInterface; | ||
|
||
class Cache implements StorageInterface | ||
{ | ||
/** | ||
* An instance of a cache store that we can store our keys in. | ||
* | ||
* @var \Illuminate\Contracts\Cache\Store; | ||
*/ | ||
protected $store; | ||
|
||
/** | ||
* The prefix for the cache key. | ||
* | ||
* @var string | ||
*/ | ||
protected $prefix; | ||
|
||
/** | ||
* Configures our cache driver with an instance of the cache store and a key | ||
* prefix. | ||
* | ||
* @param \Illuminate\Contracts\Cache\Store $store | ||
* An instance of the cache store. | ||
* @param string $prefix | ||
* A prefix for the cache keys. | ||
*/ | ||
public function __construct(Store $store, string $prefix = 'rollout') | ||
{ | ||
$this->store = $store; | ||
$this->prefix = $prefix; | ||
} | ||
|
||
/** | ||
* Get's the value corresponding to the provided key from the cache. Note | ||
* this function has the side effect of prepending the local prefix to the | ||
* key. | ||
* | ||
* @param string $key | ||
* The key for the cached item. | ||
* | ||
* @return string|null | ||
* Null if the value is not found | ||
*/ | ||
public function get($key) | ||
{ | ||
$key = sprintf('%s.%s', $this->prefix, $key); | ||
return $this->store->get($key); | ||
} | ||
|
||
/** | ||
* Store the provided key in the underlying cache layer. Note this function | ||
* has the side effect of prepending the local prefix to the ky. | ||
* | ||
* @param string $key | ||
* The key to store the cache under. | ||
* @param string $value | ||
* The value to bind to the key. | ||
* | ||
* @return void | ||
*/ | ||
public function set($key, $value) | ||
{ | ||
$key = sprintf('%s.%s', $this->prefix, $key); | ||
$this->store->forever($key, $value); | ||
} | ||
|
||
/** | ||
* Removes the given key from the cache. Note this will handle prefixing the | ||
* key before removal. | ||
* | ||
* @param string $key | ||
* The key to remove. | ||
* | ||
* @return void | ||
*/ | ||
public function remove($key) | ||
{ | ||
$key = sprintf('%s.%s', $this->prefix, $key); | ||
$this->store->forget($key); | ||
} | ||
} |
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,68 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Mockery; | ||
use Tests\TestCase; | ||
use Illuminate\Cache\ArrayStore; | ||
use Jaspaul\LaravelRollout\Drivers\Cache; | ||
|
||
class CacheTest extends TestCase | ||
{ | ||
private $prefix = 'testing'; | ||
|
||
private $store; | ||
private $cache; | ||
|
||
/** | ||
* @before | ||
*/ | ||
function setup_cache() | ||
{ | ||
$this->store = new ArrayStore(); | ||
$this->cache = new Cache($this->store, $this->prefix); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
function ensure_the_cache_can_be_constructed() | ||
{ | ||
$this->assertInstanceOf(Cache::class, $this->cache); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
function get_returns_null_if_the_cache_does_not_have_the_requested_key() | ||
{ | ||
$this->assertNull($this->cache->get('key')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
function once_set_you_can_get_the_value_back_with_the_same_key() | ||
{ | ||
$key = 'key'; | ||
$value = 'value'; | ||
|
||
$this->cache->set($key, $value); | ||
$this->assertSame($value, $this->cache->get($key)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
function once_you_remove_a_value_you_will_not_be_able_to_retrieve_it_from_the_store() | ||
{ | ||
$key = 'key'; | ||
$value = 'value'; | ||
|
||
$this->cache->set($key, $value); | ||
$this->assertSame($value, $this->cache->get($key)); | ||
|
||
$this->cache->remove($key); | ||
$this->assertNull($this->cache->get($key)); | ||
} | ||
} |