Skip to content

Commit

Permalink
Remove prefix from Ephemeral driver
Browse files Browse the repository at this point in the history
  • Loading branch information
PHLAK committed Dec 16, 2017
1 parent e051928 commit f1c04f3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 50 deletions.
16 changes: 2 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,13 @@ $stash = Stash\Cache::make('apcu', function () {
#### Ephemeral

The Ephemeral driver caches items in a PHP array that exists in memory only for
the lifetime of the script.
the lifetime of the script. The Ephemeral driver does not take a configuration
closure.

```php
$stash = Stash\Cache::make('ephemeral');
```

The Ephemeral driver does not require a configuration closure. However, if you
wish to set a prefix you can pass a configuration closure that returns an array.
The returned array must contain a key of `prefix` with a string value of the
desired prefix.

```php
$stash = Stash\Cache::make('ephemeral', function () {
return [
'prefix' => 'some_prefix'
];
});
```

Usage
-----

Expand Down
37 changes: 11 additions & 26 deletions src/Drivers/Ephemeral.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,7 @@
class Ephemeral implements Cacheable
{
/** @var array Array of cached items */
protected $cache = [null => []];

/** @var string Prefix string to prevent collisions */
protected $prefix = '';

/**
* Stash\Drivers\Ephemeral constructor, runs on object creation.
*
* @param \Closure|null $closure Anonymous configuration function
*/
public function __construct(\Closure $closure = null)
{
if (is_callable($closure)) {
$this->prefix = $closure()['prefix'];
}
}
protected $cache = [];

/**
* Put an item into the cache for a specified duration.
Expand All @@ -36,7 +21,7 @@ public function __construct(\Closure $closure = null)
*/
public function put($key, $data, $minutes = 0)
{
$this->cache[$this->prefix][$key] = new Item($data, $minutes);
$this->cache[$key] = new Item($data, $minutes);

return true;
}
Expand Down Expand Up @@ -64,8 +49,8 @@ public function forever($key, $data)
*/
public function get($key, $default = false)
{
if (array_key_exists($key, $this->cache[$this->prefix])) {
$item = $this->cache[$this->prefix][$key];
if (array_key_exists($key, $this->cache)) {
$item = $this->cache[$key];
if ($item->notExpired()) {
return $item->data;
}
Expand All @@ -83,8 +68,8 @@ public function get($key, $default = false)
*/
public function has($key)
{
if (array_key_exists($key, $this->cache[$this->prefix])) {
$item = $this->cache[$this->prefix][$key];
if (array_key_exists($key, $this->cache)) {
$item = $this->cache[$key];

return $item->notExpired();
}
Expand Down Expand Up @@ -138,8 +123,8 @@ public function rememberForever($key, \Closure $closure)
*/
public function increment($key, $value = 1)
{
if (array_key_exists($key, $this->cache[$this->prefix])) {
$item = $this->cache[$this->prefix][$key];
if (array_key_exists($key, $this->cache)) {
$item = $this->cache[$key];

return $item->increment($value);
}
Expand Down Expand Up @@ -182,8 +167,8 @@ public function touch($key, $minutes = 0)
*/
public function forget($key)
{
if (array_key_exists($key, $this->cache[$this->prefix])) {
unset($this->cache[$this->prefix][$key]);
if (array_key_exists($key, $this->cache)) {
unset($this->cache[$key]);

return true;
}
Expand All @@ -198,7 +183,7 @@ public function forget($key)
*/
public function flush()
{
$this->cache[$this->prefix] = [];
$this->cache = [];

return true;
}
Expand Down
10 changes: 0 additions & 10 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,6 @@ public function test_it_can_instantiate_the_ephemeral_driver()
$this->assertInstanceOf(Stash\Drivers\Ephemeral::class, $ephemeral);
}

public function test_it_can_instantiate_the_ephemeral_driver_with_prefix()
{
$ephemeral = Stash\Cache::make('ephemeral', function () {
return ['prefix' => 'stash_test'];
});

$this->assertInstanceOf(Stash\Interfaces\Cacheable::class, $ephemeral);
$this->assertInstanceOf(Stash\Drivers\Ephemeral::class, $ephemeral);
}

public function test_it_throws_an_exception_for_an_invalid_driver()
{
$this->setExpectedException(Stash\Exceptions\InvalidDriverException::class);
Expand Down

0 comments on commit f1c04f3

Please sign in to comment.