Alachisoft NCache 4.1 - Online Documentation

Access Multiple Caches in Single Application

 
NCache allows you to initialize multiple cache instances in a same application. The following code shows how to use this feature.
 
1. Include the following namespace in your application.
 
using 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.
 
    [Serializable]
    class Customer
    {
        ...
    }
 
4. Now insert the following code in the 'Main' of the project:
 
    Cache _cache1 = NCache.InitializeCache("mycache");
    Cache _cache2 = NCache.InitializeCache("myreplicatedcache");
 
    // Perform any of the operations provided by NCache on any of the cache initialized
    _cache1.Clear();
    _cache2.Clear();
 
    Customer customer = new Customer();
    customer.name = "David";
    customer.customerID = 1001;
    Customer customer1 = new Customer();
    customer1.name = "Alex";
    customer1.customerID = 1002;
 
    // Add Item to Cache
    NCache.Caches["mycache"].Add("Customer:David:1001", customer);
    NCache.Caches["myreplicatedcache"].Add("Customer:Alex:1002", customer1);
 
    // Get Items from Cache
    Object objectCustomer = NCache.Caches["mycache"].Get("Customer:David:1001");
    Object objectCustomer1 = NCache.Caches["myreplicatedcache"].Get("Customer:Alex:1002");
 
    Customer customerInformation = (Customer)objectCustomer;
    Console.WriteLine("Customer ID: " + customerInformation.customerID);
    Console.WriteLine("Name: " + customerInformation.name);
    Console.WriteLine();
 
    customerInformation = (Customer)objectCustomer1;
    Console.WriteLine("Customer ID: " + customerInformation.customerID);
    Console.WriteLine("Name: " + customerInformation.name);
    Console.WriteLine();
 
5. Start both the caches.
6. Run the project.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.