Code Monkey home page Code Monkey logo

Comments (27)

tuanpht avatar tuanpht commented on May 21, 2024 2

I use this to check if cache works. Hope it help someone :)

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Illuminate\Support\Facades\Cache;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Storage\LaravelCacheStorage;
use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy;

Route::get('/guzzle-cache-test', function () {
    $stack = HandlerStack::create();

    $stack->push(new CacheMiddleware(
        new GreedyCacheStrategy(
            new LaravelCacheStorage(
                Cache::store('file')
            ), 5
        )
    ), 'cache');

    $client = new Client(['handler' => $stack]);
    $responseBody = $client->get('http://localhost:1234/time.php')->getBody();

    return $responseBody->getContents();
});
// http://localhost:1234/time.php
echo 'SERVER TIME: ' . date('Y-m-d H:i:s') . PHP_EOL;

If cache works, time will be increased by 5 seconds.

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024 1

Try this minimal example in App/Http/routes.php. This works for me.

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Illuminate\Support\Facades\Cache;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Storage\LaravelCacheStorage;
use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy;

Route::get('/guzzle-cache-test', function () {
    $stack = HandlerStack::create();

    $stack->push(new CacheMiddleware(
        new GreedyCacheStrategy(
            new LaravelCacheStorage(
                Cache::store('file')
            ), 5000
        )
    ), 'cache');

    $client = new Client(['handler' => $stack]);
    $responseBody = $client->get('http://github.com')->getBody();

    return $responseBody->getContents();
});

from guzzle-cache-middleware.

Kevinrob avatar Kevinrob commented on May 21, 2024

@M165437 Can you help us on this?

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

@J-Yen Have a look at the file config/cache.php and https://laravel.com/docs/5.2/cache. Make sure caching works outside of the Guzzle middleware before using it, e.g. in the router

Route::get('/', function () {
    Cache::store('file')->put('hello', 'world', 5);
    $value = Cache::store('file')->get('hello');
    return $value; // -> world
});

By the way, it's not one cache file but rather a folder structure. Correct, by default in storage/framework/cache.

from guzzle-cache-middleware.

J-Yen avatar J-Yen commented on May 21, 2024

Caching already works outside the Guzzle middleware and i already use it. Also your example works.
But with the middleware, no new directory or file is created in the folder structure.

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

Can you post more of your code? The part you posted above should work.

from guzzle-cache-middleware.

J-Yen avatar J-Yen commented on May 21, 2024
class ConnectorService
{
    private $connectionUrl = 'http://...';
    private $client;

    function __construct()
    {
        $stack = HandlerStack::create();

        $stack->push(
            new CacheMiddleware(
                new PrivateCacheStrategy(
                    new LaravelCacheStorage(
                        Cache::store('file')
                    )
                )
            ),
            'cache'
        );
        $this->client = new GuzzleHttp\Client(['handler' => $stack]);
    }



public function post_request($postArgs)
 {
        $response = $this->client->request('POST', $this->connectionUrl, [
            'form_params' => $postArgs,
        ])->getBody();
        return json_decode($response);
}

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

Looks good. What about the response headers? Are you sure the response wants to be cached? You can check the headers with Laravel's dd() (dump and die) method, although you'd need to remove the ->getBody() from your client request:

public function post_request($postArgs)
 {
        $response = $this->client->request('POST', $this->connectionUrl, [
            'form_params' => $postArgs,
        ]);
        dd($response->getHeaders()); // temporarily
        return json_decode($response->getBody());
}

from guzzle-cache-middleware.

J-Yen avatar J-Yen commented on May 21, 2024

Ow yes that is the cause.

["Cache-Control"]=>
  array(1) {
    [0]=>
    string(62) "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
  }
  ["Pragma"]=>
  array(1) {
    [0]=>
    string(8) "no-cache"
  }

Thank you very much!

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

You're welcome! :-)

from guzzle-cache-middleware.

J-Yen avatar J-Yen commented on May 21, 2024

Now only figure out how to change it :-)

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

@Kevinrob Honestly, I had the same struggle initially. :-) The Cache-Control header might be worth a paragraph in the README file.

from guzzle-cache-middleware.

Kevinrob avatar Kevinrob commented on May 21, 2024

You can use the GreedyCacheStrategy. It doesn't care about headers from the server.
Yeah, I think that it can be added to the README 😄 (#42)

from guzzle-cache-middleware.

J-Yen avatar J-Yen commented on May 21, 2024

So i have changed my code to:

$stack->push(
   new CacheMiddleware(
        new GreedyCacheStrategy(
            new LaravelCacheStorage(
                Cache::store('file')
            ),
            5000
        )
    ),
    'cache'
);

But still no caching...

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

What about File Permissions? Have you checked your error logs? Maybe Laravel isn't able to write the cache to the file system?

from guzzle-cache-middleware.

J-Yen avatar J-Yen commented on May 21, 2024

No errors in the error logs. It should be able to write the cache to the file system because it's working with your example and in the rest of my code?

from guzzle-cache-middleware.

J-Yen avatar J-Yen commented on May 21, 2024

It is a Lumen project, can this be a problem?

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

That's quite a valuable info. Did you uncommented the $app->withFacades() method call in the bootstrap/app.php file?

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

Well, of course, otherwise the caching wouldn't work at all.

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

So, if my example works, it's obviously not a problem with the middleware.

from guzzle-cache-middleware.

J-Yen avatar J-Yen commented on May 21, 2024

yes i did uncommented the $app->withFacades() method.
Does your example work or are you going to test it?

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

It works in Laravel. Lumen and Laravel are not the same thing. Something that works in Lumen will probably work in Laravel but not necessarily vice versa. Next time please specify your framework up front. The Laravel interface was created for and tested with Laravel. I don't know if it works with Lumen.

from guzzle-cache-middleware.

J-Yen avatar J-Yen commented on May 21, 2024

Yes you're right I forgot an important detail. Nevertheless thank you very much for the quick help.

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

Out of curiosity I set up a Lumen project. The route example above works fine. So the LaravelCacheStorage interface is Lumen compatible as well. :-)

from guzzle-cache-middleware.

Kevinrob avatar Kevinrob commented on May 21, 2024

Can we close this issue?

from guzzle-cache-middleware.

M165437 avatar M165437 commented on May 21, 2024

Yes, I think so.

from guzzle-cache-middleware.

randhipp avatar randhipp commented on May 21, 2024

Try this minimal example in App/Http/routes.php. This works for me.

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Illuminate\Support\Facades\Cache;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Storage\LaravelCacheStorage;
use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy;

Route::get('/guzzle-cache-test', function () {
    $stack = HandlerStack::create();

    $stack->push(new CacheMiddleware(
        new GreedyCacheStrategy(
            new LaravelCacheStorage(
                Cache::store('file')
            ), 5000
        )
    ), 'cache');

    $client = new Client(['handler' => $stack]);
    $responseBody = $client->get('http://github.com')->getBody();

    return $responseBody->getContents();
});

This method working for me

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.