Alachisoft NCache 4.1 - Online Documentation

Adding items with priority settings

 
The cache uses priorities when it has to evict objects; objects with a lower cost are removed from the cache before objects with a higher cost. You can add an item to the Cache with priority settings by using the priority parameter of the Add or Insert method. The following example uses the Add method to add an item to the Cache with a high priority.
 
  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 '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 );
     
    _cache.add("Customer:David:1001", customer, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High);
     
    Object object_Customer = _cache.get("Customer:David:1001");
     
    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());
     
     
  5. Start the Local Cache 'myCache'.
  6. Run the project.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.