Comments (5)
Hi,
of course!
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;
// Create default HandlerStack
$stack = HandlerStack::create();
// Create the cache middleware
// You can customise the cache storage, see the doc
$cache = new CacheMiddleware();
// Add this middleware to the top with `push`
$stack->push($cache, 'cache');
// Initialize the client with the handler option
$client = new Client(['handler' => $stack]);
// Now you can use Guzzle...
To test if the cache HIT, you can check the header:
$response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO)
If you have any problem, tell me!
from guzzle-cache-middleware.
If you need more help, re-open this issue.
from guzzle-cache-middleware.
Hello! I just needed to do the exact same thing in my tests and your code worked perfectly (it returns "HIT" or "MISS").
However, when I try to do something like this :
$this->assertEquals(304, $response->getStatusCode());
$this->assertEmpty($response->getBody()->getContents());
It fails and gives me the same data as the first request (200, with a body). Any idea why ? I'm using Symfony2's $response->isNotModified()
to check if it should return the cached response with a 304 status code and empty content.
from guzzle-cache-middleware.
@gnutix The cache will change code from 304
to 200
and fill the body
with the cache. It's transparent from the caller.
You can check CacheMiddleware::HEADER_CACHE_INFO
header for debugging/testing information.
from guzzle-cache-middleware.
OK thanks!
from guzzle-cache-middleware.
Related Issues (20)
- [question] how to define time how long should the cache live? HOT 1
- Middleware does not cache 201 status code HOT 1
- Flysystem returns "File not Found" error on DELETE, POST, PUT calls HOT 1
- Incompatibility with Guzzle7? HOT 2
- "X-Kevinrob-Cache: MISS" although response is from cache HOT 1
- [Question] Specifying TTL For Laravel Cache HOT 3
- [Question] Redis - Deleting Cached Items Once Expired HOT 2
- Different TTL with GreedyCacheStrategy and RequestMatcher HOT 2
- igbinary_serialize(): Cannot serialize resource(stream) HOT 13
- Request with unsafe method does not invalidate cache HOT 1
- Drop doctrine/cache
- Response object without body? HOT 2
- CacheEntry::__sleep mutates response body to PumpStream which is read once
- Storing PHP serialized data in caches MUST be avoided HOT 2
- Not compatible with gmponos/guzzle_logger HOT 2
- POST request key generation. HOT 2
- Argument #1 ($stream) must be of type resource, int given HOT 15
- Facing SIGSEGV sometimes HOT 1
- Missing dependency on guzzlehttp/promises and not compatible with v2.0 HOT 3
- Undefined array key "request" HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from guzzle-cache-middleware.