Laravel problem when caching HTTP Client response

865 Views Asked by At

I use the file as cache driver and I want to cache the response from HTTP Client, but when I first time call a function everything works but when I call the function a second time to get data from cache I get this error

fseek(): Argument #1 ($stream) must be of type resource, int given

There is code I use for caching

public function manufacturers(){
   return Cache::remember('manufacturers', 10, function () {
      return Http::withoutVerifying()->post(config('api.systems').'Manufacturers/filter', [
          'showAll' => true
      ]);
   });
}

I tried other cache drivers and it works okay, I tried also to cache some other data instead of HTTP Client response and it also works fine.

1

There are 1 best solutions below

0
On BEST ANSWER

From what I remember, the Cache::remember method is only able to cache simple data types such as strings and arrays. It is not able to cache a file resource like a response from the HTTP client. First, try to convert the response to a simpler data type before caching it.

public function manufacturers() {
   return Cache::remember('manufacturers', 10, function () {
      $response = Http::withoutVerifying()->post(config('api.systems').'Manufacturers/filter', [
          'showAll' => true
      ]);

      $json = $response->json();
      $data = JSON::decode($json);

      return $data;
   });
}