我有一个简单的 Grails 应用程序,需要在用户会话期间(在使用界面时)多次定期调用外部 Web 服务。
我想缓存这个 Web 服务响应,但是服务的结果每隔几天就会改变一次,所以我想把它缓存一小段时间(也许每天刷新)。
Grails 缓存插件似乎不支持“生存时间”实现,所以我一直在探索一些可能的解决方案。我想知道什么插件或程序解决方案最能解决这个问题。
例子:
BuildConfig.groovy
plugins{
compile ':cache:1.0.0'
}
MyController.groovy
def getItems(){
def items = MyService.getItems()
[items: items]
}
MyService.groovy
@Cacheable("itemsCache")
class MyService {
def getItems() {
def results
//expensive external web service call
return results
}
}
更新
有很多不错的选择。我决定采用 Burt 建议的插件方法。我已经包含了一个示例答案,对上面的代码示例进行了微小的更改,以帮助其他想要做类似事情的人。此配置会在 24 小时后使缓存过期。
BuildConfig.groovy
plugins{
compile ':cache:1.1.7'
compile ':cache-ehcache:1.0.1'
}
配置文件
grails.cache.config = {
defaultCache {
maxElementsInMemory 10000
eternal false
timeToIdleSeconds 86400
timeToLiveSeconds 86400
overflowToDisk false
maxElementsOnDisk 0
diskPersistent false
diskExpiryThreadIntervalSeconds 120
memoryStoreEvictionPolicy 'LRU'
}
}