Laravel:Cache::put() & Cache::get()

 73
Laravel:Cache::put() & Cache::get()

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);
    }
});

Post a Comment

0Comments

* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Post a Comment (0)