Laravel Laravel 5.5 Laravel 5.6

3 Best Laravel Redis examples to make your site load faster

Examples-of-using-Redis-with-Laravel-Framework
Written by Yogesh Koli

If you haven’t checked first tutorial on redis then you can visit Beginners guid to use Redis with Laravel Framework I 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 utilise  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 to check to see how we can implement visitor count feature using Redis in your Laravel.

/routes/web.php:

So here we are using visits key to store visitors count, and this key doest exists 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.

2. Laravel Caching with Redis:

A normally usage of Redis is chasing, whenever I thing 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 so to avoid this problem 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:

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. but 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 refetch new data from the database right, so yes we can do that as well and that 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:

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:

so now we are going to use sample example as we have use above only the different is we are going to implement same example using Cache facade from Laravel:

/routes/web.php:

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 go 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 lot’s of different commands documented for different operations.

About the author

Yogesh Koli

Yogesh Koli is a software engineer & a Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 5+ years of experience working with the front-end, back-end, web application development, and system design.

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of