Enhancing both performance and scalability, distributed caching is popular for developing high-transaction applications. However, most distributed caches only provide (key, value) Hash table interfaces for application use. This means you must know the key of the cached item to fetch it, but this is not always possible. Additionally, you might need to search for data based on other search criteria (e.g., “Give me all customers from New York”). This prevents you from caching a lot of data that would otherwise be helpful in boosting your application’s performance and scalability. Using NCache’s LINQ Integration helps overcome this hurdle.
Using LINQ with NCache
NCache provides you with a very powerful SQL-like querying capability (called Object Query Language or OQL) to search the cache based on object attributes (not just the keys). For .NET applications, NCache allows you to search the cache through this.
It is a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It connects the object world with data world and makes searching very easy and manageable. Moreover, it allows you to integrate your data storage to it.
NCache provides a LINQ plug-in to issue queries from your .NET application and runs it against your distributed cache behind the scenes and returns a relevant result set for your application. You can integrate NCache with LINQ by implementing a class named “NCacheQuery”, which implements the interface called “IQueryable” provided by the .NET framework. With this integration, you can run queries on cached items.
Here is a source code example of LINQ Query.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
namespace NCacheLINQ { class Program { static void Main (string[] args) { IQueryable products = new NCacheQuery (_cache); try { var result1 = from product in products where product.ProductID > 10 select product; if (result1 != null) { foreach (Product p in result1) { Console.WriteLine ("ProductID : " + p.ProductID); } } else { Console.WriteLine ("No record found."); } } catch (Exception) { Console.WriteLine (_error); } } } } |
Implementing Aggregate Operator Using LINQ Query Expression
The following code uses the NCacheQuery class with the Sum Aggregate operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Precondition: Cache is already connected // Create custom class LINQ object IQueryable products = new NCacheQuery(cache); // LINQ query to search cache IQueryable result = (from product in products where product.ProductID <= 1010 select product.ProductID).Sum()) if (result != null) { foreach (Product product in result) { Console.WriteLine($"Product '{product.ProductName}' in Category '{product.Category}' has stock of '{product.UnitsInStock}' units"); } } |
Implementing Wildcard Operator Using LINQ Query Expression
The following code uses the NCacheQuery class with Contains Wildcard operator.
1 2 |
// LINQ query to search cache IQueryable result = from product in products where product.Category.Contains("Bever") select product; |
Conclusion
In summary, querying in-memory cache item data sets was never as easy and manageable as with NCache’s LINQ integration . All you need to do is to add a new assembly reference and namespace in your application. So, sign up for a fully working 30-day trial of NCache Enterprise and try it out for yourself.