Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Redis to store token?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

How to use Redis to store token? To solve this problem, today the editor summarizes this article about storing token in Redis, hoping to help more friends who want to solve this problem to find a more simple and easy way.

Two commands.

There are many data types in Redis, and here we only need two simple commands under the simplest data type, string, to complete the functions we need.

Set

Get

Both commands are very simple, set is to set a string for a key.

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

As indicated above, set the value of the key name to monkeyking. In addition, it can set the expiration time for the key. It's so convenient to set the expiration time.

$redis- > set ('name',' monkeyking', ['ex' = > 100]); # after the expiration time is set to 100s

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 the good performance, there is also a very good place to use redis. Because it is a single-threaded architecture, all commands are executed sequentially. After the previous command has been executed, the next one will not be executed until it is executed, so there will be no dirty reading.

Realize the function

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

Wechat is used to obtain access_token. The returned data format is as follows:

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

We just need to save the xEaew2sI2dsAd 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 note above that the expiration time needs to be set to be less than 7200. Prevent extreme situations, such as getting the token in 7200 seconds and going to Wechat to call the API. Because it takes time to call up the interface, the token will expire, resulting in an error.

The above is the method of using Redis to store token. Is there anything to gain after reading it? If you want to know more about it, you are welcome to follow the industry information!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report