Home >Database >Redis >Several applications of Redis string type

Several applications of Redis string type

齐天大圣
齐天大圣Original
2020-05-11 10:00:431414browse

The string data type of Redis is the simplest data type, but it is also the basis for learning other redis data types. The following introduces several applications of redis.

Current limiting

Current limiting can be achieved by using redis. Redis itself has extremely high performance and can be used to withstand some requests.

Defense against heavy traffic

Nowadays, website attacks are becoming increasingly rampant. Static resources can be protected by CDN, but how to defend dynamic programs. Below, we show a redis solution to defend against a large number of requests.

The basic idea is to limit the number of requests for an IP within a certain period of time. For example, the number of requests for an IP cannot exceed 10 times per second.

The pseudo code is as follows:

function ip_limit($cnt = 10)
{
    $ip = $_SERVER['SERVER_ADDR'];
    $key = "limit:$ip:cnt";
    $isExisit = $redis->set($key, 1, ['nx', 'ex'=>1]);
    
    if (!$isExisit || $redis->incr($key) <= $cnt) {
        // 通过
        return true;
    } else {
        // 不通过
        return false;
    }
}

Get verification code frequency limit

SMS verification code has now become a web application As standard, many places will require SMS verification codes. For example: when registering, changing passwords, and some operations that require higher security requirements. Of course, obtaining verification codes is not free, so we do not want users to obtain verification codes without limit.

The same idea is used. We can write an application that limits the frequency of obtaining SMS verification codes.

Complete an application that is only allowed to obtain a verification code once within one minute. The pseudo code is as follows:

function code_limit($mobile, $time = 60)
{
    $key = "limit:$mobile:cnt";
    $isExisit = $redis->set($key, 1, [&#39;nx&#39;, &#39;ex&#39;=>$time]);
    
    if (!$isExisit) {
        // 通过
        return true;
    } else {
        // 不通过
        return false;
    }
}

Save session

Using redis to store sessions is now the recommended way. Its advantage is high performance and automatic ecstasy when it expires. (PHP's own session garbage collection mechanism is not reliable, and there is a probability of expired ecstasy).

This method is very simple, just modify the php.ini file.

Find the session.save_handler line and change it to

session.save_handler = redis

Change session.save_path to

session.save_path = "tcp://localhost:6379"

If you have set a password for redis, just change it to

session.save_path = "tcp://localhost:6379?auth=password"

If it is not a stand-alone application, but a distributed application, how to use redis to centrally manage sessions?

is also very simple. Just modify the php.ini file. If the session is centrally managed on the machine 192.168.1.220, the php.ini files of other machines are modified as follows:

session.save_handler = redis
session.save_path = "tcp://192.168.1.220:6379?auth=password"

The above only introduces some redis string type applications. Developers can combine practical projects and their own Use your imagination to create more applications

The above is the detailed content of Several applications of Redis string type. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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