Alachisoft NCache 4.1 - Online Documentation

Adding Items

 
Once you have a valid cache handle, you can do the "Add" operations. The Add operation has many overloads and the basic overload takes two parameters, "key" as string and "value" as object. The value must be a Java serializable object otherwise NCache will throw a serialization exception.
 
Using Add Method
 
Here is how you can add items to the cache. An "Add" method will add item to the cache only if that item does not already exist in the cache. Otherwise, it will throw an exception.
 
  1. Include the following import statement 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);
     
    // Add Item to Cache
    _cache.add("Customer:David:1001", customer);
     
    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.
 
Using Insert Method
 
Additionally, you can use Insert() to either add or update the item in the cache. Insert() adds the item if it does not already exist in the cache. Otherwise, it updates the item.
 
  1. Include the following import statement 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);
     
    // Insert Item to Cache
    _cache.insert("Customer:David:1001", customer);
     
    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.