-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c573ac6
commit 5ae8a80
Showing
5 changed files
with
116 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ | |
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.5-dev" | ||
"dev-master": "1.6-dev" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Http\Client\Common\Plugin\Cache\Listener; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Cache\CacheItemInterface; | ||
|
||
/** | ||
* Adds a header indicating if the response came from cache. | ||
* | ||
* @author Iain Connor <iain.connor@priceline.com> | ||
*/ | ||
class AddHeaderCacheListener implements CacheListener | ||
{ | ||
/** @var string */ | ||
private $headerName; | ||
|
||
/** | ||
* @param string $headerName | ||
*/ | ||
public function __construct($headerName = 'X-Cache') | ||
{ | ||
$this->headerName = $headerName; | ||
} | ||
|
||
/** | ||
* Called before the cache plugin returns the response, with information on whether that response came from cache. | ||
* | ||
* @param RequestInterface $request | ||
* @param ResponseInterface $response | ||
* @param bool $fromCache Whether the `$response` was from the cache or not. | ||
* Note that checking `$cacheItem->isHit()` is not sufficent to determine this. | ||
* @param CacheItemInterface|null $cacheItem | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function onCacheResponse(RequestInterface $request, ResponseInterface $response, $fromCache, $cacheItem) | ||
{ | ||
return $response->withHeader($this->headerName, $fromCache ? 'HIT' : 'MISS'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Http\Client\Common\Plugin\Cache\Listener; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Cache\CacheItemInterface; | ||
|
||
/** | ||
* Called by the cache plugin with information on the cache status. | ||
* Provides an opportunity to update the response based on whether the cache was a hit or a miss, or | ||
* other cache-meta-data. | ||
* | ||
* @author Iain Connor <iain.connor@priceline.com> | ||
*/ | ||
interface CacheListener | ||
{ | ||
/** | ||
* Called before the cache plugin returns the response, with information on whether that response came from cache. | ||
* | ||
* @param RequestInterface $request | ||
* @param ResponseInterface $response | ||
* @param bool $fromCache Whether the `$response` was from the cache or not. | ||
* Note that checking `$cacheItem->isHit()` is not sufficent to determine this. | ||
* @param CacheItemInterface|null $cacheItem | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function onCacheResponse(RequestInterface $request, ResponseInterface $response, $fromCache, $cacheItem); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters