Okan
Okan

Reputation: 1389

Store chat message and id in a list

I want to use redis for store chat messages in ram.I want a data structure like this:

UserA { MessageId->Message}

This should be a list.For example:

User:Okan
15847->Message 1
35848->Message 2
12358->Message 3
84887->Message 4
...

And I want to delete a message by message id.How can I store them like this and delete them by message id ?

Upvotes: 1

Views: 809

Answers (1)

Ofir Luzon
Ofir Luzon

Reputation: 10907

If you'll change your structure to hold both the user and the message you'll be able to achieve it easily. Here's an example with hash:

Message:<MessageID>
   ID MessageID
   UserID UserID
   Message MessageText

ID field is redundant as it's already in the HASH key name, but it is sometimes easier to use when it is also an element inside. You should also add some auxiliary SETS, depends on your usecase

User:<userID> {messageID1, messageID2, etc...}
Users {ID1, ID2, etc...}

You can than do intersections unions and use SORT to get the Message elements from several hashes in one shot.

BTW, you should switch to userID instead of name and keep a map of ID to Name.

Deletion can be made by message ID only, but a few keys should be updated. You should write a LUA script that updates all relevant keys. (SCRIPT LOAD to load the script once, and EVALSHA to use it every time - don't use EVAL for every call you make).

Upvotes: 1

Related Questions