我在我的项目中使用Ehcache,并且我有一个辅助类:
public static void reset(Ehcache cache, List<MyClass> list) {
List<Element> elementList = new ArrayList<Element>(list.size());
for (MyClass myClass : list) {
elementList.add(new Element(myClass.getId(), myClass));
}
// missing write lock
cache.removeAll();
// if findByKey(...) executed at this time, it's result will be null
cache.putAll(elementList);
}
public static MyClass findByKey(Ehcache cache, Serializable key) {...
public static List<MyClass> findAll(Ehcache cache) {...
public static MyClass findFirstByQuery(Ehcache cache, Criteria criteria) {...
public static List<MyClass> findByQuery(Ehcache cache, Criteria criteria) {...
public static void put(Ehcache cache, MyClass myClass) {...
我需要同步方法。我是否必须在所有方法中手动实现它,或者是否有任何其他(Ehcache 特定)方法来执行此操作?
最佳答案
您在这里只能靠自己了,因为 Ehcache 没有为此提供开箱即用的解决方案。
您还需要意识到,根据更新的频率和运行时间,这可能会导致瓶颈。 您不能找到一种不同的模式来执行此操作吗?
关于java - 以线程安全的方式重置Ehcache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25981657/