Alachisoft NCache 4.1 - Online Documentation

Add/Update Item

 
You can add items in cache from anywhere in your Java application, provided cache has already been initialized. The value must be a Java serializable object otherwise NCache will throw a serialization exception. There are five methods provided in the Cache class which enable us to add objects or any piece of data into cache.
 
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 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);
     
    // Add Item to Cache
    _cache.add("Customer:David:1001", customer);
     
    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());
     
  5. Start the Local Cache 'myCache'.
  6. Run the project.
 
 
Using Insert Method
 
Furthermore, you can use Insert() to either add or update the item in the cache. Insert() method adds the item if it does not already exist in the cache. Otherwise, it updates the item.
 
  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);
     
    // Update Item to Cache
    _cache.insert("Customer:David:100", customer);
     
    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());
     
  5. Start the Local Cache 'myCache'.
  6. Run the project.
 
 
See Also

 
Copyright © 2005-2012 Alachisoft. All rights reserved.