Skip to content

Commit aa67cba

Browse files
committed
Add a basic Cache driver for rollout
1 parent eae5c63 commit aa67cba

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed

config/rollout.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
| This option controls the default location where we'll store the data
1111
| used by rollout.
1212
|
13-
| Supported: "database", "null"
13+
| Supported: "cache"
1414
|
1515
*/
16-
'driver' => env('ROLLOUT_DRIVER', 'database')
16+
'driver' => env('ROLLOUT_DRIVER', 'cache')
1717

1818
];

src/Drivers/Cache.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Jaspaul\LaravelRollout\Drivers;
4+
5+
use Illuminate\Contracts\Cache\Store;
6+
use Opensoft\Rollout\Storage\StorageInterface;
7+
8+
class Cache implements StorageInterface
9+
{
10+
/**
11+
* An instance of a cache store that we can store our keys in.
12+
*
13+
* @var \Illuminate\Contracts\Cache\Store;
14+
*/
15+
protected $store;
16+
17+
/**
18+
* The prefix for the cache key.
19+
*
20+
* @var string
21+
*/
22+
protected $prefix;
23+
24+
/**
25+
* Configures our cache driver with an instance of the cache store and a key
26+
* prefix.
27+
*
28+
* @param \Illuminate\Contracts\Cache\Store $store
29+
* An instance of the cache store.
30+
* @param string $prefix
31+
* A prefix for the cache keys.
32+
*/
33+
public function __construct(Store $store, string $prefix = 'rollout')
34+
{
35+
$this->store = $store;
36+
$this->prefix = $prefix;
37+
}
38+
39+
/**
40+
* Get's the value corresponding to the provided key from the cache. Note
41+
* this function has the side effect of prepending the local prefix to the
42+
* key.
43+
*
44+
* @param string $key
45+
* The key for the cached item.
46+
*
47+
* @return string|null
48+
* Null if the value is not found
49+
*/
50+
public function get($key)
51+
{
52+
$key = sprintf('%s.%s', $this->prefix, $key);
53+
return $this->store->get($key);
54+
}
55+
56+
/**
57+
* Store the provided key in the underlying cache layer. Note this function
58+
* has the side effect of prepending the local prefix to the ky.
59+
*
60+
* @param string $key
61+
* The key to store the cache under.
62+
* @param string $value
63+
* The value to bind to the key.
64+
*
65+
* @return void
66+
*/
67+
public function set($key, $value)
68+
{
69+
$key = sprintf('%s.%s', $this->prefix, $key);
70+
$this->store->forever($key, $value);
71+
}
72+
73+
/**
74+
* Removes the given key from the cache. Note this will handle prefixing the
75+
* key before removal.
76+
*
77+
* @param string $key
78+
* The key to remove.
79+
*
80+
* @return void
81+
*/
82+
public function remove($key)
83+
{
84+
$key = sprintf('%s.%s', $this->prefix, $key);
85+
$this->store->forget($key);
86+
}
87+
}

tests/Drivers/CacheTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Mockery;
6+
use Tests\TestCase;
7+
use Illuminate\Cache\ArrayStore;
8+
use Jaspaul\LaravelRollout\Drivers\Cache;
9+
10+
class CacheTest extends TestCase
11+
{
12+
private $prefix = 'testing';
13+
14+
private $store;
15+
private $cache;
16+
17+
/**
18+
* @before
19+
*/
20+
function setup_cache()
21+
{
22+
$this->store = new ArrayStore();
23+
$this->cache = new Cache($this->store, $this->prefix);
24+
}
25+
26+
/**
27+
* @test
28+
*/
29+
function ensure_the_cache_can_be_constructed()
30+
{
31+
$this->assertInstanceOf(Cache::class, $this->cache);
32+
}
33+
34+
/**
35+
* @test
36+
*/
37+
function get_returns_null_if_the_cache_does_not_have_the_requested_key()
38+
{
39+
$this->assertNull($this->cache->get('key'));
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
function once_set_you_can_get_the_value_back_with_the_same_key()
46+
{
47+
$key = 'key';
48+
$value = 'value';
49+
50+
$this->cache->set($key, $value);
51+
$this->assertSame($value, $this->cache->get($key));
52+
}
53+
54+
/**
55+
* @test
56+
*/
57+
function once_you_remove_a_value_you_will_not_be_able_to_retrieve_it_from_the_store()
58+
{
59+
$key = 'key';
60+
$value = 'value';
61+
62+
$this->cache->set($key, $value);
63+
$this->assertSame($value, $this->cache->get($key));
64+
65+
$this->cache->remove($key);
66+
$this->assertNull($this->cache->get($key));
67+
}
68+
}

0 commit comments

Comments
 (0)