EhCache 缓存使用模式

cache-aside:直接操作。先询问cache某条缓存数据是否存在,存在的话直接从cache中返回数据,绕过SOR;如果不存在,从SOR中取得数据,然后再放入cache中。 public V readSomeData(K key) { Element element; if ((element = cache.get(key)) != null) { return element.getValue(); } if (value = readDataFromDataStore(key)) != null) { cache.put(new Element(key, value)); } return value; } cache-as-sor:结合了read-through、write-through或write-behind操作,通过给SOR增加了一层代理,对外部应用访问来说,它不用区别数据是从缓存中还是从SOR中取得的。

read-through。

write-through。

write-behind(write-back):既将写的过程变为异步的,又进一步延迟写入数据的过程。

Copy Cache的两个模式:CopyOnRead和CopyOnWrite。

CopyOnRead指的是在读缓存数据的请求到达时,如果发现数据已经过期,需要重新从源处获取,发起的copy element的操作(pull);

CopyOnWrite则是发生在真实数据写入缓存时,发起的更新其他节点的copy element的操作(push)。

前者适合在不允许多个线程访问同一个element的时候使用,后者则允许你自由控制缓存更新通知的时机。

更多push和pull的变化和不同。