Detailed explanation of the method of reading website cache by laravel using Redis

  • 2021-09-16 06:30:13
  • OfStack

Introduction to redis

Redis is completely open source and free, complies with BSD protocol, and is a high-performance key-value database. Redis vs. Other key-value
Caching products have the following three characteristics: Redis supports the persistence of data, which can keep the data in memory on disk, and can be reloaded for use when restarting.

Redis not only supports simple key-value data, but also provides storage of list, set, zset, hash and other data structures.

Redis supports data backup, that is, data backup in master-slave mode.

Redis Advantages

Extremely high performance Redis can read 110000 times/s and write 81000 times/s. The rich data type Redis supports Strings, Lists, Hashes, Sets, and Ordered Sets data type operations in binary cases. All operations of the atomic Redis are atomic, and Redis also supports atomic execution after several operations are completely merged. The rich features Redis also support publish/subscribe, notification, key expiration, and more.

How to Install redis

1. Ubuntu (Because I use Homestead environment, the virtual machine is Ubuntu, so I only used this one method to install Windows. Please go to http://www.redis.net.cn/tutor or https://www.ofstack.com/article/84071. htm...)

Official installation method of Ubuntu


$sudo apt-get update
$sudo apt-get install redis-server

Because my website uses the framework of laravel, I want to use composer method to install it


composer require predis/predis

illuminate/redis (5.2. *) is also required if the lumen microframework is installed


composer require illuminate/redis (5.2.*)

Configure

If lumen is installed


// Need to be in bootstrap/app.php Add this code to it 
$app->register(Illuminate\Redis\RedisServiceProvider::class);
$app->configure('database');

Redis can be used as the primary database, so the configuration information for Redis in Laravel is located in config/database. php:


'redis' => [
'cluster' => false,
'default' => [
 'host' => '127.0.0.1',
 'port' => 6379,
 'database' => 0,
],
],

The cluster option indicates whether to fragment among multiple Redis nodes. Here, we only tested one node locally, so we set it to false.

The default option indicates the default Redis host connection, where the Redis and Web servers share one host, so the host is 127.0. 0.1, and the Redis default port is 6379.

In addition, the default option supports more connection parameters (if needed):


参数
意义
默认值
host 服务器IP地址/主机名称 127.0.0.1
port Redis服务器监听的端口号 6379
password 如果服务器需要认证密码 不使用密码
database

连接时选择的数据库索引

没有设置索引
timeout
连接到Redis服务器超时时间 5秒
read_write_timeout 通过网络连接进行读写操作的超时时间

系统默认(不限制超时时间的话设置为-1)


read_write_timeout the time-out for reading and writing over a network connection is the default (set to-1 if the time-out is not limited)

In addition, if Redis is used as a caching tool, you also need to configure redis option in config/cache. php:


'redis' => [

'driver' => 'redis',
'connection' => 'default',
],

The connection here corresponds to the default host default configuration for redis in config/database.

After completing the above configuration, we can use Redis for data access in application code.

Look at an example-the collocation of mysql and Redis of laravel

My idea is that when I read it, I will go to the cache to find it first. If I find it, I will be lucky. If I can't find it, I will go to the database to find it and transfer it to the cache.


 if (Cache::has($key)){    // Search first cache If found 
  $values = Cache::get($>key); // Direct read cache
  return $values;
 }else{         // If cache There is no inside   
  $values = DB::select($sql);
  Cache::put($key,$value,$time);
  return $values;
 }

But cache of laravel also provides an remember function


 $values = Cache::remember($key,$time,function () {
   return DB::select($this->sql);
  });

If the cache has a direct read and return, if the cache entry does not exist in the cache, the closure returned to the remember method will be run, and the result of the closure run will be stored in the cache.

Summarize


Related articles: