Comments (27)
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.
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.
@M165437 Can you help us on this?
from guzzle-cache-middleware.
@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.
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.
Can you post more of your code? The part you posted above should work.
from guzzle-cache-middleware.
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.
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.
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.
You're welcome! :-)
from guzzle-cache-middleware.
Now only figure out how to change it :-)
from guzzle-cache-middleware.
@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.
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.
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.
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.
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.
It is a Lumen project, can this be a problem?
from guzzle-cache-middleware.
That's quite a valuable info. Did you uncommented the $app->withFacades()
method call in the bootstrap/app.php file?
from guzzle-cache-middleware.
Well, of course, otherwise the caching wouldn't work at all.
from guzzle-cache-middleware.
So, if my example works, it's obviously not a problem with the middleware.
from guzzle-cache-middleware.
yes i did uncommented the $app->withFacades() method.
Does your example work or are you going to test it?
from guzzle-cache-middleware.
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.
Yes you're right I forgot an important detail. Nevertheless thank you very much for the quick help.
from guzzle-cache-middleware.
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.
Can we close this issue?
from guzzle-cache-middleware.
Yes, I think so.
from guzzle-cache-middleware.
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)
- [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.