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 –
- Customization of Markdown Mailable Email Templates
- Eloquent Mutators and Accessors in Laravel 8 Tutorial
- File or Image Upload Using Livewire Laravel 8 Tutorial
- Generate QR Code Laravel 8 Example
Let’s get started.
Laravel Installation
We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.
Here is the command to create a laravel project-
composer create-project --prefer-dist laravel/laravel blog
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;
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.
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.