DaTransportGuru Posted January 20, 2020 Share Posted January 20, 2020 Hi, I was unable to post this in the developer section, so hopefully someone can move it to the right place. I am trying to use the Truckers MP PHP API package with Laravel. I have the following code to get the servers and cache them: $servers = Cache::remember('servers', 10, function () { if (Cache::has('servers')) { return Cache::get('servers'); } else { $client = new Client(); return $client->servers()->get(); } }); return view('layouts.servers', ['servers' => $servers]); I don't think it is working correctly because it seems to refresh quicker than the cache duration (10 minutes). Not sure what's missing or wrong. Any help would be appreciated. Link to comment Share on other sites More sharing options...
Chev Posted January 20, 2020 Share Posted January 20, 2020 //Moved to the Developer Portal. 1 Have a question? Feel free to message me. Feedback | Rules | Support | News | Meet the Team Link to comment Share on other sites More sharing options...
CJMAXiK Posted January 20, 2020 Share Posted January 20, 2020 You don't need to check the cache entry in that closure, `remember` will do it for you. Also, timeout is in seconds, not in minutes. 1 1 Link to comment Share on other sites More sharing options...
kocalparslan97 Posted July 30, 2023 Share Posted July 30, 2023 The Cache::remember method automatically checks if the data exists in the cache and, if not, executes the given closure to retrieve and cache the data. Quote $servers = Cache::remember('servers', 600, function () { // Check if 'servers' key exists in the cache if (Cache::has('servers')) { // Return the data from the cache return Cache::get('servers'); } else { // If 'servers' key doesn't exist, fetch data from the API $client = new Client(); $serversData = $client->servers()->get(); // Cache the fetched data for 10 minutes (600 seconds) Cache::put('servers', $serversData, 600); // Return the data return $serversData; } }); The cache duration is set to 600 seconds (10 minutes) in the Cache::remember method. We first check if the 'servers' key exists in the cache using Cache::has. If the key exists, we return the data from the cache using Cache::get. If the key doesn't exist, we fetch the data from the API and cache it using Cache::put. Finally, we return the fetched data. Karakteriniz düzgün olsun ki gerisini hallederiz.. My first condition is that your character is not broken... AlparslanK Link to comment Share on other sites More sharing options...
Recommended Posts