What is Redis?
Redis is an open source in memory data structure store mostly use as a database, cache or a message broker on backend, Redis is advanced it uses key value storage mechanism to store the data, if you are thinking to use Redis on your project then its an outstanding open source choice available in the market to manage the data and improve application performance.
New 8 Laravel 6 Features you should aware of in 2019 – New Features and Updates
Advantages using Redis with Laravel Framework
Redis supports rich data structures
Yes redis refers do support hight amount data structure in contain strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries so with this in mind we can play with different types of data without any issues.
Redis processed data very fast as it run on in-memory dataset it gives exceptionally good performance to our application, evenly it will boost website user experience
Apart from caching Redis has ability to interchange data real-time, it is also useful for messaging and sending real-time notification to active users.
Redis also supports trasactinons which means we can run process one after another the commands can be queued.
Steps to install Redis on Ubuntu Operating system
Before starting to use redis with Laravel project we need to install redis server on our system, In this step I am going to show you how you can install redis on Ubuntu operating system, if you are using Ubuntu as your development environment then you can follow next steps to install:
First update your system to get it ready to install new application:
sudo apt-get update sudo apt-get upgrade
Next use bellow command to grab redis on your system:
sudo apt-get install redis-server
After installing redis successfully into the system, we need to start redis server, the following command will start the redis server and it will make sure to keep redis running when you restarted your system:
sudo systemctl enable redis-server.service
Steps to install Redis on Mac Operating System
If you are on Mac Operating system then you have a couple of options, if you are using homestead on your mac for Laravel development then your good to go you don’t need to install it again because homestead has installed out of the box. another option is using homebrew, first make sure to install homebrew and then following next commands, if you have’t installed homebrew yet then use this command to install:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Use following command to install redis server:
brew install redis
Use this command to start redis server:
brew services start redis
I assume that you have redis server running on your development platform, now let’s proceed further to learn how to configure redis with Laravel project and see real-time examples using redis.
Configure Redis with Laravel Framework
In this step we are going to configure redis within Laravel project, so to do that we have to install predis library into the project using composer and to do this you should have Laravel project installed and ready to use with redis, if you have existing project then not to worry your good to go if not then Creating new Laravel project is really is easy, use following command to create one:
Using Laravel installer:
laravel new redis-demo
composer create-project --prefer-dist laravel/laravel redis-demo
Okay so I assume that you have Laravel Project running and ready to rock, let’s install predis php library to support Redis operations from the project, use following command to install:
composer require predis/predis
Next Laravel has provided built-in redis configuration options into the config/database.php file, go ahead and open config/database.php file into your code editor and checkout the default configuration:
/* |-------------------------------------------------------------------------- | Redis Databases |-------------------------------------------------------------------------- | | Redis is an open source, fast, and advanced key-value store that also | provides a richer body of commands than a typical key-value system | such as APC or Memcached. Laravel makes it easy to dig right in. | */ 'redis' => [ 'client' => 'predis', 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_DB', 0), ], 'cache' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_CACHE_DB', 1), ], ],
The default configuration for redis should work with you development platform, however you can modify according to your redis installation if needed but at the first place you don’t need to do any changes.
Examples using Redis with Laravel
Now let’s see real time examples using redis and Laravel click here – 3 examples using redis that makes your laravel site load faster