How to use redis in laravel-PHP

Introduction:

Redis is mostly used to cache data that a web-server needs. why need cache data, because in some point users of our product increase rapidly. That means we need to handle large database, large audience interaction, more response time and that obviously large costs. to handle such situation we use caching

What is caching ?

Cache is a temporary hardware or software storage component area where the data is stored so that future requests for that data can be served faster. Caching is the process of storing some data in Cache.

What is Redis?

Redis is an free open source.Redis used as a database, cache and message broker.It supports data structures strings, hashes, lists, sets, and sorted sets

Why use Redis?

  • Redis gives a structured way to store data in memory
  • Redis is faster than our traditional database.
  • Some other programs that do this like memcache,however redis offers a few more data structures to store your data in
  • Redis can also used as a messaging queue using it’s pub-sub functionality

Simple use in laravel

First Install the predis/predis package via Composer : Laravel Documentation 

composer require predis/predis
use Illuminate\Support\Facades\Redis;
 $redis = Redis::connection();       
 $redis->set('visitors',json_encode(DB::table('visitors')->get()));        
 $visitors=$redis->get('visitors');        
 return $visitors; 

Note Find redis commands in there documentation

Add a Comment

Your email address will not be published. Required fields are marked *