Home  >  Article  >  Database  >  Use Redis to store tokens

Use Redis to store tokens

齐天大圣
齐天大圣Original
2020-05-08 10:20:143147browse

I believe that all phpers who have done WeChat public account development know that access_token is required to access the WeChat interface. access_token is the globally unique interface calling credential of the official account. There is a limit to the number of times this access_token can be obtained per day, so we need to save it ourselves. In addition, this token also has a validity period. Therefore, we need to refresh regularly and re-obtain the access_token.

How to save it? If you use mysql, one table only stores one piece of data, which feels overkill and not very elegant. If you use files to save, the performance is not very good, and it is still a little troublesome. You need to consider the issue of dirty reads.

Let me introduce a simple, elegant and high-performance way to use Redis to store tokens.

Two commands

Redis has many data types. Here, we only need 2 of the simplest data types string Simple commands can complete the functions we need.

  • set

  • get

These two commands are very simple, set is for one The key is set to a string.

$redis->set('name', 'monkeyking');

The above indicates that the value of the key name is set to monkeyking. Additionally, it can set expiration time for keys. It is very convenient to be able to set the expiration time.

$redis->set('name', 'monkeyking', ['ex' => 100]);
# 过期时间设置为100秒后

The get command is to get a string. The following code means to get the value of the key name.

$redis->get('name');

In addition to good performance, using redis also has a very good point. Because it is a single-threaded architecture, all commands are executed in sequence. After the previous command is executed, the next one will be executed, so there will be no dirty reading.

Implementing functions

After introducing set and get, we can complete our needs.

The interface for WeChat to obtain access_token, the returned data format is as follows:

{"access_token":"xEaew2sI2dsAd","expires_in":7200}

We only need to add xEaew2sI2dsAd Just save it and set the expiration time.

The code is as follows:

function setAccessToken ($token) {
    return $redis->set(
        'wx_access_token', 
        $token, 
        ['ex'=>7000]
    );
}

function getAccessToken ()
{
    return $redis->get('wx_access_token')
}

There is a point to note above, the expiration time needs to be set smaller than 7200. Prevent extreme situations, such as getting the token exactly at 7200 seconds and calling the interface on WeChat. Because calling the interface also takes time, the token will expire, causing an error.

Using Redis to access tokens is very simple and convenient! Everyone can try it.

The above is the detailed content of Use Redis to store tokens. 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