How To Store Data in Laravel Cache Tutorial

Share this Article
Reading Time: 4 minutes
403 Views

Inside this article we will see the concept of store data in laravel cache. How to use cache in laravel 8 to store data or say any record 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.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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;

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 How To Store Data in Laravel Cache Tutorial in a very detailed way.

Buy Me a Coffee

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.

Find More on Laravel 8 Articles here