Alachisoft NCache 4.1 - Online Documentation

Adding items with priority settings

The cache uses priorities when it has to evict objects. Objects with low priority are removed first from the cache then the objects with higher priority . You can add an item to the Cache with priority settings by using the priority parameter of the Add or Insert method. The following example uses the Add method to add an item to the Cache with a high priority.
  1. Include the following namespaces in your application.
    using Alachisoft.NCache.Web.Caching;
    using Alachisoft.NCache.Runtime;
  2. Create a new Class named 'Customer' in your already created Test Application.
  3. Insert the following code in the 'Customer' class.
    class Customer
  4. Now insert the following code in the 'Main' of the project:
    Cache _cache = NCache.InitializeCache("mycache");
    Customer customer = new Customer (); = "David" ;
    customer.customerID = 1001;
    customer.City = "London" ;
    customer.ContactNumber = "+44-xxx-xxxx-xxxx" ;
    customer.IsOrderInProcess = true ;
    _cache.Add( "Customer:David:1001" , customer, null , Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High);
    Object objectCustomer = _cache.Get("Customer:David:1001");
    Customer customerInformation = (Customer)objectCustomer;
    Console.WriteLine("Customer ID: " + customerInformation.customerID);
    Console.WriteLine("Name: " +;
    Console.WriteLine("City: " + customerInformation.City);
    Console.WriteLine("Contact Number: " + customerInformation.ContactNumber);
    Console.WriteLine("Order placed: " + customerInformation.IsOrderInProcess);
  5. Start the Local Cache 'myCache'.
  6. Run the project.
Through NCache Manager, default priority can be specified for items of Caches/Clusters, if no priority is specified for them at the time of addition into cache. See Policies Tab for Cluster and Policies Tab for Local Cache presenting Default Priority configuration option.
See Also
Copyright © 2005-2012 Alachisoft. All rights reserved.