Giter Site home page Giter Site logo

Comments (5)

Kevinrob avatar Kevinrob commented on May 20, 2024

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.

Kevinrob avatar Kevinrob commented on May 20, 2024

If you need more help, re-open this issue.

from guzzle-cache-middleware.

gnutix avatar gnutix commented on May 20, 2024

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.

Kevinrob avatar Kevinrob commented on May 20, 2024

@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.

gnutix avatar gnutix commented on May 20, 2024

OK thanks!

from guzzle-cache-middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.