Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()

Expand All@@ -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()
Expand All@@ -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

Expand Down
4 changes: 3 additions & 1 deletion src/ArrayCache.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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]);
Expand All@@ -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);
}
}
13 changes: 8 additions & 5 deletions src/CacheInterface.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,27 +8,30 @@ 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
*/
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);
}
23 changes: 21 additions & 2 deletions tests/ArrayCacheTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,9 @@

class ArrayCacheTest extends TestCase
{
/**
* @var ArrayCache
*/
private $cache;

public function setUp()
Expand DownExpand Up@@ -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())
Expand All@@ -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(
Expand Down