Special Character
Special Character

Reputation: 2359

Redis Cache With Data Sources, Users, and Queries

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?

Upvotes: 0

Views: 215

Answers (1)

Chris Tanner
Chris Tanner

Reputation: 1650

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.

Upvotes: 1

Related Questions