Working with redis inside laravel
For some time now I have been wanting to learn about laravel and redis. I have never used redis, so I really don't know much about it.
Redis is quite handy as it can keep data in the local server memory. By eliminating the need to access disks, in-memory key-value stores such as redis avoid seek time delays and can access data in microseconds.
How to Install Redis on CentOS 7 Redis package is not included in the default CentOS repositories. We will be installing Redis version 5.0.2 from the Remi repository.
The installation is pretty straightforward, just follow the steps below:
Start by enabling the Remi repository by running the following commands in your SSH terminal:
sudo yum install epel-release yum-utils sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm sudo yum-config-manager --enable remi
Install the Redis package by typing:
sudo yum install redis
Once the installation is completed, start the Redis service and enable it to start automatically on boot with:
sudo systemctl start redis sudo systemctl enable redis
To check the status of the service enter the following command:
sudo systemctl status redis
You should see something like the following:
● redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis.service.d └─limit.conf Active: active (running) since Sat 2018-11-24 15:21:55 PST; 40s ago Main PID: 2157 (redis-server) CGroup: /system.slice/redis.service └─2157 /usr/bin/redis-server 127.0.0.1:6379
Install redis php extension
To install the redis php extensions for linux we'll do the following.
Here we download the package
wget https://github.com/phpredis/phpredis/archive/5.2.2.zip -O phpredis.zip
After this we install the package
yum -y install install php-redis
Now redis has been installed in our server.
How to use redis in our laravel application
Next we'll start using it within our laravel application.
To store a string in redis we can do the following in one of our laravel classes.
Cache::put('key', 'value', 0);
To Get a string from redis.
$value = Cache::get('key');
To forget a redis string.
This is how you can use redis withing laravel.