• Facebook
  • Twitter
  • Youtube
  • LinedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Download
  • Contact Us
Download
Show / Hide Table of Contents

Entry Processor Usage and Implementation

The Entry Processor can be implemented in a class implementing the interface IEntryProcessor. This implementation will contain the logic to be executed over the cache item(s) on the server-side. 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.

Step 1: Implement IEntryProcessor Interface

Your custom logic is provided in the Process method of the class. This takes IMutableEntry as parameters, 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 tamper with 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

    • .NET
    • Legacy API
    • To learn about the standard prerequisites required to work with all NCache server-side features, please refer to the given page on Server-Side API Prerequisites.
    • For API details refer to: IDictionary, InsertBulk, ICollectionManager, Invoke, IExecutionService.
    • The class implementing IEntryProcessor interface must be marked as Serializable, along with any parameters the process code might be taking in.
    • The class must be implemented as a Class Library (.dll) in Visual Studio. This will be deployed in the NCache cluster.
    • Create a new Console Application.
    • Install either of the following NuGet packages in your .NET client application:
      • Enterprise: Install-Package Alachisoft.NCache.SDK -Version 4.9.1.0
      • Professional: Install-Package Alachisoft.NCache.Professional.SDK -Version 4.9.1.0
    • Make sure that the data being added is serializable.
    • Add NCache References by locating %NCHOME%\NCache\bin\assembly\4.0 and adding Alachisoft.NCache.Web and Alachisoft.NCache.Runtime as appropriate.
    • Include the Alachisoft.NCache.Runtime.Processor namespace in your application.
    • To learn more about the NCache Legacy API, please download the NCache 4.9 documents available as a .zip file on the Alachisoft Website.

    The following sample implementation processes gives keys accordingly and returns the values where needed.

    • .NET
    • Legacy API
    [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;
            }
        }
    }
    
    // Using NCache Enterprise 4.9.1
    [Serializable]
    public class CustomEntryProcessor : IEntryProcessor
    {
        // Lock on entries will be ignored
        public bool IgnoreLock()
        {
            return true;
        }
    
        // Custom logic for processing entries
        public object ProcessEntry(IMutableEntry entry, params object[] arguments)
        {
            if (entry.Key.Equals("Product:1001"))
            {
                return true;
            }
            else if (key.Equals("Product:1002"))
            {
                // Simulate update
                string updatedValue = "Value updated against " + key;
    
                // Update cache entry
                entry.Value = updatedValue;
    
                return updatedValue;
            }
            else if (key.Equals("Product:1004"))
            {
                // Remove from cache
                entry.Remove();
    
                return "Entry Removed against " + key;
            }
            else
            {
                if (entry.Exists())
                {
                    return "No logic provided for: " + key;
                }
    
                return "Entry does not exist for: " + key;
            }
        }
    }
    

    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.

    • .NET
    • Legacy API
    // 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
        }
    }
    
    // Using NCache Enterprise 4.9.1
    // Precondition: Cache is already connected
    // This class must be deployed on cache
    // Get a new instance of sample Class implementing EntryProcessor interface.
    CustomEntryProcessor myProcessor = new CustomEntryProcessor();
    
    // Populate dictionary of Product Cache Items
    // Key is in format "Product:1001"
    IDictionary<string, CacheItem> keyValues = GetProductDictionary();
    
    // Convert dictionary to arrays for InsertBulk
    string[] keys = new string[keyValues.Count];
    CacheItem[] items = new CacheItem[keyValues.Count];
    
    int index = 0;
    foreach (var pair in keyValues)
    {
        keys[index] = pair.Key;
        items[index] = pair.Value;
        index++;
    }
    
    // Insert in bulk
    _cache.InsertBulk(keys, items);
    
    // Invoking the Entry processor against a single item
    object result = _cache.Invoke("Product:1001", myProcessor);
    
    // IInvoking the Entry processor against a set of keys
    string[] multiKeys = new string[] { "Product:1002", "Product:1003", "Product:1004" };
    
    ICollection results = _cache.Invoke(multiKeys, myProcessor);
    
    // Handle result
    foreach (IEntryProcessorResult entryResult in results)
    {
        if (entryResult.IsSuccessful)
        {
            var entryValue = entryResult.Value;
            // Perform operations 
    
        }
    }
    

    See Also

    .NET: Alachisoft.NCache.Runtime namespace.

    Contact Us

    PHONE

    +1 (214) 764-6933   (US)

    +44 20 7993 8327   (UK)

     
    EMAIL

    sales@alachisoft.com

    support@alachisoft.com

    NCache
    • NCache Enterprise
    • NCache Community
    • Edition Comparison
    • NCache Architecture
    • Benchmarks
    Download
    Pricing
    Try Playground

    Deployments
    • Cloud (SaaS & Software)
    • On-Premises
    • Kubernetes
    • Docker
    Technical Use Cases
    • ASP.NET Sessions
    • ASP.NET Core Sessions
    • Pub/Sub Messaging
    • Real-Time ASP.NET SignalR
    • Internet of Things (IoT)
    • NoSQL Database
    • Stream Processing
    • Microservices
    Resources
    • Magazine Articles
    • Third-Party Articles
    • Articles
    • Videos
    • Whitepapers
    • Shows
    • Talks
    • Blogs
    • Docs
    Customer Case Studies
    • Testimonials
    • Customers
    Support
    • Schedule a Demo
    • Forum (Google Groups)
    • Tips
    Company
    • Leadership
    • Partners
    • News
    • Events
    • Careers
    Contact Us

    • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

    • Contact Us
    •  
    • Sitemap
    •  
    • Terms of Use
    •  
    • Privacy Policy
    © Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
    Back to top