Reputation: 6478
I'm investigating the use of Redis in an asp.net mvc application using the ServiceStack.Redis client and a single Redis instance running on a remote machine.
Our cache is broken up into 3 major "areas" (Asp.net output cache, NHibernate second level cache, application cache) and I would like to have the ability able to "flush" all of the keys in these areas individually.
In Couchbase (which we currently utilize) this would be accomplished by using separate buckets with a client instance pointing to each one. We could then flush all values in these buckets using a single call from the appropriate client instance.
Is there a way to accomplish a setup like this using Redis? If so, how do I approach this from the client/server side?
Upvotes: 1
Views: 2572
Reputation: 49932
I can think of three approaches to achieve that.
Redis supports shared databases that are basically separate keyspaces managed by the same server. After connecting to your Redis, you switch between the databases with the SELECT statement and you can use the FLUSHDB command to flush each one individually.
Pro:
Cons:
(For more information on Shared vs. Dedicated Redis databases, check out my post at http://redislabs.com/blog/benchmark-shared-vs-dedicated-redis-instances)
You could use a single Redis database and prefix you keys according to the "area" they belong to (e.g. keys that go into the ASP.Net area will be prefixed with 'asp:' etc...). To delete an area, iterate through the keyspace with the SCAN command using the relevant key name pattern and DEL the results it returns.
Pros: can't think of any Cons:
Use a separate Redis instance for each area, plain and simple. Set up your remote machine to run 3 Redis servers, each managing its own keyspace. To flush, connect to the relevant database and do FLUSHDB or FLUSHALL.
Pros:
Con:
Lastly, if you're looking for a way to use Redis without the hassle, I urge you to consider Redis Cloud as an option for a hosted Redis service in the Cloud. We are the only service provider that lets you set up multiple, dedicated Redis databases in the same subscription at no extra cost.
Upvotes: 2