Alachisoft NCache 4.1 - Online Documentation

Get Cache Item

 
Items can be retrieved from the cache by using a Get method. Overloaded methods are also explained below:
 
Include the following namespace in your project:
 
using Alachisoft.NCache.Web.Caching;
 
Using Get Method:
 
Here is how you can retrieve items from the cache.
 
//returns null if item not found
Object myObject = _cache.Get("Customer:David:1001");
if (myObject == null)
{
    //item not found
}
 
To get latest Cache value of cache 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.
    
    Cache _cache = NCache.InitializeCache("mycache");
    _cache.Clear();
 
    Customer customer = new Customer();
    customer.Name = "David";
    customer.Id = 1001;
 
    // Add Item to Cache
    _cache.Add("Customer:David:1001", customer);
 
    // Get the latest value with respect to key "Customer:David:1001"
    CacheItem cItem = _cache.GetCacheItem("Customer:David:1001");
 
    if (cItem != null) // check if Item exists
    {
        // do some operations
        // ...
        // Update Cache Item
        _cache.Insert("Customer:David:1001", customer);
 
        CacheItem cItem1 = _cache.GetCacheItem("Customer:David:1001");
        Customer customer1 = (Customer)cItem1.Value;
 
        Console.WriteLine("Customer ID: " + customer1.Id);
        Console.WriteLine("Name: " + customer1.Name);
    }
 
5. Start the Local Cache 'myCache'.
6. Run the project.
 
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.