How to Store Data in Cache in Laravel 10 Tutorial

Reading Time: 6 minutes

Caching is an important approach in web development for improving application performance and reducing database load. Caching data in Laravel 10 lets you to keep frequently visited or computationally expensive data in memory, allowing for faster retrieval and response times for subsequent queries.

This tutorial will walk you through the process of putting data in the cache in Laravel 10. We’ll look at the many caching drivers offered by Laravel, such as the file, database, Redis, and Memcached drivers. You will learn how to use the expressive syntax and methods provided by Laravel’s caching system to configure caching, save data, and retrieve cached information.

Read More: Laravel 10 Upload and Save XML File Data in Database

Let’s get started.

What Is Laravel Caching?

Laravel caching is a significant feature of the Laravel PHP framework that helps you to boost application performance and efficiency. Without writing code, you can easily configure and switch between several cache engines with Laravel.

To configure Laravel’s caching options, you can use a single configuration file that contains all of your application’s settings. Furthermore, Laravel supports all popular caching backends out of the box, such as Memcached and Redis, and provides a uniform API for interfacing with diverse caching frameworks.

Why Caching Is Important?

Caching is a technique for temporarily keeping data at a faster, more easily available location, eliminating the need to constantly obtain it from a slower storage tier. Caching is crucial because it can increase application performance dramatically by reducing the time it takes to fetch data, resulting in faster response times and a better user experience.

This is particularly useful for applications that deal with massive amounts of data, such as e-commerce sites, social networks, and search engines.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve


Assuming laravel already installed inside your system.

Concept of Facade Cache Class

To store data here we will use the concept of cache. Laravel facade cache class provides several methods which we can use to put values or data.

Read More: Laravel 10 Remove Composer Package Tutorial


use Illuminate\Support\Facades\Cache;

Simply you can import above class into your controller and use it’s methods to work with caches.


There are several methods which we can use according to case. If suppose we are storing values of variables then we have methods of it, if we store large data set different methods to use.

Storing Items In The Cache

# Store a single item
Cache::put('key', 'value', $seconds = 10);

# Store large data
$value = Cache::remember('users', $seconds, function () {
     return DB::table('users')->get();

If the storage time will not be specified to the put method, the item will be stored indefinitely.

# Store for forever
Cache::put('key', 'value');

$value = Cache::rememberForever('users', function () {
     return DB::table('users')->get();

Accessing Item Value

$value = Cache::get('key');

Read More: Top 40 Pixwox Alternatives for Viewing and Downloading Instagram Stories

Checking For Item Existence

The has method may be used to determine if an item exists in the cache. This method will also return false if the item exists but its value is null.

if (Cache::has('key')) {

Removing Items From The Cache


You can learn more about cache class from here.

Usage of Cache Facade in Application

Suppose we have a devices table in which data rows approx 1000. If we hit this table every time for some conditional query then site speed, it’s performance will be down.

We will use the concept of facade cache class and it’s methods to store, fetch and remove.

Store devices conditional data for 60 seconds.

Cache::remember('devicesData', 60, function()
     return  DB::table('devices')
             "SOME COMPLEX JOINS ETC.."

devicesData is the cache key which you can use to get stored data.

Get Data from cache

$data = Cache::get('devicesData');

Read More: Laravel 10 Custom Validation Rule Tutorial

Remove Cached data


We hope this article helped you to learn How to Store Data in Cache in Laravel 10 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.