Laravel 9 How To Store Data in Cache Tutorial

Reading Time: 5 minutes
2,373 Views

Inside this article we will see the concept of store data in laravel cache. laravel 9 How to Store data in cache or database record in cache we will the detailed concept here.

Tutorial will give you a clear vision of laravel cache to store data and also use it when need. Article contains a classified information.

If you are working with large queries in which you required large number of data set. Every time when you do this, your application may slow down the site speed as well as it’s performance. To overcome this case you may use laravel cache functionality.

Laravel cache provides to store any amount of data and also it provides methods to fetch and remove it.

Learn More –

Let’s get started.

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

URL: http://127.0.0.1:8000

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.

Load

use Illuminate\Support\Facades\Cache;

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

Methods

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 is not passed 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');

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

Cache::forget('key');

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')
         ->select(DB::raw(
             "SOME COMPLEX JOINS ETC.."
         ))->get();
});

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

Get Data from cache

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

Remove Cached data

Cache::forget('devicesData');

We hope this article helped you to learn Laravel 9 How To Store Data in Cache 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.