Alachisoft NCache 4.1 - Online Documentation

Using Continuous Query

 
NOTE: This feature is not available in NCache Express and Professional edition.
 
Following is a list of methods needs to be defined by the class for implementing Continuous Query and a brief description of their purpose.
 
 
  • Instance of ContinuousQuery class and notification registration:
 
An instance of a ContinuousQuery class is required for all methods. It holds information regarding a unique string query i.e. "command text" and Hashtable values i.e. "command parameters". It also contains event notifications for Add, Update and Remove operations. To activate a Continuous Query, you need to register event notifications for that particular Continuous Query instance.
 
Note: For every continuous query instances, at least one notification must be registered.
 
Following event handler procedures should be created that will be called by Add, Update and Remove callbacks whenever a result set changes. If user is not interested in some callback, "null" can be passed for that particular callback:
 
ContinuousQuery query = new ContinuousQuery(queryString, values);
 
query.RegisterAddNotification(new ContinuousQueryItemAddedCallback(query_ItemAdded));
 
query.RegisterUpdateNotification(new ContinuousQueryItemUpdatedCallback(query_ItemUpdated));
 
query.RegisterRemoveNotification(new ContinuousQueryItemRemovedCallback(query_ItemRemoved));
 
static void query_ItemRemoved(string key)
{
    Console.WriteLine("Removed");
}
 
static void query_ItemUpdated(string key)
{
    Console.WriteLine("Updated");
}
 
static void query_ItemAdded(string key)
{
    Console.WriteLine("Added");
}
 
 
  • void RegisterCQ(ContinuousQuery query);
 
Register continuous query method is used to activate "Continuous Query" on server. This method can be called many times for the same query for registering/un-registering various callbacks. Cache.RegisterCQ method is used only for registering the notifications and it does not return any result set to the client application.
 
_cache.RegisterCQ(query);
 
 
  • void UnRegisterCQ(ContinuousQuery query);
 
Unregister Continuous Query method is used to unregister the callbacks from cache when a user is no more interested in notifications for changes in a query result set.
 
_cache.UnRegisterCQ(query);
 
 
  • ICollection SearchCQ(ContinuousQuery query);
 
Search Continuous Query method comprises of two functionalities, ‘RegisterCQ’ which activates the continuous query on server (if it is not already activated) and ‘SearchCQ’ which searches the cache and returns a collection of keys to the client application. The benefit of this approach is that it only returns keys. Then, the client application can determine the keys to be fetched. The drawback of this approach is that if user is going to fetch most of the items from the cache anyway, he will end up making a lot of trips to the cache. And, if the cache is distributed, this may eventually become costly.
 
 
            ICollection keys = _cache.SearchCQ(query);
            if (keys.Count > 0)
            {
                IEnumerator ie = keys.GetEnumerator();
                while (ie.MoveNext())
                {
                    String key = (String)ie.Current;
                    Person prs = (Person)NCache.Cache.Get(key);
                    HandlePerson(prs);
                    Console.WriteLine("Age: {0}", prs.Age);
                }
            }
 
 
  • IDictionary SearchEntriesCQ(ContinuousQuery query);
 
Search Entries Continuous Query method is almost similar to the Cache.SearchCQ method but it searches the cache and returns a dictionary containing both keys and values. It will keep pre-fetched data locally and will immediately return the data on demand. It runs the query on the dataset and returns a Dictionary containing both keys and values. This way, all the data based on the query is fetched in one call. This is much more efficient way of fetching the data from the cache.
 
 
            IDictionary dict = _cache.SearchEntriesCQ(query);
            if (dict.Count > 0)
            {
                IDictionaryEnumerator ide = dict.GetEnumerator();
                while (ide.MoveNext())
                {
                    String key = (String)ide.Key;
                    Person prs = (Person)ide.Value;
                    HandlePerson(prs);
                    Console.WriteLine("Key = {0}, Age: {1}", key, prs.Age);
                }
            }
 
 
Example ContinuousQuery:
 
Here is how your class should look like:
 
    Cache _cache = NCache.InitializeCache("myreplicatedcache");
 
    string queryString = "SELECT ClassLibrary1.Person WHERE this.Age >= ?";
    Hashtable values = new Hashtable();
    values.Add("Age", 25);
 
    ContinuousQuery query = new ContinuousQuery(queryString, values);
 
    query.RegisterAddNotification(new ContinuousQueryItemAddedCallback(query_ItemAdded));
    query.RegisterUpdateNotification(new ContinuousQueryItemUpdatedCallback(query_ItemUpdated));
    query.RegisterRemoveNotification(new ContinuousQueryItemRemovedCallback(query_ItemRemoved));
 
 
The following operators are supported in NCache Continuous queries:
 
Category
Operator
<Query>
= , == , != , <> , < , > , <=, >=, IN, LIKE , NOT LIKE
Logical Operators
AND , OR , NOT
Aggregate Functions
 
Sum, Count, AVG, Min, Max
Miscellaneous
DateTime.Now , DateTime ("any date time compatible string")
 
 
 
See Also

 
Copyright © 2005-2012 Alachisoft. All rights reserved.