gpt4 book ai didi

caching - 在 Grails 中缓存昂贵的 Web 服务调用的最佳策略

转载 作者:行者123 更新时间:2023-12-02 07:09:40 27 4
gpt4 key购买 nike

我有一个简单的 Grails 应用程序,需要在用户 session 期间(使用界面时)多次定期调用外部 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
}
}

更新

有很多不错的选择。我决定采用伯特建议的插件方法。我提供了一个示例答案,对上面的代码示例进行了细微的更改,以帮助其他人想做类似的事情。此配置会使缓存在 24 小时后过期。

BuildConfig.groovy

plugins{
compile ':cache:1.1.7'
compile ':cache-ehcache:1.0.1'
}

Config.groovy

grails.cache.config = {
defaultCache {
maxElementsInMemory 10000
eternal false
timeToIdleSeconds 86400
timeToLiveSeconds 86400
overflowToDisk false
maxElementsOnDisk 0
diskPersistent false
diskExpiryThreadIntervalSeconds 120
memoryStoreEvictionPolicy 'LRU'
}
}

最佳答案

核心插件不支持TTL,但Ehcache插件支持。请参阅http://grails-plugins.github.com/grails-cache-ehcache/docs/manual/guide/usage.html#dsl

http://grails.org/plugin/cache-ehcache插件取决于http://grails.org/plugin/cache但将缓存管理器替换为使用 Ehcache 的缓存管理器(因此您需要安装两者)

关于caching - 在 Grails 中缓存昂贵的 Web 服务调用的最佳策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14840575/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com