0

I am starting to use Redis as a cache and I am not sure of the best structure for my application. The overall structure is below:

enter image description here

Some things I need to be able to do are:

  1. Flushing everything for a data source (all users and all queries).
  2. Flushing everything for a particular user (e.x: I would need to remove user 1 and it's queries from data source 1 and data source 2).

Everything listed in the tree is meant to be a part of the key to accessing the results of running the query for the user on the specific data source. I am new to Redis and have been going back and forth between using Hashes and Sets.

Option 1 (Sets):

DataSource1 => user1, user2, user3, user4
DataSource1:user1 => Query1, Query2, Query3
DataSource1:user1:Query1 => Results

Flushing things would be expensive because I would have to find all keys that match user1 or DataSource1.

Partial Option 2 (Hashes):

users:user1 DataSource1:Query1 Results1 DataSource2:Query1 Results2

Still not sure how flushing data sources or users would work here.

Does anyone have other thoughts/modifications?

asked Jan 11, 2017 at 21:03

1 Answer 1

1

Based on the information I would have a set with DataSource -> Users,and a hash for users -> queries. That way flushing users is just deleting the hash, and flushing a data source (which you probably do less) is looping through the set and deleting users, then deleting the datasource -> user set.

If you go with the first option of sets then it'll be more operations every time you flush queries from users, but it really depends on the the data is accessed.

Also depending on how many queries you store per user, hashes are more efficient than sets.

answered Jan 12, 2017 at 14:58
Sign up to request clarification or add additional context in comments.

1 Comment

I like this solution. It is clean and straightforward. Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.