Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.

Pluggable foundation blocks for building distributed apps.Turns out Foundatio was created for building Exceptionless. I’ve blogged about Exceptionless before, which is a real-time error and logging .NET and Javascript.
Basics
Caching in it’s various forms (in-memory, distributed) is simply about storing a copy of the data in a way that improves performance. This could be data from a web service, database, or any other call that involves reading data that rarely changes but is read/queried often.Foundatio
First thing will be to get the latest NuGet package.PM> Install-Package FoundatioThere are many other components that Foundatio provides besides Caching, such as: Queues, Locks, Messaging, Jobs, File Storage, Metrics and Logging. I haven’t dug into any of these other areas yet, but once I do, you can expect a blog post.
In-Memory Caching Demo
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. Since historical exchange rates don’t change, there is no point in making an HTTP call for a day that was already queried. Also, one really cool feature I’ll show, is that you can set the max number of items in your cache. So for this demo, I’m only going to keep the last 3 exchange rates by date in cache. First we can start off by creating a new instance of an InMemoryCacheClient and setting it’s MaxItems property to 3. 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 key for our cached object is going to be the date. If the key does not exist in the cache, then we will make our HTTP call, get the results and cache it. 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 3 requests, we will hit the cache instead making our HTTP call.
- Requested 2016-01-01, 2016-01-02, 2016-01-03, all of which were fetched from HTTP service.
- Requested 2016-01-01 again, which was pulled from cache.
- Requested a new date 2016-01-04 which was fetched from the HTTP service.
- Requested 2016-01-02 again, however since it is no longer the last 3 requested (2016-01-03, 2016-01-01, 2016-01-04) it has to fetch from the HTTP service.
More
There are other interesting CacheClient’s such as a hybrid Redis + InMemory cache client that I’m looking forward to exploring in Foundatio. Let me know if you have any comments related to Foundatio or other caching libraries in comments or on twitter.Source Code

Great find!
Now if only I could find an open source caching framework that supported tagging cached items (by client, user, etc). NCache Enterprise does this but it’s too pricey for the project I’m working on right now 🙁
Have a look at Apache Ignite.NET 🙂
Awesome, thanks for the great recommendation Kefir!
did you use Apache Ignite.NET in Windows ? any good patterns and practices in a real world sample?
Thanks, will check out foundatio. If you only needed in memory caching you could check out my library LazyCache. As well as supporting concurrency it will ensure the code to generate the cached item will only ever run once, which is often useful in application startup. In addition the API allows you to check the cache and generate the result in one call – GetOrAdd -making your caching code much simpler. Would be interested in your thoughts as a comparison between the two.
https://github.com/alastairtree/LazyCache
Thanks for the heads up. I’ll take a look!
Did a quick write up of using LazyCache in my demo application. I really like the Func for GetOrAdd.
http://codeopinion.com/lazycache-caching-service-objectcache/