Sponsorship is available! If you’d like to sponsor CodeOpinion.com and have your product or service advertised exclusively (no AdSense) on every post, contact me.I 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.
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.
DemoFirst 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).
ResultNow 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.
- Requested 2016-01-01, 2016-01-02, 2016-01-03, 2016-01-04, all of which were fetched from HTTP service.
- Request 2016-01-01 which is fetched from the HTTP service because it exceeded 30 seconds between the first call for that date.
- Request 2016-01-04 which returned from cache.