Summary
Caching is a very large subject and I’m only going to dive into a small concept that uses Redis, an abstract caching class, and a dummy caching class for unit testing and show how to put it all together for a simple but powerful caching system.
Caching Basics
The type of caching I’m talking about in this blog post involves the caching of data from a database that is queried repetitively. The idea is that you would write a query to read your data and return the results to a web site, or an application that is under heavy load. The data being queried might be something that is used as a look-up, or maybe it’s a sales advertisement that everybody visiting your website is going to see.
The data request should check to see if the data results are already in the cache first. If they are not, then read the data from the database and copy to the cache and then return the results. After the first time this data is queried, the results will be in the cache and all subsequent queries will retreive the results from the cache. One trick to note is that the cached results name needs to be unique to the data set being cached, otherwise, you’ll get a conflict.
Redis
Redis is free, powerful and there is a lot of information about this caching system. Normally, you’ll install Redis on a Linux machine and then connect to that machine from your website software. For testing purposes, you can use the windows version of Redis, by downloading this package at GitHub (click here). Once you download the Visual Studio project, you can build the project and there should be a directory named “x64”. You can also download the MSI file from here. Then you can install and run it directly.
Once the Redis server is up and running you can download the stack exchange redis client software for C#. You’ll need to use “localhost:6379” for your connection string (assuming you left the default port of 6379, when you installed Redis).
Caching System Considerations
First, we want to be able to unit test our code without the unit tests connecting to Redis. So we’ll need to be able to run a dummy Redis cache when we’re unit testing any method that includes a call to caching.
Second we’ll need to make sure that if the Redis server fails, then we can still run our program. The program will hit the database every time and everything should run slower than with Redis running (otherwise, what’s the point), but it should run.
Third, we should abstract our caching class so that we can design another class that uses a different caching system besides Redis. An example Windows caching system we could use instead is Memcached.
Last, we should use delegates to feed the query or method call to the cache get method, then we can use our get method like it’s a wrapper around our existing query code. This is really convenient if we are adding caching to a legacy system, since we can leave the queries in place and just add the cache get wrappers.
CacheProvider Class
The CacheProvider class will be an abstract class that will be setup as a singleton pattern with the instance pointing to the default caching system. In this case the RedisCache class (which I haven’t talked about yet). The reason for this convoluted setup, is that we will use the CacheProvider class inside our program and ignore the instance creation. This will cause the CacheProvider to use the RedisCache class implementation. For unit tests, we’ll override the CacheProvider instance in the unit test using the BlankCache class (which I have not talked about yet).
Here’s the CacheProvider code:
public abstract class CacheProvider { public static CacheProvider _instance; public static CacheProvider Instance { get { if (_instance == null) { _instance = new RedisCache(); } return _instance; } set { _instance = value; } } public abstract T Get<T>(string keyName); public abstract T Get<T>(string keyName, Func<T> queryFunction); public abstract void Set(string keyName, object data); public abstract void Delete(string keyName); }
I’ve provided methods to save data to the cache (Set), read data directly from the cache (Get) and a delete method to remove an item from the cache (Delete). I’m only going to talk about the Get method that involves the delegate called “queryFunction”.
RedisCache Class
There is a link to download the full sample at the end of this blog post, but I’m going to show some sample snippets here. The first is the Redis implementation of the Get. First, you’ll need to add the Stack Exchange Redis client using NuGet. Then you can use the connect to the Redis server and read/write values.
The Get method looks like this:
public override T Get<T>(string keyName, Func<T> queryFunction) { byte[] data = null; if (redis != null) { data = db.StringGet(keyName); } if (data == null) { var result = queryFunction(); if (redis != null) { db.StringSet(keyName, Serialize(result)); } return result; } return Deserialize<T>(data); }
The first thing that happens is the StringGet() method is called. This is the Redis client read method. This will only occur if the value of redis is not equal to null. The redis value is the connection multiplexer connection that occurs when the instance is first created. If this fails, then all calls to Redis will be skipped.
After an attempt to read from Redis is made, then the variable named data is checked for null. If the read from Redis is successful, then there should be something in “data” and that will need to be deserialized and returned. If this is null, then the data is not cached and we need to call the delegate function to get results from the database and save that in the cache.
The call to StringSet() is where the results of the delegate are saved to the Redis server. In this instanced, the delegate is going to return the results we want (already deserialized). So we need to serialize it when we send it to Redis, but we can just return the results from the delegate result.
The last return is the return that will occur if we were able to get the results from Redis in the first place. If both the Redis and the database servers are down, then this method will fail, but the app will probably fail anyway. You could include try/catch blocks to handle instances where the delegate fails, assuming you can recover in your application if your data doesn’t come back from the database server and it’s not cached already.
You can look at the serialize and deserialize methods in the sample code. In this instanced I serialized the data into a binary format. You can also serialize to JSON if you prefer. Just replace the serialize and deserialize methods with your own code.
Using the RedisCache Get Method
There are two general ways to use the Get method: Generic or Strict. Here’s the Generic method:
var tempData = CacheProvider.Instance.Get("SavedQuery", () => { using (var db = new SampleDataContext()) { return (from r in db.Rooms select r).ToList(); } });
For strict:
for (int i = 0; i < iternations; i++) { List<Room> tempData = CacheProvider.Instance.Get<List<Room>>("SavedQuery2", () => { using (var db = new SampleDataContext()) { return (from r in db.Rooms select r).ToList(); } }); }
In these examples you can see the LINQ query with a generic database using statement wrapping the query. This sample was coded in Entity Framework 6 using Code-First. The query is wrapped in a function wrapper using the “() => {}” syntax. You can do this with any queries that you already have in place and just make sure the result set is set to return from this. The tempData variable will contain the results of your query.
Using the BlankCache Class
There are two different ways you could implement a dummy cache class. In one method, you would provide a Get() method that skips the caching part and always returns the result of the delegate. You would be implementing an always miss cache class.
The other method is to simulate a caching system by using a dictionary object to store the cached data and implement the BlankCache class to mimic the Redis server cache without a connection. In this implementation we making sure our code under test will behave properly if a cache system exists and we’re not concerned about speed per-say. This method could have a negative side-effect if your results are rather large, but for unit testing purposes you should not be accessing large results.
In either BlankCache implementation, we are not testing the caching system. The purpose is to use this class for unit testing other objects in the system.
A snippet of the BlankCache class is shown here:
public class BlankCache : CacheProvider { // This is a fake caching system used to fake out unit tests private Dictionary<string, byte[]> _localStore = new Dictionary<string, byte[]>(); public override T Get<T>(string keyName, Func<T> queryFunction) { if (_localStore.ContainsKey(keyName)) { return Deserialize<T>(_localStore[keyName]); } else { var result = queryFunction(); _localStore[keyName] = Serialize(result); return result; } } }
As you can see, I used a dictionary to store byte[] data and used the same serialize and deserialize methods that I used with Redis. I also simplified the Get method, since I know that I will always get a connection to the fake cache system (aka the Dictionary).
When using the CacheProvider from a unit test you can use this syntax:
CacheProvider.Instance = new BlankCache();
That will cause the singleton instance to point to the BlankCache class instead of Redis.
Getting the Sample Code
You can download the sample code from my GitHub account by clicking here. Make sure you search for “” and replace with the name of your SQL server database name (this is in the TestRedis project under the DAL folder inside the SampleDataContext.cs file).
If you didn’t create the ApiUniversity demo database from any previous blog posts, you can create an empty database, then copy the sql code from the CreateApiUniversityDatabaseObjects.sql file included in the Visual Studio code. The sample code was written in VS2013, but it should work in VS2012 as well.