NCache 4.4 - Online Documentation

Using Continuous Query

 
Assuming that the searchable attributes are already indexed, the user then needs to implement continous query in his/her application. The first step is to define all the call backs that the user needs to be executed if the result set of the query is changed in any way. Following are the main methods which are provided by NCache and are associated with the implementation of continuous queries.  
 
Methods
Description
RegisterCQ
The main purpose of this method is to activate the user’s continuous query with the cache server. This method can be used  multiple times in an application depending on its need to receive the notifications for a change in the dataset of your query. This method takes as argument an object ofContinuousQuery which has the query and the callbacks registered to it.
UnRegisterCQ
UnregisterCQ is used by the application after the user has registered a continuous query to deactivate it on the cache server. Like RecgisterCQ, it takes as argument an object of ContinuousQuery to unregister the callbacks which are no more fired after this call. It is used when the user is no more interested in notifications for changes in a query result set.
SearchCQ
Sometimes the user requires not only to register the continuous query, but also to fetchthe result set associated with that continuous query. For this purpose, one of the methods provided by NCache is SearchCQ.This method takes the object of ContinuousQuery as its argument and returns a collection of keys to your application. These are the keys of objects which are in the result set of the query the CQ is registered to.  It also registers the continuous query if it is not already registered. As this method only returns the keys, NCache API calls can be used to fetch the data of the keys which the user wishes to manipulate from the result set. If the result set of user’s query contains the majority items of cache, this approach can result in a large number of network trip, therefore, affecting the performance.
SearchEntriesCQ
The main reason to provide this method is the same as that of SearchCQ, that is, if the user wishes to fetch the result set when registering the continuous query. This method takes the object of ContinuousQuery as argument but, unlike SearchCQ, returns an IDictionary which contains keys as well as the data of the result set of that query. In this case, it is no longer required to fetch data using the keys explicitly, but the data of result set is returned from this call. In situations where the result set of the query is large, this option is better than SearchCQ as it takes only a single network trip to fetch the data set from the cache rather than fetching data against each key separately, as in the case of SearchCQ.
 
 
Example Code
 
class Program
{
static void Main(string[] args)
  {
    Cache cache = NCache.InitializeCache("mycache");
    string queryString = "Select ClassLibrary.Product WHERE this.supplier=?";
    Hashtable values = new Hashtable();
    values.Add("supplier", "Carlos Diaz");
    ContinuousQuery continuousQuery = new ContinuousQuery(queryString, values);
    continuousQuery.RegisterNotification(new QueryDataNotificationCallback(ItemAddedCallBack), EventType.ItemAdded, EventDataFilter.None); // register notification for add operation.
continuousQuery.RegisterNotification(new QueryDataNotificationCallback(QueryItemCallBack), EventType.ItemUpdated | EventType.ItemRemoved , EventDataFilter.None); // register notification for update and remove operation.
  continuousQuery.RegisterClearNotification(new ContinuousQueryClearCallback(CacheClear)); // register cache clear notification
    cache.RegisterCQ(continuousQuery);
    Product product = new Product();
    product.ProductName = " Chai";
    product.Supplier = "Carlos Diaz";
    product.Categories = " Beverages";
    cache.Add("key", new CacheItem(product)); //this operation trigger add notification.
    product.Supplier = "Elio Rossi";
    cache.Insert("key", new CacheItem(product)); //trigger remove notification because that key will be removed from the query result set by changing supplier name.   
    cache.Clear();      //trigger cache clear notification.
    cache.Dispose();
  }
// Create target methods
      static void ItemAddedCallBack(string key, CQEventArg arg)
{
    EventCacheItem item = eventArg.Item;
  // do something
}
      static void QueryItemCallBack(string key, CQEventArg arg)
      {
            switch (arg.EventType)
            {
                case EventType.ItemRemoved:
                // do someting
                break;
                case EventType.ItemUpdated:
                // do someting
                break
            }
      }
 
 
See Also