How to Use Laravel Caching to Speed Website Loading Time
Laravel caching is storing copies of data in temporary storage, this feature speed up your website loading time, so let's assume that we have a blog and we want to cache posts for 1 hour.
Store cache
Inside our PostController, we use the Cache facade to remember posts for 1 hour = 3600 seconds.
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
class PostController extends Controller
{
//
public function index(){
$posts = Cache::remember('posts', 3600, function() {
return Post::latest()->paginate(10);
});
return view('home')->with([
'posts' => $posts,
]);
}
}