NCache 4.4 - Online Documentation

Removing Items

 
You can remove items from the cache from anywhere in your application using Remove and RemoveAsync methods. Include the following namespace in your project:
 
using Alachisoft.NCache.Web.Caching;
 
 
Using Remove Method
 
  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:
     
    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);
     
    // Removes key from Cache and returns the attached object with it
    Object removedObject = _cache.Remove("Customer:David:1001");
     
    Customer customerInformation = (Customer)removedObject;
    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