NCache 4.4 - Online Documentation

NCache Language Integrated Query (LINQ)

 
LINQ allows fetching data of any type through a set of pre-defined standard rules. This helps in setting a common standard through which data can be accessed. It simply joins the application's object layer to data layer. Data can be retrieved in collection of any possible data structure like objects, XML tree, lists etc but there will be no change in the retrieval format of  code.  If the model is created through LINQ, then there is no need to worry about inner details. 
 
NCache allows  using LINQ with  cache to improve the application’s performance through caching without changing the LINQ object model. The cache handle should be given to the NCache LINQ provider after creating its instance. NCache LINQ provider converts LINQ related query into NQL format and returns the result accordingly after transforming it in LINQ format. Because of using NQL under the wrapper of NCache LINQ provider,  indexes should be configured for using LINQ too.
 
Here is a sample query in LINQ
 
var result = from product in products
where product.ProductID > 10
select product;
 
In this query, all those products which have Product ID greater than 10 will be fetched from the cache. The query format is the same as when  normal LINQ is used without NCache LINQ provider. Here ProductID and other required attributes need to be indexed before using this query with NCache.
 
There are some LINQ operators which are not supported by NCache LINQ Provider. These are:
 
  • NCache LINQ Provider does not support nested queries.
  • LINQ operator ORDERBY is not supported by NCache LINQ Provider.
  • LINQ operator GROUPBY is not supported by NCache LINQ Provider.
  • Join operations are not supported by NCache LINQ Provider.
 
 
Runtime indexing is not supported in NCache LINQ integration now.
 
 
To provide LINQ benefits, LINQ is integrated with NCache by implementing a class named NCacheQuery, which further implements the interface called IQueryable provided by .NET framework. This integration allows the user to run LINQ queries on cached items. For executing LINQ queries on cached items, an object implementing IQueryable interface should be defined and passed as an instance of cache which contains the object as shown below:
 
IQueryable<Product> products = new NCacheQuery<Product>(cache);
 
Defining Indexes
 
NCache requires all searchable attributes to be indexed.  NCache provides its own indexing mechanism through which the users can identify objects in .NET assemblies that they want to index. See the section Indexing for details.
 
 
NCache only support single entity search, multiple entities or joins are not supported.
 
 
Using LINQ
 
No code change is required for using NCache with LINQ except that a new assembly reference and namespace should be added in application.  Rest of the code will work without any change in application.
 
The namespace to be added in application for LINQ support is as follows:
 
 using Alachisoft.NCache.Linq;
 
The following code shows how to implement the NCacheQuery class which further implements IQueryable interface. The code also shows the LINQ query based on NCache Query Language running on a cached data.
 
 
//create your custom class LINQ object by giving handle of your cache.
IQueryable<Product> products = new NCacheQuery<Product>(cache);
try
{
    var result = from product in products
                where product.ProductID > 1010
                select product;
    if (result != null)
    {
        foreach (Product product in result)
        {
            //put your code here for modification of products.
        }
    }
    else
    {
        //if there is no result in the cache related to your query
    }
}
catch (Exception)
{
    // can be operation failed due to server lost, state transfer or invalid query format of LINQ
}
 
See Also