How to use Continuous Query in Cache
Note
This feature is only available in NCache Enterprise Edition.
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.
If all your applications don't require tracking of any query result set, then you should not only unregister notifications but also unregister the query from your cache.
Pre-Requisites
- Searchable attributes should be configured on cache using query indexes.
- Cache should have some data related to configured attributes.
- Install the following NuGet packages in your application:
- Include the following namespaces in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Events
Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- For API details, refer to: ICache, EventDataFilter, EventType, ExecuteReader, RegisterCQ, UnRegisterCQ, UnRegisterNotification.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures
Step 1: Register Callback for Events
Assuming that you have indexed the required searchable attributes, you can implement Continuous Query in your application. This requires you to define all the callbacks that need to be executed once the result set of your query is modified. Then, we need to register the Continuous Query with the cache server.
A callback can be registered for multiple events.
static void QueryItemCallBack(string key, CQEventArg arg)
{
switch (arg.EventType)
{
case EventType.ItemAdded:
// "key" has been added to cache
break;
case EventType.ItemUpdated:
// "key" has been updated in cache
// Get updated Product object
if (arg.Item != null)
{
Product updatedProduct = arg.Item.GetValue<Product>();
// Perform operations
}
break;
case EventType.ItemRemoved:
// "key" has been removed from cache
break;
}
}
Step 2: Register Query and Notifications
After the callbacks are registered, create a Continuous Query, which specifies the criteria for the result set of which the events will be fired. This query will be registered against the server.
Once Continuous Query has been created, the pre-defined callbacks are registered with the query. The callbacks are registered according to
EventType
andEventDataFilter
.The continuous query can now be registered on the server using
RegisterCQ()
. You can use this method multiple times in your application to receive the notifications for a change in the dataset of your query.Any modifications in cache event notifications will be triggered according to the event type. For querying cached data,
ExecuteReader
executes the query and the result set generated is then read at client side, chunk by chunk.You can trigger events by modifying cache data such that it affects the result set. The code sample updates an existing cache item such that is added to the query result set, thereby firing an ItemAdded event.
Warning
If the connection breaks between a server and client, any events fired within this duration will not be received by the client.
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
try
{
// Precondition: Cache is already connected
// Query for required operation
string query = "SELECT $VALUE$ FROM FQN.Product WHERE Category = ?";
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("Category", "Beverages");
// Create Continuous Query
var cQuery = new ContinuousQuery(queryCommand);
// Item add notification
// EventDataFilter.None returns the cache keys added
cQuery.RegisterNotification(new QueryDataNotificationCallback(QueryItemCallBack), EventType.ItemAdded, EventDataFilter.None);
// Item update notification
// EventDataFilter.DataWithMetadata returns cache keys + modified item + metadata on updation
cQuery.RegisterNotification(new QueryDataNotificationCallback(QueryItemCallBack), EventType.ItemUpdated, EventDataFilter.DataWithMetadata);
// Item Remove notification
// EventDataFilter.Metadata returns cache keys + item metadata on updation
cQuery.RegisterNotification(new QueryDataNotificationCallback(QueryItemCallBack), EventType.ItemRemoved, EventDataFilter.DataWithMetadata);
// Register continuousQuery on server
cache.MessagingService.RegisterCQ(cQuery);
// Query Cached Data
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// If resultset is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
Product result = reader.GetValue<Product>(1);
// Perform operations
}
}
else
{
// Null query result set returned
}
// Update Product Data in Cache to trigger callback
var updatedProduct = new Product()
{
ProductID = 1001,
ProductName = "Coffee",
Category = "Beverages" // Complies with criteria
};
string key = $"Product:(updatedProduct.ProductID)";
cache.Insert(key, updatedProduct);
// This will add the item to the result set
// as it matches query criteria
}
catch (OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure that the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentException, ArgumentNullException
}
Step 3: Unregister Notifications from Continuous Query
Notifications can be unregistered from Continuous Query when they are no more required in application. You can unregister notifications for a specific event type if multiple event types have been registered using UnRegisterNotification
method.
For example, if ItemAdded
and ItemRemoved
event types were registered but your business logic no longer requires events for ItemAdded
, you specifically unregister notifications for ItemAdded
events.
// Unregister notifications for ItemAdded events only
cQuery.UnRegisterNotification(new QueryDataNotificationCallback(QueryItemCallBack), EventType.ItemAdded);
Step 4: Unregister Continuous Query from Server
Once the application is no more interested in receiving notifications for changes in a query result set, the registered continuous query should be unregistered from server.
UnregisterCQ
takes as argument an object of ContinuousQuery
to unregister the callbacks which are no more fired after this call.
// Unregister Continuous Query from server
cache.MessagingService.UnRegisterCQ(cQuery);
Additional Resources
For a full functioning .NET application executing Continuous Query, you can use the samples shipped with NCache which are placed on GitHub.
See Also
SQL Reference for NCache
Event Notifications in Cache
Pub/Sub Messaging