About the storage of users' chat records in chat system

can the chat record be saved in redis? because the chat record is time-limited, I want to use the expiration function of redis and delete it automatically at a certain time

.
Mar.16,2021

in theory, but highly recommended!
for example, I thought I could use the keys feature in redis:
suppose User_a and User_b sent two messages, so they can be stored as:

import redis
conn = redis.Redis(host='localhost', port=6379)
-sharp  User_a:User_b:+  key, value 
conn.set('User_a:User_b:2018060123050809001', 'msg1')
-sharp  3 second
conn.set('User_a:User_b:2018060123050809002', 'msg2', ex=3)

-sharp  Redis.keys `User_a``User_b`keys
msg_keys = conn.keys('User_a:User_b:*')

-sharp keys
msgs = conn.mget(msg_keys) if msg_keys else []

but we can see that because redis stores data according to key-value pairs, it is not very convenient to query data, nor does it support some complex conditional queries. Even for the above solution, there may be performance problems. Reference: redis production environment be careful to use keys fuzzy matching method . keys methods can be replaced with scan_iter methods.

to sum up, using redis to store chat records is not a very wise choice. Redis was not born to do this.

I recommend using mongodb. Refer to Expire Data from Collections by Setting TTL . Of course, relational databases can also meet this business requirement by adding a create_time field, and then only finding chat records that do not expire each time, that is, chats that satisfy create_time + expires > datetime.now () .

Menu