Entry Processor Usage and Implementation
The Entry Processor can be implemented in a class implementing the interface IEntryProcessor. This class, enabling you to move logic to the data and reduce network overhead. Once implemented, this implementation will be deployed on NCache. You can then invoke the cache from your client application to perform the specified Entry Processor logic over the server, which is more efficient than fetching the data for client-side processing.
Step 1: Implement IEntryProcessor Interface
Your custom logic is provided in the Process method of the class. This takes an IMutableEntry parameter, which contains the following flags:
- IsAvailableInCache
- IsUpdated
- IsRemoved
For example, if the entry is updated in the method and you wish to save the updated value in the cache, you can initialize entry.Value with the new value. Otherwise, if you only wish to get the result and not update it, you should not modify the IMutableEntry flags.
In case an entry is locked by an application and you wish to run the Entry Processor over it, you can use the IgnoreLock method to ignore the lock and access the entry to execute your method.
Important
Once implemented, deploy this class on NCache by referring to Deploy Providers in the Administrator’s Guide.
Prerequisites
Before using the NCache Entry Processor, ensure that the following prerequisites are fulfilled:
- Install either of the following NuGet packages in your application:
- Enterprise: Alachisoft.NCache.SDK
- OpenSource: Alachisoft.NCache.Opensource.SDK
- Include the following namespace in your application:
- The cache must be running.
- The class must be implemented as a Class Library (.dll) in Visual Studio. This will be deployed in the NCache cluster.
- The class implementing
IEntryProcessorinterface must be marked as Serializable, along with any parameters the process code might be taking in. - For API details refer to: IDictionary, InsertBulk, ICollectionManager, Invoke, IExecutionService.
The following sample implementation processes keys accordingly and returns values where needed and returns the values where needed.
[Serializable]
public class CustomEntryProcessor : IEntryProcessor
{
// Lock on entries will be ignored
public bool IgnoreLock()
{
return true;
}
// Custom logic for processing entries
public object Process(IMutableEntry entry, params object[] arguments)
{
if (entry.Key.Equals("Product:1001"))
{
return true;
}
else if (entry.Key.Equals("Product:1002"))
{
// Update Value against key
var product = new Product()
{
ProductID = 1002,
ProductName = "UpdatedName"
};
// Process entry, save updates in cache and return
object updatedValue = "Value updated against " + entry.Key;
// Update value
entry.Value = updatedValue;
return updatedValue;
}
else if (entry.Key.Equals("Product:1004"))
{
// Remove this product from cache
entry.Remove();
return "Entry Removed against " + entry.Key;
}
else
{
if (entry.Value is Product)
{
return "No logic provided for: " + entry.Key;
}
return "Entry does not exist for: " + entry.Key;
}
}
}
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Step 2: Deploy Implementation on Cache
Deploy this class and any dependent assemblies on NCache by referring to Deploy Providers in the Administrator’s Guide for help.
Step 3: Invoke Entry Processor
Once the IEntryProcessor interface is implemented (CustomEntryProcessor in the previous step) and deployed on cache, you can execute the Entry Processor using the Invoke method in your client application.
The following code sample adds a bulk of items into the cache using the InsertBulk() method and then invokes the Entry Processor using the keys added to the cache, for which implementation has been provided in CustomEntryProcessor class.
try
{
// Pre-condition: Cache is already connected
// Class implementing IEntryProcessor interface
// This class must be deployed on cache
var myProcessor = new CustomEntryProcessor();
// Populate dictionary of Product CacheItems
// Key is in format "Product:1001"
IDictionary<string, CacheItem> dictionary = GetProductDictionary();
// Insert data in bulk
cache.InsertBulk(dictionary);
// Invoking the Entry processor on a single item of key "Product:1001"
object returnedValue = cache.ExecutionService.Invoke("Product:1001", myProcessor);
// Invoking the Entry processor against a set of keys
string[] keys = new string[] { "Product:1002", "Product:1003", "Product:1004" };
ICollection retEntries = cache.ExecutionService.Invoke(keys, myProcessor);
// Handle result
foreach (IEntryProcessorResult entryResult in retEntries)
{
if (entryResult.IsSuccessful)
{
var result = entryResult.Value;
// Perform operations
}
}
}
catch (EntryProcessorException ex)
{
// If this occurs, no mutations will be made to the cache
// Provide custom logic to handle exception
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
See Also
.NET: Alachisoft.NCache.Runtime namespace.