NCache 4.6 - Online Documentation

Using Continuous Query

Assuming that you have indexed the required searchable attributes, you are now required to implement continuous query in your application. Keeping in mind the purpose of continuous queries, the first thing you need to do is to define all the callbacks that need to be executed once the result set of your query is in any way changed. Then, we need to register the continuous query with the cache server. Following are the main methods that are provided by NCache and are associated with the implementation of continuous queries.
The main purpose of this method is to activate your continuous query with the cache server. You can use this method multiple times in your 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 of ContinuousQuery which has the query and the callbacks registered to it.
unregisterCQ is used by your application after you have registered a continuous query to deactivate it on the cache server. Like registerCQ, it takes as argument an object of ContinuousQuery to unregister the callbacks which are no more fired after this call. This method is used when the user is no more interested in receiving notifications for changes in a query result set.
ExecuteReaderCQ() opens DataReader on server node(s), executes the query and returns the first chunk of data to the client. At the same time, it also registers the continuous query for call backs in case of changes in data fulfilling the query’s criteria.
The result set generated is then read at client side, chunk by chunk, using Read() of ICacheReader. 
[Obsolete] Only returns list of keys in result set which fulfill the query criteria.
[Obsolete] Returns list of key-value pairs in result set which fulfills the query criteria.
class CQ
        static void Main(string[] args)
            Cache cache = NCache.InitializeCache("mycache");
            string queryString = "Select 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); //register ContinuousQuery
                // Use ExecuteReaderCQ() for Continuous Query
                ICacheReader reader = cache.ExecuteReaderCQ(CQ, true, 50);
                    while (reader.Read())
                        string category = (string)reader.GetValue(0);
                        decimal countVal = (decimal)reader.GetValue(1);
                        //use other values in your code if needed.
                // (OBSOLETE) SearchCQ to get keys from the result set
                ICollection result = cache.SearchCQ(continuousQuery);
                // (OBSOLETE) SearchEntriesCQ to get keys from the result set
                IDictionary res = cache.SearchEntriesCQ(continuousQuery);
                //perform operations
            catch (Exception ex)
                // handle exception
            Product product = new Product();
            product.ProductName = " Chai";
            product.Supplier = "Carlos Diaz";
            product.Category = 4;
            cache.Add("key", new CacheItem(product)); //this operation trigger add notification.
            product.Supplier = "Elio Rossi"; //Change supplier name
            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.
        // Create target methods
        static void CacheClear()
            Cache cache = null;
        static void ItemAddedCallBack(string key, CQEventArg arg)
            EventCacheItem item = arg.Item;
            // do something
        static void QueryItemCallBack(string key, CQEventArg arg)
            switch (arg.EventType)
                case EventType.ItemRemoved:
                    // do something
                case EventType.ItemUpdated:
                    // do something
See Also