Home > Database > Redis > Use Redis to store tokens

Use Redis to store tokens

齐天大圣
Release: 2020-05-08 10:42:07
Original
3169 people have browsed it

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');
Copy after login

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秒后
Copy after login

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

$redis->get('name');
Copy after login

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')
}
Copy after login

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!

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