Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel Cache::store('file') #56

Closed
J-Yen opened this issue May 25, 2016 · 27 comments
Closed

Laravel Cache::store('file') #56

J-Yen opened this issue May 25, 2016 · 27 comments

Comments

@J-Yen
Copy link

J-Yen commented May 25, 2016

Does the following code work?

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

Where is the cache saved? Normally in storage/framework/cache but i don't find the cache file.

@Kevinrob
Copy link
Owner

@M165437 Can you help us on this?

@M165437
Copy link
Contributor

M165437 commented May 25, 2016

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

@J-Yen
Copy link
Author

J-Yen commented May 25, 2016

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.

@M165437
Copy link
Contributor

M165437 commented May 25, 2016

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

@J-Yen
Copy link
Author

J-Yen commented May 25, 2016

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);
}

@M165437
Copy link
Contributor

M165437 commented May 25, 2016

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());
}

@J-Yen
Copy link
Author

J-Yen commented May 25, 2016

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!

@M165437
Copy link
Contributor

M165437 commented May 25, 2016

You're welcome! :-)

@J-Yen
Copy link
Author

J-Yen commented May 25, 2016

Now only figure out how to change it :-)

@M165437
Copy link
Contributor

M165437 commented May 25, 2016

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

@Kevinrob
Copy link
Owner

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)

@J-Yen
Copy link
Author

J-Yen commented May 25, 2016

So i have changed my code to:

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

But still no caching...

@M165437
Copy link
Contributor

M165437 commented May 25, 2016

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();
});

@M165437
Copy link
Contributor

M165437 commented May 25, 2016

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

@J-Yen
Copy link
Author

J-Yen commented May 25, 2016

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?

@J-Yen
Copy link
Author

J-Yen commented May 25, 2016

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

@M165437
Copy link
Contributor

M165437 commented May 25, 2016

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

@M165437
Copy link
Contributor

M165437 commented May 25, 2016

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

@M165437
Copy link
Contributor

M165437 commented May 25, 2016

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

@J-Yen
Copy link
Author

J-Yen commented May 25, 2016

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

@M165437
Copy link
Contributor

M165437 commented May 25, 2016

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.

@J-Yen
Copy link
Author

J-Yen commented May 25, 2016

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

@M165437
Copy link
Contributor

M165437 commented May 26, 2016

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

@Kevinrob
Copy link
Owner

Can we close this issue?

@M165437
Copy link
Contributor

M165437 commented Jun 17, 2016

Yes, I think so.

@tuanpht
Copy link

tuanpht commented Jul 7, 2017

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.

@randhipp
Copy link

randhipp commented May 9, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants