Skip to main content

Cloud

Declarative data caching in .NET business layer

One of the most effective ways to improve application performance is to implement data caching. Most of the applications are relatively retrieving the same data from external sources like database of web service and if that source data is never or seldom changes then application is just wasting CPU time and I/O querying the source again and again. There are a great many ways to cache data in application, there are different software frameworks, standalone applications and even caching appliances, but probably the most widespread and easiest way to cache data is a built-in .NET MemoryCache class which is providing a simple in-process caching since .NET 4.0. cache

MemoryCache works fine, but it’s quite a hassle to implement it in all of your data access methods (it’s likely that your application have a plenty of them), it requires a few lines of a custom code to check if the item is in cache and put it to cache if it’s not. Plus, one needs to exercise some caution working in multi-threaded environment, because cache is a singleton.
We all know and love ASP.NET output caching helpers which is a simple and elegant way to add caching to your web application, all that you need to do is to either place a directive on your view definition file or decorate a controller action with an attribute, clean and simple.
However, ASP.NET output caching works only … with ASP.NET/MVC and only applicable on the page/action level. What if I want to have a nice, aspect-oriented, declarative caching in my business/DAL layer and I don’t want to have any dependency on a web layer?
Well, there is a nuget package for that. It’s called DynaCache, and this is a project home at Codeplex: http://dynacache.codeplex.com/ . DynaCache allows you to simply decorate your business logic class method with the following attribute: [CacheableMethod(cacheDurationInSeconds)] to automatically cache the result of method execution for specified number of seconds. It’s that easy.
Fine print: in order to get DynaCache to work, you’ll need to use a IOC framework of your choice (because DynaCache is operating by dynamically generating a caching wrapper around your class’s method and it needs to be able to inject that wrapper between your cached method and it’s caller) and you’ll also need to follow a repository pattern in your business logic module, but it’s a good practice to follow this pattern neither less.
So, I can just use DynaCache and all my performance problems will be solved, right? Well, it’s not that simple. DynaCache is easy to use, but it’s quite limited. Currently, you can only specify a fixed time interval for duration of the caching, which may not work in many scenarios. It would be nice to have cache dependency on some other processes or events. For example, if your you data access layer is caching a result of a SQL query then it would be useful to reset cache when data is changed in the database. Fortunately, DynaCache is ope source, so you can extend it and modify as you like.
I was able to extend DynaCache to make the cached item to be depended from the data in SQL database. When data is modified in the dependent table the cache item is being reset. If somebody is interested in exploring this topic then I can elaborate on it more.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stan Tarnovskiy

Solutions Architect at Perficient

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram