在Grails中缓存昂贵的Web服务调用的最佳策略 [英] Best strategy to cache expensive web service call in Grails

查看:103
本文介绍了在Grails中缓存昂贵的Web服务调用的最佳策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Grails应用程序,需要在用户会话期间多次定期调用外部Web服务(同时使用该接口)。



我想缓存这个Web服务响应,但服务的结果每隔几天就会改变一次,所以我想暂时缓存它(也许每天都会刷新一次)。

Grails缓存插件似乎不支持实时实现,因此我一直在探索一些可能的解决方案。我想知道哪个插件或程序化解决方案最能解决这个问题。



示例:

BuildConfig.groovy $ b

 插件{
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

//昂贵的外部Web服务调用

返回结果
}
}

更新



有很多不错的选择。我决定采用Burt建议的插件方法。我已经包含了一个样本答案,对上面的代码示例稍作修改,以帮助其他人想要做类似的事情。此配置会在24小时后过期。



BuildConfig.groovy

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

Config.groovy

  grails.cache.config = {
defaultCache {
maxElementsInMemory 10000
永恒假
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。组织/插件/缓存,但用一个使用Ehcache的缓存管理器取代缓存管理器(所以你需要同时安装)


I have a simple Grails application that needs to make a periodic call to an external web service several times during a user's session (while the use the interface).

I'd like to cache this web service response, but the results from the service change about every few days, so I'd like to cache it for a short time (perhaps daily refreshes).

The Grails cache plugin doesn't appear to support "time to live" implementations so I've been exploring a few possible solutions. I'd like to know what plugin or programatic solution would best solve this problem.

Example:

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

There were many good options. I decided to go with the plugin approach that Burt suggested. I've included a sample answer with minor changes to above code example to help others out wanting to do something similar. This configuration expires the cache after 24 hours.

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'
     }
 }

解决方案

The core plugin doesn't support TTL, but the Ehcache plugin does. See http://grails-plugins.github.com/grails-cache-ehcache/docs/manual/guide/usage.html#dsl

The http://grails.org/plugin/cache-ehcache plugin depends on http://grails.org/plugin/cache but replaces the cache manager with one that uses Ehcache (so you need both installed)

这篇关于在Grails中缓存昂贵的Web服务调用的最佳策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆