Table of Contents
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 it’s an outstanding open-source choice available in the market to manage the data and improve application performance.
New 8 Laravel 6 Features you should be 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, hyper loglogs and geospatial indexes with radius queries so with this in mind we can play with different types of data without any issues.
Speed
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
Support Messaging
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.
Supports Transactions
Redis also supports transactions 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 make 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 haven’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
OR
Using Composer:
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 your 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