Now insert the following code in the 'Main' of the project.
Absolute Expiration:
The time period at which the added object expires and is removed from the cache. If you are using sliding expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration.
Cache _cache = NCache.initializeCache("myCache");
_cache.clear();
Customer customer = new Customer();
customer.name = "David";
customer.customerID = 1001;
customer.setCity("London");
customer.setContactNumber("+44-xxx-xxxx-xxxx");
customer.setOrderInProcess(true);
Calendar calendar = Calendar.getInstance();
calendar.add(calendar.MINUTE, 10);
_cache.add("Customer:David:1001", customer, null, calendar.getTime(), Cache.NoSlidingExpiration, CacheItemPriority.Normal);
Thread.sleep(1000*60*11);
Object object_Customer = _cache.get("Customer:David:1001");
Customer customerInformation = (Customer) object_Customer;
System.out.println("Customer ID: " + customerInformation.customerID);
System.out.println("Name: " + customerInformation.name);
System.out.println("City: " + customerInformation.getCity());
System.out.println("Contact Number: " + customerInformation.getContactNumber());
System.out.println("Order placed: " + customerInformation.isOrderInProcess());
Sliding Expiration:
Time interval between the last accessed time of the added object and object expiration time. If this value is the equivalent of 10 minutes, the object expires and is removed from the cache 10 minutes after it is last accessed. If you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.
Cache _cache = NCache.initializeCache("myCache");
_cache.clear();
Customer customer = new Customer();
customer.name = "David";
customer.customerID = 1001;
customer.setCity("London");
customer.setContactNumber("+44-xxx-xxxx-xxxx");
customer.setOrderInProcess(true);
_cache.add("Customer:David:1001", customer, null, Cache.NoAbsoluteExpiration, new TimeSpan(10,0), CacheItemPriority.Normal);
Thread.sleep(1000*60*10);
Object object_Customer = _cache.get("Customer:David:1001");
Customer customerInformation = (Customer) object_Customer;
System.out.println("Customer ID: " + customerInformation.customerID);
System.out.println("Name: " + customerInformation.name);
System.out.println("City: " + customerInformation.getCity());
System.out.println("Contact Number: " + customerInformation.getContactNumber());
System.out.println("Order placed: " + customerInformation.isOrderInProcess());