Skip to content

Commit

Permalink
Merge pull request #84 from bpolaszek/null-pattern
Browse files Browse the repository at this point in the history
Null object pattern
  • Loading branch information
Kevinrob authored Jul 10, 2017
2 parents aa9ddb6 + 4458af0 commit 8ec8d1b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Storage/LaravelCacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function save($key, CacheEntry $data)
$lifeTime = $data->getTTL() / 60;
if ($lifeTime === 0) {
return $this->cache->forever(
$key,
$key,
serialize($data)
);
} else if ($lifeTime > 0) {
Expand Down
1 change: 0 additions & 1 deletion src/Storage/VolatileRuntimeStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ public function save($key, CacheEntry $data)

return true;
}

}
35 changes: 35 additions & 0 deletions src/Strategy/NullCacheStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Kevinrob\GuzzleCache\Strategy;

use Kevinrob\GuzzleCache\CacheEntry;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class NullCacheStrategy implements CacheStrategyInterface
{

/**
* @inheritDoc
*/
public function fetch(RequestInterface $request)
{
return null;
}

/**
* @inheritDoc
*/
public function cache(RequestInterface $request, ResponseInterface $response)
{
return true;
}

/**
* @inheritDoc
*/
public function update(RequestInterface $request, ResponseInterface $response)
{
return true;
}
}
36 changes: 36 additions & 0 deletions tests/NullCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Kevinrob\GuzzleCache;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Kevinrob\GuzzleCache\Strategy\NullCacheStrategy;
use PHPUnit\Framework\TestCase;

class NullCacheTest extends TestCase
{

public function testCacheIsNeverHit()
{
$stack = HandlerStack::create(new MockHandler([
new Response(),
new Response(),
]));

$stack->push(new CacheMiddleware(new NullCacheStrategy()));

$request = new Request('GET', '/foo');

$client = new Client([
'handler' => $stack
]);
$client->send($request); // Will not be cached
$response = $client->send($request); // Will not come from cache

$this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO));
}

}

0 comments on commit 8ec8d1b

Please # to comment.