
Introduction:
If you haven’t checked first tutorial on Redis then you can visit Beginners guide to use Redis with Laravel Framework have provided all basic required details there starting from defining Redis, advantages or using Redis, installing Redis and Redis configuration with Laravel project, in this tutorial I am going to provide examples of using Redis with Laravel framework, by reading this tutorial you will get real idea on you can utilize Redis to improve your application performance, let’s begin to learn.
Laravel has provided inbuilt Redis Facade, which can help us to execute Redis commands easily in next few examples I will be using Redis facades to demonstrate the use of Redis, let’s see how we can use Redis facade in next example.
1. Determine Website Visitors count, or Page Visitors count:
We can use Redis to store visitors count for particular object it can be a website or single page from the website such as a blog post or any other article page and so on.
I assume you have your Laravel project open in your code editor:
Next go Ahed and open up the web.php
file from routes folder and write down the following example using Redis facade. Check to see how we can implement visitor count feature using Redis in your Laravel.
Route::get('/', function () {
$visits = Redis::incr('visits');
return $visits;
});
So here we are using visits key to store visitors count, and this key doesn’t exist yet, but the important thing is Redis still going to work because Redis is going to create that key and increment by one.
That means by default it starts from zero and it will be set to one if you access routes from your browser so on each page load it will increment by one.
Try to load the page and refresh again and again to see the number incrementing. i told you redis is incredibly simple, with just one line of code we get the visitors counts for the website, and if you want it to at page level count, then simple just create multiple unique key associated to the page id or the page link.
New 8 Laravel 6 Feature you should be aware of in 2019 – New Features and Updates
2. Laravel Caching with Redis:
A normally usage of Redis is chasing, whenever I think of chasing implementation the first thing came in my mind is Redis, Redis is fast and easy to use when it comes to manage cache with Laravel, most of the development usually uses Redis. so, let’s see an example on caching to see how you can use it as well.
Let’s assume that you have bunch of records in your database in your project, it can be anything such as users, articles, news records or something related to other subject important thing the data.
So, whenever a single user visits to your site you want to fetch those records from the database and show them to the user and this process is basically same for every subsequence request, so of course your database going to have multiple queries coming along to execute.
To avoid this issue we can use cache, so we can just fetch records once from the database and cache into the Redis and then all of future request will have the records into the cache and your application performance will increase as your taking care of database load.
Basic cache example using Redis:
/routes/web.php:
use Illuminate\Support\Facades\Redis;
Route::get('/', function () {
// redis has posts.all key exists
// posts found then it will return all post without touching the database
if ($posts = Redis::get('posts.all')) {
return json_decode($posts);
}
// get all post
$posts = Post::all();
// store into redis
Redis::set('posts.all', $posts);
// return all posts
return $posts;
});
So, checkout the above example it very simple, we are checking to see if the Redis has records then fetch from Redis and return it to the user
If not, then fetch records from the database remember into Redis and return it to the user.
However, the above example is good when you want to remember data forever but it not when you want to store a data which is basically going to update in the future may be in next 24 hours, you probably gone need to delete the Redis data with decide timestamp and then re-fetch new data from the database right?
so yes, we can do that as well and that is the right use case unless you want to cache data forever, let see that in next example.
Cache Records with expiration or Timeout:
/routes/web.php:
use Illuminate\Support\Facades\Redis;
Route::get('/', function () {
// check if redis has posts.all key exits
// if posts found then it will return all post without touching the database
if ($posts = Redis::get('posts.all')) {
return json_decode($posts);
}
// get all post
$posts = Post::all();
// store data into redis for next 24 hours
Redis::setex('posts.all', 60 * 60 * 24, $posts);
// return all posts
return $posts;
});
So, as you can see, we have updated first cache example, what I did is just changed set method to setex
which supports data timeout, or we can say data expiration time.
So now let’s use Laravel predefine cache along with Redis to manage application cache in the next example:
Use Redis Cache Drive in Laravel:
Before going to use Redis with Laravel application, make sure you have updated cache driver variable from .env file:
.env:
CACHE_DRIVER=redis
So now we are going to use sample example as above, only the different will be – we are going use Cache
facade from Laravel instead of Redis
:
/routes/web.php:
use Illuminate\Support\Facades\Cache;
Route::get('/', function () {
return Cache::remember('posts.all', 60 * 60 * 24, function () {
return Post::all();
});
});
I hope you get the idea on using Redis with Laravel application, keep playing with it try to implement few more different examples and then you should be good to go.
If you want to learn more about Redis commands just visit https://redis.io/commands and check out the command according to your needs it has lots of different commands documented for different operations.
Very helpful examples! Good work. Keep that Up!