Alachisoft NCache 4.1 - Online Documentation

Async Operations

 
NCache now offers Asynchronous cache operations, so that maximum performance can be achieved. Following are the simple coding examples implementing Async operations:
 
  1. Include the following namespace in your application.
     
    using Alachisoft.NCache.Web.Caching;
     
  2. Create a new Class named 'Customer' in your already created Test Application.
  3. Insert the following code in the 'Customer' class.
     
    [Serializable]
    class Customer
    {
    ...
    }
     
  4. Now insert the following code in the 'Main' of the project:
     
    Cache _cache = NCache.InitializeCache("mycache");
    _cache.Clear();
     
    Customer customer = new Customer ();
     
    customer.name = "David" ;
    customer.customerID = 1001;
    customer.City = "London" ;
    customer.ContactNumber = "+44-xxx-xxxx-xxxx" ;
    customer.IsOrderInProcess = true ;
     
    For Adding item in a cache asynchronously
     
    // Add Item to Cache asynchronously
    _cache.AddAsync("Customer:David:1001" , new CacheItem (customer), DSWriteOption.None, null);
     
    Thread.Sleep(1000);
    Object objectCustomer = _cache.Get("Customer:David:1001");
    Customer customerInformation = (Customer)objectCustomer;
    Console.WriteLine("Customer ID: " + customerInformation.customerID);
    Console.WriteLine("Name: " + customerInformation.name);
    Console.WriteLine("City: " + customerInformation.City);
    Console.WriteLine("Contact Number: " + customerInformation.ContactNumber);
    Console.WriteLine("Order placed: " + customerInformation.IsOrderInProcess);
     
    For Updating item in a cache asynchronously
     
    // Add Item to Cache asynchronously
    _cache.InsertAsync("Customer:David:1001", new CacheItem(customer), DSWriteOption.None, null);
    Thread.Sleep(1000);
     
    Object objectCustomer = _cache.Get("Customer:David:1001");
    Customer customerInformation = (Customer)objectCustomer;
    Console .WriteLine("Customer ID: " + customerInformation.customerID);
    Console .WriteLine("Name: " + customerInformation.name);
    Console .WriteLine("City: " + customerInformation.City);
    Console .WriteLine("Contact Number: " + customerInformation.ContactNumber);
    Console .WriteLine("Order placed: " + customerInformation.IsOrderInProcess);
     
    For Removing item from a cache asynchronously
     
    // Remove Item from Cache asynchronously
    _cache.RemoveAsync("Customer:David:1001", null, DSWriteOption.None, null);
     
    For Clearing cache asynchronously
     
    // Clears Cache asynchronously
    _cache.ClearAsync(DSWriteOption.None, null, null);
     
     
  5. Start the Local Cache 'myCache'.
  6. Run the project.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.