Data Expiration in JCache
This page explains how you can use JCache's expiration policies with NCache. The page details the prerequisites neccessary to use JCache with NCache, along with code snippets demonstrating the different expiration policies offered by JCache.
Prerequisites for Data Expiration in JCache
- For using JCache with NCache Professional, replace the
ncache-client
withncache-professional-client
in your pom.xml.
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-client</artifactId>
<version>x.x.x</version>
</dependency>
- Import the following packages in your application:
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
- Make sure that the data is serialized or registered with NCache Serialization format.
- Make sure that the cache is initialized and running.
Add Items with Accessed Expiration
Note
JCache Accessed Expiration behaves similar to the NCache’s Sliding Expiration. However, Accessed Expiration does not include cache updates.
The expiration time of a cache item in the AccessedExpiryPolicy
is based on its last access time. The following example creates an instance of JCache's caching provider to get JCacheManager's instance through NCache, it then creates a configuration file specifying the expiration time of a cache item.
// Get instance of JCacheManager.
CacheManager manager = Caching.getCachingProvider().getCacheManager();
//We are creating a config for object accessed expiration.
//It defines the expiry Duration of a Cache Entry based on the last time it was accessed.
//Accessed does not include a cache update.
MutableConfiguration<String, Product> config = new MutableConfiguration<>();
config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new AccessedExpiryPolicy(Duration.FIVE_MINUTES)));
//Get a cache from manager via its string name.
javax.cache.Cache jCache = manager.createCache("demoCache", config);
Product product = new Product();
product.setProductID("Product:1001");
product.setProductName("Coffee");
product.setPrice(100);
jCache.put("Product:1001", product);
Add Items with Created Expiration
Note
JCache Created Expiration behaves similar to the NCache’s Absolute Expiration.
The expiration time of a cache item in the CreatedExpiryPolicy
is based on its creation time. Therefore, any update to the cache item does not reset the expiry time. The following example adds a cache item with the CreatedExpiryPolicy
.
// Get instance of JCacheManager.
CacheManager manager = Caching.getCachingProvider().getCacheManager();
//We are creating a config for object creation expiration.
//It defines the expiry Duration of a Cache Entry based on when it was created.
//An update does not reset the expiry time.
MutableConfiguration<String, Product> config = new MutableConfiguration<>();
config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new CreatedExpiryPolicy(Duration.FIVE_MINUTES)));
//Get a cache from manager via its string name.
javax.cache.Cache jCache = manager.createCache("demoCache", config);
Product product = new Product();
product.setProductID("Product:1001");
product.setProductName("Coffee");
product.setPrice(100);
jCache.put("Product:1001", product);
Add Items with Modified Expiry
Note
JCache Modified Expiration behaves similar to the NCache Sliding Expiration. However, Modified Expiration includes updating, modifying, and creating an entry.
The ModifiedExpiryPolicy
defines the expiration time of the cache entry on the basis of its last update. The update can be any change in the entry. The following example adds an item with ModifiedExpiryPolicy
in the cache.
// Get instance of JCacheManager.
CacheManager manager = Caching.getCachingProvider().getCacheManager();
//We are creating a config for object modified expiration.
//It defines the expiry Duration of a Cache Entry based on the last time it was updated.
//Updating includes created and changing (updating) an entry.
MutableConfiguration<String, Product> config = new MutableConfiguration<>();
config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new ModifiedExpiryPolicy(Duration.FIVE_MINUTES)));
//Get a cache from manager via its string name.
javax.cache.Cache jCache = manager.createCache("demoCache", config);
Product product = new Product();
product.setProductID("Product:1001");
product.setProductName("Coffee");
product.setPrice(100);
jCache.put("Product:1001", product);
Add Items with Eternal Expiry
Note
JCache Eternal Expiration is similar to when there is no expiration policy in NCache.
An item added with EternalExpiryPolicy
does not expire. It is evicted based on the eviction priority assigned to it. The following example demonstrates how to add an item in cache with the EternalExpiryPolicy
.
// Get instance of JCacheManager.
CacheManager manager = Caching.getCachingProvider().getCacheManager();
//We are creating a config for object modified expiration.
//It specifies that Cache Entries won't expire.
//This however doesn't mean they won't be evicted if an underlying implementation needs to free-up resources where by it may choose to evict entries that are not due to expire.
MutableConfiguration<String, Product> config = new MutableConfiguration<>();
config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new EternalExpiryPolicy()));
//Get a cache from manager via its string name.
javax.cache.Cache jCache = manager.createCache("demoCache", config);
Product product = new Product();
product.setProductID("Product:1001");
product.setProductName("Coffee");
product.setPrice(100);
jCache.put("Product:1001", product);
Add Items with Touched Expiration
Note
JCache Touched Expiration behaves similar to the NCache Sliding Expiration. However, Touch Expiration includes updating, modifying, and creating an entry.
The TouchedExpiryPolicy
defines the expiration of an item on the basis of last touch, i.e, the time when the item was last created, updated, or accessed. The following example adds an item in the cache with TouchedExpiryPolicy
.
// Get instance of JCacheManager.
CacheManager manager = Caching.getCachingProvider().getCacheManager();
//We are creating a configuration for Touched Expiration.
//It defines the expiry Duration of a Cache Entry based on when it was last touched.
//A touch includes creation, update or access.
MutableConfiguration<String, Product> config = new MutableConfiguration<>();
config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new TouchedExpiryPolicy(Duration.FIVE_MINUTES)));
//Get a cache from manager via its string name.
javax.cache.Cache jCache = manager.createCache("demoCache", config);
Product product = new Product();
product.setProductID("Product:1001");
product.setProductName("Coffee");
product.setPrice(100);
jCache.put("Product:1001", product);
See Also
Hibernate Caching
Event Notifications in Cache
NCache Java Session Module