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

Use Redis to record user online status

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Hash table (HashTable) is a data structure that implements key-value (Key-Value) mapping. According to Key, you can quickly find Value. And, no matter how many key-value pairs there are, the query time remains the same. Python's dictionary is based on a hash table.

There is also a data structure called a hash table in Redis.

In Redis, a hash table can be used to hold a large amount of data, and the query time remains the same no matter how much data there is.

A hash table of Redis can store 2 to the power of 32 minus 1 (about 4.3 billion) key-value pairs.

Now, some forum sites can show whether users are currently online or offline. So how is this function realized? One of the implementation methods is based on Redis.

The logic of the program is very simple and includes the following steps:

(1)。 When the user logs in, add a string to the Redis. Key is the user account and Value is 1.

(2)。 When a user exits the site, the Key corresponding to the account name is deleted from the Redis.

(3)。 When querying, the program tries to get the string corresponding to the user's account from Redis: if the value is 1, it means "online"; if the value is None, it means "not online".

Use a string and a hash table to record the user's online information, respectively. What are the advantages of hash tables over strings in this scenario?

If 1000 users are online at the same time, the result after Redis lists all the Key is shown in the figure:

Now, the website has added a points mechanism. Each user has an integral data, which needs to be queried and modified frequently, so Redis is also used to save it.

The problem arises. Online information uses user accounts as Key, and points information also uses accounts as Key. Isn't that a conflict?

So someone added suffixes to different Key. For example, to record whether the user is online, the Key used is "account: online". If the user's account number is 10032, his online status Key is "10032:online". The Key for recording user credits is "account: score". For example, the Key corresponding to user 10032 is "10032:score".

Note: in Redis, colons in Key are ordinary characters that are used to separate prefixes and suffixes with no special meaning. The effect of "10032_online" or "10032-score" is exactly the same.

Example of code that uses hash storage:

Line 12: add a field named user account with a value of l to the hash table named user_online_status in Redis. If a hash table named user_online_status does not exist, one is automatically created.

Line 21: delete a field from the query table named user online status in Redis, and the user account name is Yu Duan.

Line 30: check to see if there is a specific parity segment in the hash table named user_online_status, return False if this field is not available, and True if there is one.

Using the hash table can not only reduce the number of Redis, but also optimize the storage space. Red is officials specifically stated that the hash table has specially optimized the storage structure to store the same content and takes up much less memory than strings.

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