Skip to content

Commit

Permalink
Add a basic Cache driver for rollout
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaspaul committed Mar 28, 2017
1 parent eae5c63 commit aa67cba
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 2 deletions.
4 changes: 2 additions & 2 deletions config/rollout.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
| This option controls the default location where we'll store the data
| used by rollout.
|
| Supported: "database", "null"
| Supported: "cache"
|
*/
'driver' => env('ROLLOUT_DRIVER', 'database')
'driver' => env('ROLLOUT_DRIVER', 'cache')

];
87 changes: 87 additions & 0 deletions src/Drivers/Cache.php
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);
}
}
68 changes: 68 additions & 0 deletions tests/Drivers/CacheTest.php
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));
}
}

0 comments on commit aa67cba

Please sign in to comment.