JCache 是一个基于 Java 的 API,用于 Java 对象的内存缓存。 这还包括跨 Java 虚拟机 (JVM) 的对象创建、失效、假脱机、共享访问和一致性。
NCache 是一个非常强大且通用的分布式缓存解决方案,带有大量的功能和工具。 NCache 提供了 JCache (JSR 107) 规范的实现。 使用它,您可以获得由 NCache.
需要创建 JCache 实例才能对缓存执行任何操作。 在Java中,getCache()方法被认为是获取JCache实例最方便的方法之一。 下面的代码示例演示了如何使用此方法初始化缓存。
/*Create an instance of JCache's caching provider to get JCacheManager's instance */
CachingProvider provider = Caching.getCachingProvider();
CacheManager manager = provider.getCacheManager();
/*Get a cache from manager via its string name.*/
javax.cache.Cache jCache = manager.getCache("myreplicatedcache");
成功初始化缓存并获得缓存句柄后,您可以对缓存执行许多操作,例如 添加, 正在取得, 更新及 删除 缓存中的数据。
下面的代码示例演示了如何使用缓存实例(您在上面的代码示例中创建)对缓存执行基本操作。
/*Adding data to the cache*/
Product product = fetchProductFromDB();
String key = "Product:" + product.getProductID();
jCache.put(key, product);
/*Update data in the cache*/
product.setUnitsInStock(newValue); // updated units
jCache.replace(key, product);
/*Get data from the cache*/
Object item = jCache.get(key);
/*Remove data from the cache*/
Boolean result = jCache.remove(key);
JCache 的另一个特点是 数据过期 其中缓存数据根据时间过期。 JCache 支持两种主要的过期类型,即 绝对到期 和 滑动到期. 在绝对过期中,缓存中的数据在固定时间间隔后过期。 在滑动过期中,缓存中的数据在一个时间间隔后过期,但是每次访问此数据时都会重置此时间间隔。
可以在缓存中添加和更新项目,并定义绝对和滑动到期。 在下面的代码示例中,数据被添加到定义了绝对过期的缓存中。
CachingProvider cachingProvider = Caching.getCachingProvider();
CacheManager manager = cachingProvider.getCacheManager();
Customer customer = new Customer();
customer.setAge(22);
String key = "Customer:" + customer.getCustomerID();
//configure the expiration duration
CompleteConfiguration<Object, Object> cacheConfig = new MutableConfiguration().setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));
//create cache with customized configuration settings
javax.cache.Cache jCache = manager.createCache("mypartitionedcache", cacheConfig);
//add item to cache which will expire after 1 minute
jCache.put(key, customer);
缓存级别通知 是在某些注册事件发生时触发的一般事件。 为了从 JCache 获取事件通知,您需要实现相关的事件相关接口。
在下面的示例中,假设您已经实现了事件过滤器工厂。
// class that implements the event-listener factory
public class MyCacheEntryListener<K, V>
implements CacheEntryCreatedListener<K, V>, Serializable
{...
for (CacheEntryEvent<? extends K,? extends V> event: events)
{
System.out.println("Received a " + event);
}
...
}
...
//configuring
MyCacheEntryListener<String,String> m_listener = new MyCacheEntryListener<String,String>();
MutableConfiguration<String, String> config = new MutableConfiguration<String, String>();
config.setTypes(String.class, String.class).addCacheEntryListenerConfiguration(
new MutableCacheEntryListenerConfiguration<String, String>
(FactoryBuilder.factoryOf(m_listener),FactoryBuilder.factoryOf(
new MyCacheEntryEventFilter<String, String>()),true,true));
Cache<String, String> jCache = cacheManager.createCache("myreplicatedcache", config);
提供的API NCache 与 JCache 提供的 API 相比,为您提供更强大的特性和功能。 其中的一些功能 NCache 提供但 JCache 不提供如下:
为了使用由 NCache API 你需要有一个它的缓存实例。
下面的代码示例演示了如何使用 JCache API 来获取 NCache 通过使用 unwrap 方法。 然后使用此实例将数据添加到缓存中 标签 (特点 NCache) 启用。 以后可以使用这些标签代替键从缓存中检索数据。
...
com.alachisoft.ncache.client.Cache cache = null;
cache = (com.alachisoft.ncache.client.Cache) jCache.unwrap(com.alachisoft.ncache.client.Cache.class);
Customer customer = fetchCustomer();
String customerKey = “Customer” + customer.getCustomerID();
// Specify Tags
List<Tag> tags = List.of(new Tag("East Coast Customers"));
CacheItem cacheItem = new CacheItem(customer);
cacheItem.setTags(tags);
cache.add(customerKey, cacheItem);