Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
Closed
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
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ against the interface and third parties to provide alternate implementations.

### get
```php
$cache
->get('foo')
->then('var_dump');
$cache
->get('foo')
->then('var_dump');
```

This example fetches the value of the key `foo` and passes it to the
Expand All @@ -25,7 +25,7 @@ If the key `foo` does not exist, the promise will be rejected.

### set
```php
$cache->set('foo', 'bar');
$cache->set('foo', 'bar');
```

This example eventually sets the value of the key `foo` to `bar`. If it
Expand All @@ -35,7 +35,7 @@ it, it may take a while.

### remove
```php
$cache->remove('foo');
$cache->remove('foo');
```

This example eventually removes the key `foo` from the cache. As with `set`,
Expand All @@ -49,10 +49,10 @@ A common use case of caches is to attempt fetching a cached value and as a
fallback retrieve it from the original data source if not found. Here is an
example of that:
```php
$cache
->get('foo')
->then(null, 'getFooFromDb')
->then('var_dump');
$cache
->get('foo')
->then(null, 'getFooFromDb')
->then('var_dump');
```

First an attempt is made to retrieve the value of `foo`. A promise rejection
Expand All @@ -69,24 +69,24 @@ chain will correctly fall back, and provide the value in both cases.
To expand on the fallback get example, often you want to set the value on the
cache after fetching it from the data source.
```php
$cache
$cache
->get('foo')
->then(null, array($this, 'getAndCacheFooFromDb'))
->then('var_dump');

public function getAndCacheFooFromDb()
{
return $this->db
->get('foo')
->then(null, array($this, 'getAndCacheFooFromDb'))
->then('var_dump');

public function getAndCacheFooFromDb()
{
return $this->db
->get('foo')
->then(array($this, 'cacheFooFromDb'));
}

public function cacheFooFromDb($foo)
{
$this->cache->set('foo', $foo);

return $foo;
}
->then(array($this, 'cacheFooFromDb'));
}

public function cacheFooFromDb($foo)
{
$this->cache->set('foo', $foo);

return $foo;
}
```
By using chaining you can easily conditionally cache the value if it is
fetched from the database.
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
"react/promise": "~2.0"
},
"autoload": {
"psr-4": { "React\\Cache\\": "src\\" }
"psr-4": {
"React\\Cache\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"React\\Tests\\Cache\\": "tests"
}
},
"extra": {
"branch-alias": {
Expand Down
20 changes: 10 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="React Test Suite">
<directory>./tests/</directory>
Expand Down
14 changes: 13 additions & 1 deletion src/ArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@

class ArrayCache implements CacheInterface
{
private $data = array();
/**
* @var array
*/
protected $data = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the reason to change this from private to protected?


/**
* {@inheritdoc}
*/
public function get($key)
{
if (!isset($this->data[$key])) {
Expand All @@ -17,11 +23,17 @@ public function get($key)
return Promise\resolve($this->data[$key]);
}

/**
* {@inheritdoc}
*/
public function set($key, $value)
{
$this->data[$key] = $value;
}

/**
* {@inheritdoc}
*/
public function remove($key)
{
unset($this->data[$key]);
Expand Down
15 changes: 14 additions & 1 deletion src/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@

namespace React\Cache;

use React\Promise\PromiseInterface;

interface CacheInterface
{
// @return React\Promise\PromiseInterface
/**
* @param string $key
*
* @return PromiseInterface
*/
public function get($key);

/**
* @param string $key
* @param mixed $value
*/
public function set($key, $value);

/**
* @param string $key
*/
public function remove($key);
}
18 changes: 14 additions & 4 deletions tests/ArrayCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@

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

public function setUp()
{
$this->cache = new ArrayCache();
}

/** @test */
/**
* @test
*/
public function getShouldRejectPromiseForNonExistentKey()
{
$this->cache
Expand All @@ -24,13 +29,16 @@ public function getShouldRejectPromiseForNonExistentKey()
);
}

/** @test */
/**
* @test
*/
public function setShouldSetKey()
{
$this->cache
->set('foo', 'bar');

$success = $this->createCallableMock();

$success
->expects($this->once())
->method('__invoke')
Expand All @@ -41,7 +49,9 @@ public function setShouldSetKey()
->then($success);
}

/** @test */
/**
* @test
*/
public function removeShouldRemoveKey()
{
$this->cache
Expand Down
32 changes: 26 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,60 @@

namespace React\Tests\Cache;

class TestCase extends \PHPUnit_Framework_TestCase
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_TestCase;

class TestCase extends PHPUnit_Framework_TestCase
{
/**
* @param int $amount
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
protected function expectCallableExactly($amount)
{
$mock = $this->createCallableMock();

$mock
->expects($this->exactly($amount))
->method('__invoke');

return $mock;
}

/**
* @return PHPUnit_Framework_MockObject_MockObject
*/
protected function createCallableMock()
{
return $this->getMock('React\Tests\Cache\CallableStub');
}

/**
* @return PHPUnit_Framework_MockObject_MockObject
*/
protected function expectCallableOnce()
{
$mock = $this->createCallableMock();

$mock
->expects($this->once())
->method('__invoke');

return $mock;
}

/**
* @return PHPUnit_Framework_MockObject_MockObject
*/
protected function expectCallableNever()
{
$mock = $this->createCallableMock();

$mock
->expects($this->never())
->method('__invoke');

return $mock;
}

protected function createCallableMock()
{
return $this->getMock('React\Tests\Cache\CallableStub');
}
}
7 changes: 0 additions & 7 deletions tests/bootstrap.php

This file was deleted.