diff --git a/README.md b/README.md index 8ccaf89..2545cda 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,8 @@ This example fetches the value of the key `foo` and passes it to the `var_dump` function. You can use any of the composition provided by [promises](https://github.com/reactphp/promise). -If the key `foo` does not exist, the promise will be fulfilled with `null` as value. +If the key `foo` does not exist, the promise will be fulfilled with `null` as value. On +any error it will also resolve with `null`. #### set() @@ -56,8 +57,9 @@ $cache->set('foo', 'bar'); ``` This example eventually sets the value of the key `foo` to `bar`. If it -already exists, it is overridden. No guarantees are made as to when the cache -value is set. If the cache implementation has to go over the network to store +already exists, it is overridden. To provide guarantees as to when the cache +value is set a promise is returned. The promise will fulfill with `true` on success +or `false` on error. If the cache implementation has to go over the network to store it, it may take a while. #### remove() @@ -67,7 +69,8 @@ $cache->remove('foo'); ``` This example eventually removes the key `foo` from the cache. As with `set`, -this may not happen instantly. +this may not happen instantly and a promise is returned to provide guarantees whether +or not the item has been removed from cache. ### ArrayCache diff --git a/src/ArrayCache.php b/src/ArrayCache.php index 6da1574..f4bf9e8 100644 --- a/src/ArrayCache.php +++ b/src/ArrayCache.php @@ -11,7 +11,7 @@ class ArrayCache implements CacheInterface public function get($key) { if (!isset($this->data[$key])) { - return Promise\resolve(null); + return Promise\resolve(); } return Promise\resolve($this->data[$key]); @@ -20,10 +20,12 @@ public function get($key) public function set($key, $value) { $this->data[$key] = $value; + return Promise\resolve(true); } public function remove($key) { unset($this->data[$key]); + return Promise\resolve(true); } } diff --git a/src/CacheInterface.php b/src/CacheInterface.php index c4f5a2f..da6661f 100644 --- a/src/CacheInterface.php +++ b/src/CacheInterface.php @@ -8,7 +8,7 @@ interface CacheInterface { /** * Retrieve an item from the cache, resolves with its value on - * success or null when no item can be found. + * success or null when no item can be found or when an error occurs. * * @param string $key * @return PromiseInterface @@ -16,19 +16,22 @@ interface CacheInterface public function get($key); /** - * Store an item in the cache. + * Store an item in the cache, returns a promise which resolves to true on success or + * false on error. * * @param string $key * @param mixed $value - * @return void + * @return PromiseInterface Returns a promise which resolves to true on success of false on error */ public function set($key, $value); /** - * Remove an item from the cache. + * Remove an item from the cache, returns a promise which resolves to true on success or + * false on error. When the $key isn't found in the cache it also + * resolves true. * * @param string $key - * @return void + * @return PromiseInterface Returns a promise which resolves to true on success of false on error */ public function remove($key); } diff --git a/tests/ArrayCacheTest.php b/tests/ArrayCacheTest.php index 9ebed3c..f98fc0e 100644 --- a/tests/ArrayCacheTest.php +++ b/tests/ArrayCacheTest.php @@ -6,6 +6,9 @@ class ArrayCacheTest extends TestCase { + /** + * @var ArrayCache + */ private $cache; public function setUp() @@ -33,9 +36,17 @@ public function getShouldResolvePromiseWithNullForNonExistentKey() /** @test */ public function setShouldSetKey() { - $this->cache + $setPromise = $this->cache ->set('foo', 'bar'); + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->identicalTo(true)); + + $setPromise->then($mock); + $success = $this->createCallableMock(); $success ->expects($this->once()) @@ -53,9 +64,17 @@ public function removeShouldRemoveKey() $this->cache ->set('foo', 'bar'); - $this->cache + $removePromise = $this->cache ->remove('foo'); + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->identicalTo(true)); + + $removePromise->then($mock); + $this->cache ->get('foo') ->then(