Alachisoft NCache 4.1 - Online Documentation

Async Operations

 
NCache provides the facility of providing Cache operations asynchronously so that maximum performance can be achieved. Following are the simple coding example implementing Async operations.
 
  1. Include the following import statements in your project:
     
    import com.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.
     
    import java.io.Serializable;
    public class Customer implements Serializable
    {
    }
     
  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.setCity("London");
    customer.setContactNumber("+44-xxx-xxxx-xxxx");
    customer.setOrderInProcess(true);
     
     
    For Adding item in a cache asynchronously
     
    // Add Item to Cache asynchronously
    _cache.addAsync(String key, CacheItem item, DSWriteOption dsWriteOption, DataSourceItemsAddedCallback onSourceItemAdded);
     
    Thread.sleep(1000);
    Object object_Customer = _cache.get("Customer:David:100");
    Customer customerInformation = (Customer)object_Customer;
    System.out.println("Customer ID: " + customerInformation.customerID);
    System.out.println("Name: " + customerInformation.name);
    System.out.println("City: " + customerInformation.getCity());
    System.out.println("Contact Number: " + customerInformation.getContactNumber());
    System.out.println("Order placed: " + customerInformation.isOrderInProcess());
     
    For Updating item in a cache asynchronously
     
    // Update Item to Cache asynchronously
    _cache.insertAsync((String key, Object value, AsyncItemUpdatedCallback asyncItemUpdatedCallback,
    String group, String subGroup);
     
    Thread.sleep(1000);
     
    Object object_Customer = _cache.get("Customer:David:100");
    Customer customerInformation = (Customer)object_Customer;
    System.out.println("Customer ID: " + customerInformation.customerID);
    System.out.println("Name: " + customerInformation.name);
    System.out.println("City: " + customerInformation.getCity());
    System.out.println("Contact Number: " + customerInformation.getContactNumber());
    System.out.println("Order placed: " + customerInformation.isOrderInProcess());
     
    For Removing item from a cache asynchronously
     
    // Remove Item from Cache asynchronously
    _cache.removeAsync(String key, AsyncItemRemovedCallback asyncItemRemovedCallback, DSWriteOption dsWriteOption, DataSourceItemsRemovedCallback onDataSourceItemRemoved);
     
    For Clearing cache asynchronously
     
    // Clear Cache asynchronously
    _cache.clearAsync(DSWriteOption dsWriteOption, AsyncCacheClearedCallback onAsyncCacheCleared, DataSourceClearedCallback onDataSourceCleared);
     
     
  5. Start the Local Cache 'myCache'.
  6. Run the project.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.