LazyCache: Caching Service for ObjectCache

LazyCache ObjectCacheI recently blogged about in-memory caching while I was looking for a library to sit on top of .NET ObjectCache or MemoryCache.

Alastair Crabtree commented on my post, suggesting I take a look at this LazyCache library.

So I figured I would take my existing demo application and port it to using LazyCache.

As with most of my posts, all the following code is available as a demo application on GitHub.

This demo console app is going to show currency exchange rate between USD and CAD for a given day.

LazyCache

Lazy cache is a simple in-memory caching service. It has a developer friendly generics based API, and provides a thread safe cache implementation that guarantees to only execute your cachable delegates once (it’s lazy!). Under the hood it leverages ObjectCache and Lazy to provide performance and reliability in heavy load scenarios.

Since it uses ObjectCache under the hood and by default it will use MemoryCache.Default.  It has all the expiring features of ObjectCache.

This means you can specify a CacheItemPolicy, which allows you to expire on a specifc DateTimeOffset, Timespan Expiration, as well as gives you Update/Remove callbacks.

So for this demo, I’m only going to simply use the Timespan expiration for invalidating a cached item after 30 seconds.

Demo

First we can start off by creating a new instance of an CachingService.

Next, I want to create a method that is going to query for the exchange rate. But before it does, I want to check the cache to see if it exists.

The beauty with of LazyCache is the GetOrAdd method which returns the existing item from your cache or calls the provided delegate Func<T> which will return the object you want to cache.

Now to write this all up, I’m going to take a date as input from the console application and have it invoke our GetCurrencyRate(DateTime).

Result

Now when we run our application, we can see that if we request the same date, within the last 30 seconds, we will hit the cache instead making our HTTP call.

ObjectCache

  1. Requested 2016-01-01, 2016-01-02, 2016-01-03, 2016-01-04, all of which were fetched from HTTP service.
  2. Request 2016-01-01 which is fetched from the HTTP service because it exceeded 30 seconds between the first call for that date.
  3. Request 2016-01-04 which returned from cache.

More

The other interesting aspect of LazyCache is that since it can take an ObjectCache in it’s constructor, this opens the door to using it with Redis ObjectCache.

Let me know if you have any comments related to LazyCache or other caching libraries in comments or on twitter.


Source Code

GithubThe source code for this demo is available on GitHub.