TairHash is a data structure that allows you to specify the time-to-live (TTL) and version number for a field. TairHash is more flexible to use and simplifies application development.
Background information
You can use Redis strings to manage user logon from a single device with ease. However, for multi-device logon from a single user, Redis strings require a string concatenation of the user ID and the device type. Example: User_1_phone
. This solution has the following drawbacks:
Application development: Extra workload is required for string concatenation.
Programming: Repeated encoding and decoding is required.
Storage: Duplicate prefixes of user IDs result in a waste of storage space.
This topic describes how to use TairHash to manage multi-device logon from a single user. TairHash is an in-house extended data structure of Tair. TairHash allows you to configure TTL settings for both keys and fields. In this case, you can set key to the user ID, field to the device type, and value to the user token. You can also specify a TTL for fields.
Sample code
In this example, Python 3.8 is used, and Tair-py is installed. You can run the pip3 install tair
command to install Tair-py.
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import time
from tair import Tair
from tair import ResponseError
def get_tair() -> Tair:
"""
This method is used to connect to a Tair instance.
* host: the endpoint that is used to connect to the Tair instance.
* port: the port number that is used to connect to the Tair instance. Default value: 6379.
* username: the username of a database account. If you do not specify this parameter, the default account is used.
* password: the password of the database account.
"""
tair: Tair = Tair(
host = "r-bp************.redis.rds.aliyuncs.com",
port = 6379,
db = 0,
username = "",
password = "D****123",
)
return tair
def add_user_pass(userID: str, device: str, token: str, timeout: int) -> bool:
"""
This method uses the EXHSET command to store user logon information in TairHash.
* Set key to the user ID.
* Set field to the device type.
* Set value to the user token.
* Set ex to the TTL of the user token.
"""
try:
tair = get_tair()
ret = tair.exhset(userID, device, token, ex=timeout)
return ret == 1
except ResponseError as e:
print(e)
return False
def print_up(userID):
"""
This method is used to display the information of user tokens that have not expired.
"""
for i in tair.exhgetall(userID):
print('{}:{}'.format (userID,i))
if __name__ == "__main__":
tair = get_tair()
# Add the test data of User 1 and User 2.
user_1 = "user1"
user_2 = "user2"
add_user_pass(user_1, "phone", "token_123", 5)
add_user_pass(user_1, "pad", "token_124", 10)
add_user_pass(user_2, "pad", "token_456", 10)
add_user_pass(user_2, "pc", "token_457", 10)
# Wait 6 seconds.
print("Wait 6 seconds")
time.sleep(6)
# Display the information of user tokens that have not expired.
print_up(user_1)
print_up(user_2)
Sample success output:
Wait 6 seconds
user1:{field: pad, value: token_124}
user2:{field: pad, value: token_456}
user2:{field: pc, value: token_457}
Result description: A total of four user tokens are written. However, only the information of three user tokens is displayed 6 seconds after all tokens are written. This is because the TTL of the first user token is 5 seconds.