NCache 4.3 - 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 .NET serializable object otherwise NCache will throw a serialization exception if .NET serialization or NCache compact serialization is not being used.
 
Include the following namespace in your project:
 
using Alachisoft.NCache.Web.Caching;
 
Using Add Method
 
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. Create a new Class named 'Customer' in your already created Test Application.
  2. Insert the following code in the 'Customer' class.
     
    [Serializable]
    class Customer
    {
    ...
    }
     
  3. Now insert the following code in the 'Main' of the project:
     
    static void Main( string [] args)
    {
    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 ;
     
    // Add Item to Cache
    _cache.Add( "Customer:David:1001", customer);
     
    Object object_Customer = _cache.Get("Customer:David:1001");
     
    Customer customerInformation = ( Customer )object_Customer;
    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);
    }
     
  4. Start the Local Cache 'myCache'.
  5. Run the project.
 
Using Insert Method
 
Additionally, you can use "Insert" method to either add or update the item in the cache. Insert method adds item only if it does not already exist in the cache. Otherwise, it updates the item.
 
 
  1. Create a new Class named 'Customer' in your already created Test Application.
  2. Insert the following code in the 'Customer' class.
     
    [Serializable]
    class Customer
    {
    ...
    }
     
  3. Now insert the following code in the 'Main' of the project:
     
    static void Main( string [] args){
    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 ;
     
    // Insert Item to Cache
    _cache.Insert("Customer:David:1001", customer);
     
    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);
    }
     
  4. Start the Local Cache 'myCache'.
  5. Run the project.
 
 
See Also
 
© Copyright  2014 Alachisoft. All rights reserved.