From f1c04f397febc7ed36a67ed77a03dc6dfd4b8467 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Sat, 16 Dec 2017 16:29:31 -0700 Subject: [PATCH] Remove prefix from Ephemeral driver --- README.md | 16 ++-------------- src/Drivers/Ephemeral.php | 37 +++++++++++-------------------------- tests/CacheTest.php | 10 ---------- 3 files changed, 13 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index efd46c6..62f5104 100644 --- a/README.md +++ b/README.md @@ -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 ----- diff --git a/src/Drivers/Ephemeral.php b/src/Drivers/Ephemeral.php index b7dbb24..19a14e8 100644 --- a/src/Drivers/Ephemeral.php +++ b/src/Drivers/Ephemeral.php @@ -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. @@ -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; } @@ -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; } @@ -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(); } @@ -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); } @@ -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; } @@ -198,7 +183,7 @@ public function forget($key) */ public function flush() { - $this->cache[$this->prefix] = []; + $this->cache = []; return true; } diff --git a/tests/CacheTest.php b/tests/CacheTest.php index 50195e6..9337f7a 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -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);