加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

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

发布时间:2020-12-14 19:36:31 所属栏目:资源 来源:网络整理
导读:我有一个简单的Grails应用程序,需要在用户会话期间多次定期调用外部Web服务(使用界面). 我想缓存这个Web服务响应,但服务的结果会每隔几天更改一次,所以我想缓存一段时间(也许每天刷新). Grails缓存插件似乎不支持“实时”实现,所以我一直在探索一些可能的解
我有一个简单的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
    }
}

UPDATE

有很多好的选择.我决定用Burt建议的插件方法.我已经在上面的代码示例中添加了一个小的更改的示例答案,以帮助他人想要做类似的事情.此配置在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替换缓存管理器(因此您需要同时安装)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读