How to Use LINQ for Searching Distributed Cache?

Distributed caching is becoming really popular among developers of high transaction applications because it improves your application’s performance and scalability. And, this popularity means that developers are caching more and more data in it which they also want to be able to search just like they are able to search relational databases.

But, one major limitation of many distributed caches is that they only provide (key, value) Hashtable interface to you. This means that for you to fetch any cached item, you must know its key. But in real life this is not always possible and in many cases you need to search for data based on other search criteria (e.g. “Give me all customers from New York”). So, if you’re not able to search a distributed cache, you’re likely to only cache data for which you always know their keys. And, this prevents you from caching a lot of data that would otherwise really boost your application’s performance and scalability.

Fortunately, NCache provides you a very powerful SQL-like querying capability (called Object Query Language or OQL) to let you search the cache based on object attributes and not just the keys. And, for .NET applications, NCache provides LINQ integration and allows you to search the cache through LINQ.

LINQ is a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. And, it connects the object world and data world and makes searching very easy and manageable. Moreover, LINQ allows you to integrate your own data storage to it.

NCache provides a LINQ plug-in. So, you now issue LINQ queries from your .NET application and behind the scene this query runs against an NCache distributed cache and a result set is returned to your application. NCache is integrated with LINQ by implementing a class named “NCacheQuery”, which further implements the interface called “IQueryable” provided by .NET framework. By this integration you can run LINQ queries on cached items.
Here is a source code example of LINQ Query.

In summary, querying in-memory collections wasn’t this much easy and manageable before LINQ and NCache gives LINQ integration in such a way that you don’t have to change code all you need to do is add a new assembly reference and namespace in your application.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.