Let's say I want to obtain data from an External REST API through this. How would I cache this Data to e.g. only have max. 1 Request per Minute to the external API Endpoint with Laravel's Caching? Is there anything I can enable or any packages I can use to achieve this? Or what would I have to do?
Hm yeah that would work I just thought there's maybe a direct way to cache Responses (so when calling the Endpoint, I get the cached version)
Would this work?
To my knowledge, guzzle does not support caching. Also I would personally use laravel cache to make sure that I have full control. My own example would be exactly how I would do it
From how I understand it, your Code, runs the Query and caches that, however, I have no Idea how I'd then get the Information. Because I basically needs a function that, if the Request is cached, returns that. And if not, requests, caches and then returns that
@crugg You can wrap your requests in a repository that has a caching layer. You would then interact with this repository class.
class FooRepository
{
public function fooRequest()
{
$key = 'cache.key.here';
$ttl = 60; // 60 seconds / 1 minute
return Cache::remember($someKey, $ttl, function () {
return Http::get('http://example.com/api');
});
}
}
protected $foos;
public function __construct(FooRepository $foos)
{
$this->foo = $foos;
}
public function someClassMethod()
{
$response = $this->foos->fooRequest(); // will cache
}