Caching or storing frequently accessed data in memory is one of the proven ways to improve site speed.
But, does Laravel website support caching?
Yes.
Installing Redis cache on Laravel really helps to speed up your web app. But, this involves configuration changes too.
At Bobcares, we usually get requests to setup Redis cache as part of our Outsourced Technical Services.
Today, we’ll see how our Dedicated Engineers quickly install Redis on Laravel framework and fix common errors with it.
Why use Redis cache?
Redis gives a structured way to store data in server memory.
Before proceeding further, we need to know why to choose Redis cache in Laravel. Luckily, Redis comes with couple of benefits. Let’s now take a look at them.
- Fast – Redis is really quick in processing the data in server memory.
- Persistence – Redis has persistence of data. After a server crash, it can quickly load data from backup files and put them into server memory.
- Messaging support – It has messaging feature and sends real-time notification to active users.
- Transaction support – Redis can run process one after another, or in other words, it supports queuing of commands.
With all these benefits, Redis is the most popular NoSQL database in containers.
Additionally, in Redis, we can set a maximum memory size. Redis will never use more memory than this limit. Also, it gives unused memory back into the memory pool.
How to install Redis in Laravel?
So far, we found the various benefits of using Redis as the caching mechanism in Laravel. We’ll now see how our Dedicated Support Engineers install and configure Redis on Laravel framework.
1. Setup Redis server
As the first step, we ensure that the server has all the latest packages. Then, we install the Redis server package.
For example, in a CentOS7 server, the exact sequence of commands will be:
yum install redis
systemctl start redis
systemctl enable redis
That completes the installation. To check the status of Redis service, we use:
systemctl status redis.service
The successful result will show up as
[root@xxx ~]# systemctl status redis.service
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
2. Configure Redis for Laravel
Next, we need to configure Redis for Laravel. For this, we need to install the predis package. Our Support Engineers setup it using:
composer require predis/predis
Moving on, we check and ensure the built-in Redis configuration options in the file config/database.php
The default configuration works for normal Laravel development. However, for customers that need further tweaking, we adjust the values in the Redis configuration file.
Common errors with Redis Laravel setup
Although, the Redis Laravel setup work perfectly most of the time, it may show up errors too. Let’s take a look at them now.
1. Related Module Support
Usually, the Laravel Redis errors happen when the dependency packages are not available on the server. One such error is
Class 'Predis\Client' not found in Laravel
In this case, the server was not having the predis package.
To fix, our Support Engineers had to add predis/predis into composer.json for the project. From the console, in project folder we executed:
composer require predis/predis
2. Redis connection [cache] not configured
Similarly, often Laravel Redis setup can show up the error:
Redis connection [cache] not configured
This mainly happens due to the wrong configuration of Redis. To correct, we edit the cache.php and set the proper values as per the Redis server configuration.
3. Laravel : Redis No connection could be made
Recently, a customer reported problems that the Redis server was not accepting connections from outside. The error said:
Laravel : Redis No connection could be made : [tcp://127.0.0.1:6379]
Here, to fix the error, our Support Engineers had to edit the configuration file at /usr/local/etc/redis.conf. We had to first disable the localhost binding by commenting out the line bind 127.0.0.1 ::1. Then, we turned off the protected-mode followed by a restart of the Redis server.
And, Redis server started accepting the connections.
[Need help to install Redis on Laravel framework? We can help you.]
Conclusion
Installing and configuring Redis on Laravel framework helps to speed up the websites by caching. Today, we saw how our Dedicated Engineers quickly install Redis on Laravel and fix common errors with it.
0 Comments