Using Redis to store user information
Asked Answered
T

2

9

I'm building a simple game type application where I have user authentication. I'm planning on using Redis for my database, and I was wondering what the best practice was to store users? What I understand is that I'll need a list or sorted set to store the users, and each user would be a hashmap. Is this the preferred method of storing users or is there a more optimal way?

Thaumaturgy answered 31/7, 2015 at 3:34 Comment(2)
Your should store the data in Redis based on how you're going to access it. A Hash per user usually makes sense. The Sorted Set may or may not fit here, depending on what you are trying to do.Gent
Yeah, after thinking about it a sorted set is a little silly, thanks!Thaumaturgy
F
10

A hash per user works best in most cases. Redis documentation on data types mentions:

Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (e.g. A User with a number of fields like name, surname, age, and so forth):

Fluency answered 4/8, 2015 at 5:42 Comment(1)
Could you help me, please? I've role in user data, do you know how to save this data in redis hash? user: {"username": "Abc" "password:"123", roles: { "app": [1,2,3,4,5,6] } }Luca
S
0

Single hashmap per user is enough to store string/number/boolean type variables as string. If you want to store array per user, string/number/boolean array, you should also use a set or list per user. If you want to store object array per ser, i think you should store each object as a hashmap and store keys of these hashmaps in a set or list for each user.

Shultz answered 25/3, 2022 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.