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?
-
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.Itamar Haber– Itamar Haber2015-07-31 07:51:21 +00:00Commented Jul 31, 2015 at 7:51
-
Yeah, after thinking about it a sorted set is a little silly, thanks!David Cao– David Cao2015-07-31 15:01:30 +00:00Commented Jul 31, 2015 at 15:01
2 Answers
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):
1 Comment
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.