问题描述
我们正在使用 Ignite,对于这个特定的缓存,当项目添加到缓存时,我们希望使用后写在 RDBMS 中插入新行。
问题在于,我们希望最初使用现有行加载缓存。
目前 Ignite 尝试写入加载在 cache.loadCache() 中的项目,这显然不是预期的行为。我想以某种方式表明这些项目不需要持久化。
我的搜索没有找到任何有用的信息,如果可以,请提供建议。非常感谢!
解决方法
IgniteCache#loadCache 应该忽略 writeThrough,换句话说,通过 loadCache 方法加载的记录不应插入到 RDBMS。
第二个选项是使用通过 IgniteCache#witkSkipStore() 或 IgniteDataStreamer#skipStore 获得的缓存/数据流器实例手动加载数据。
public class CacheStoreExample {
public static void main(String[] args) {
Ignite ignite = Ignition.start(
new IgniteConfiguration()
.setCacheConfiguration(new CacheConfiguration("cache")
.setWriteThrough(true)
.setWriteBehindEnabled(true)
.setCacheStoreFactory(new Factory<CacheStore>() {
@Override public CacheStore create() {
return new CacheStoreAdapter() {
@Override public void loadCache(IgniteBiInClosure clo,Object... args) {
System.out.println("Loading cache with single a single key/value:");
clo.apply(1,1);
}
@Override public Object load(Object key) throws CacheLoaderException {return null; }
@Override public void write(Cache.Entry entry) throws CacheWriterException {
System.out.println("Writing entry: "+entry);
}
@Override public void delete(Object key) throws CacheWriterException {}
};
}
})
));
IgniteCache<Object,Object> cache = ignite.cache("cache");
cache.loadCache((k,v) -> true);
System.out.println("The first record:"+cache.get(1));
System.out.println("Put operation skipping store");
cache.withSkipStore().put(2,2);
System.out.println("The second records:"+cache.get(2));
System.out.println("Put operation with write trough");
cache.put(3,3);
System.out.println("The third record:"+cache.get(3));
}
}