Home > PHP Framework > ThinkPHP > body text

Using Redis in ThinkPHP6

王林
Release: 2023-06-20 12:31:40
Original
5288 people have browsed it

With the development of Internet technology, more and more websites and applications need to handle a large number of concurrent requests and data storage. Therefore, it becomes more important to use efficient data caching and storage solutions. Redis is a high-performance in-memory database that is widely used in data caching, session management and other scenarios in the Internet field. This article mainly introduces how to use Redis in ThinkPHP6.

1. Redis installation and configuration

First of all, there are two ways to install Redis on a Windows system. One is to download the Redis compressed package and decompress it and start the exe file. The other is to use Chocolatey package manager to install.

If you have already installed Chocolatey, you can use the following command to install Redis:

choco install redis-64
Copy after login

After the installation is complete, you need to configure it. Find the redis.windows.conf file in the Redis installation directory, and change the bind and protected-mode to the following configuration:

bind 0.0.0.0
protected-mode no
Copy after login

This will make Redis listen to all IP addresses and turn off the protected mode, which is convenient for us Do development and testing.

2. Redis extension in ThinkPHP6

The Redis extension of ThinkPHP6 is developed based on the PHP extension package predis. You need to add the following dependencies in the composer.json file before use:

"predis/predis": "^1.1"
Copy after login

Then use composer to install:

composer update
Copy after login

After the installation is completed, create the redis.php configuration file in the config directory and add the following content:

return [
    'default' => [
        'host'     => '127.0.0.1',
        'port'     => 6379,
        'password' => '',
        'database' => 0,
        'prefix'   => '',
        'timeout'  => 5,
    ],
];
Copy after login

The default connection information of Redis is configured here , including the IP address, port number, authentication password, database number, etc. of the Redis server.

3. Basic use of Redis

In ThinkPHP6, you can obtain a Redis instance through the following code:

use thinkacadeCache;

$redis = Cache::store('redis')->handler();
Copy after login

Among them, the cache driver is specified through Cache::store is Redis, and then the Redis instance is obtained through the handler method.

Next, we can perform the following operations on Redis.

3.1. Setting and getting the cache

// 设置缓存
$redis->set('name', 'Tom', 60);

// 获取缓存
$name = $redis->get('name');
Copy after login

A cache named name is set here, the value is Tom, and the validity period is 60 seconds. Then get the cached value through the get method.

3.2. Delete cache

// 删除缓存
$redis->del('name');
Copy after login

Here the name cache is deleted through the del method.

3.3. Determine whether the cache exists

// 判断缓存是否存在
if ($redis->exists('name')) {
    echo '缓存存在';
} else {
    echo '缓存不存在';
}
Copy after login

Here, the exists method is used to determine whether the name cache exists.

4. Advanced applications of Redis

In addition to basic cache operations, Redis also supports operations on data types such as hashes, lists, sets, and ordered sets. Here are some commonly used advanced applications.

4.1. Hash table operation

// 设置哈希表
$redis->hset('user', 'name', 'Tom');
$redis->hset('user', 'age', 18);

// 获取哈希表
$user = $redis->hgetall('user');
$name = $redis->hget('user', 'name');
$age = $redis->hget('user', 'age');
Copy after login

Here, a hash table named user is set up through the hset method, which contains two fields: name and age. Then obtain the data of the entire hash table through the hgetall method, and obtain the values ​​of the name and age fields respectively through the hget method.

4.2. List operation

// 添加列表元素
$redis->rpush('list', 'a');
$redis->rpush('list', 'b');
$redis->rpush('list', 'c');

// 获取列表元素
$list = $redis->lrange('list', 0, -1);

// 弹出列表元素
$value = $redis->lpop('list');
Copy after login

Here, three elements a, b, and c are added to the list named list through the rpush method, and then all elements in the list are obtained through the lrange method. Pop the first element in the list through the lpop method.

4.3. Set operation

// 添加集合元素
$redis->sadd('set', 'a');
$redis->sadd('set', 'b');
$redis->sadd('set', 'c');

// 获取集合元素
$set = $redis->smembers('set');

// 删除集合元素
$redis->srem('set', 'a');
Copy after login

Here, three elements a, b, and c are added to the set named set through the sadd method, and then all elements in the set are obtained through the smembers method. Delete an element from the collection through the srem method.

4.4. Ordered set operation

// 添加有序集合元素
$redis->zadd('zset', 60, 'a');
$redis->zadd('zset', 70, 'b');
$redis->zadd('zset', 80, 'c');

// 获取有序集合元素
$zset = $redis->zrange('zset', 0, -1);

// 修改有序集合分数
$redis->zincrby('zset', 10, 'a');
Copy after login

Here, three elements a, b, and c are added to the ordered set named zset through the zadd method. The scores of each element are respectively 60, 70, 80. Then use the zrange method to get all the elements in the ordered set, sorting them according to their scores from small to large. Finally, the score of an element can be increased or decreased through the zincrby method.

5. Summary

This article introduces how to use Redis in ThinkPHP6, and introduces some basic and advanced applications of Redis. Through these operations, you can improve the concurrent processing capabilities and data storage performance of websites and applications, and improve user experience and user satisfaction.

The above is the detailed content of Using Redis in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template