我正在使用 Spring 框架和 Java CXF 服务。
有多个客户端访问固定的数据列表,这些数据是从数据库中获取的。由于从数据库中获取是一个昂贵的调用并且数据库不会经常更改,所以我认为缓存值是很好的。
数据库刷新 -> 每天 15 次,时间间隔不定
缓存刷新 -> 每 15 分钟一次。
通过调用数据库加载缓存需要 15 秒。
现在,如果通过调用 DB 来刷新缓存,则需要 15 秒。在这 15 秒内,如果客户端要访问数据,我可以发送缓存中以前的数据。应用是可以容忍陈旧和过时的数据,而不是等待 15 秒(有一个 delta 函数可以在加载时间缓存后带来数据,这是非常便宜的)。 ehcache 有没有办法在加载新数据时返回缓存中的旧数据,同时缓存以 15 分钟的固定间隔刷新?
最佳答案
A consistent copy of the cache on local disk provides many possibilities for business requirements, such as working with different datasets according to time-based needs or moving datasets around to different locations. It can range from a simple key-value persistence mechanism with fast read performance, to an operational store with in-memory speeds during operation for both reads and writes.
Ehcache 有一个 RestartStore,它提供快速重启和缓存持久化选项。 RestartStore 实现了内存缓存的磁盘镜像。任何重启后,缓存中最后的数据会自动从磁盘加载到 RestartStore,然后缓存中的数据可用。
通过将子元素添加到缓存配置来配置磁盘上的缓存持久性。该子元素包括两个属性:strategy 和 synchronousWrites。
<cache>
<persistence strategy=”localRestartable|localTempSwap|none|distributed” synchronousWrites=”false|true”/>
</cache>
更多信息Ehcache
关于java - 刷新缓存时ehCache能否返回旧的缓存值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865960/