Richard
Richard

Reputation: 32899

Advice on best data structure to use in Redis?

I'm new to redis, and would like to start storing an object that's currently JSON in redis instead. But I need some advice on the best data structure to use.

Basically, the object stores information about which user has looked at which page. Here's the JSON:

all_pageviews = {
    'unique_user_session_id_1' : { 'page' : 2, 'country' : 'DE' },
    'unique_user_session_id_2': { 'page' : 2, 'country' : 'FR' } 
     ...
};

I've been using a JSON object with the user IDs as keys because that way I can ensure the keys are unique, which is important for various reasons in my app.

I'm going to want to query it efficiently in the following ways:

Any thoughts on what would be the best redis structure to use? Ordering doesn't matter for the purposes of my app, but querying efficiently does.

Please let me know if I have explained myself badly, or if you need more information. Thanks!

Upvotes: 1

Views: 417

Answers (1)

ABentSpoon
ABentSpoon

Reputation: 5169

To look up data in redis by multiple keys, you'll have to use multiple structures.

I would use a hash to map user_session_ids to the json string, and a sorted set to map pages to user_session_ids

Upvotes: 2

Related Questions