
Introduction
This method accepts three key parameters, duration, and data to be cached.
Cache::put():
Let's take a look at how to use Cache::put()
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;
Route::get('/put-cache, function(){
$posts = Post::all();
$duration = Carbon::now()->addMinutes(1);
Cache::put('posts', $posts, $duration);
});
In the above example:
- Posts is the cache key.
- Posts is the data you want to store in the cache.
- Duration represents the expiration time in minutes After 1 minute, the cached value associated with the key will expire and be removed from the cache.
Cache::get():
Route::get('/get-cache', function(){
if(Cache::has('posts')){
$posts = Cache::get('posts');
dd($posts);
}
});
* Please Don't Spam Here. All the Comments are Reviewed by Admin.