简体   繁体   English

如何在Grails中设置服务方法缓存

[英]How to setup service method caching in grails

My application has a couple of services that make external calls via httpClient (GET and POST) that are unlikely to change in months, but they are slow; 我的应用程序有几个服务,它们可以通过httpClient(GET和POST)进行外部调用,这些服务几个月之内不会发生变化,但是速度很慢。 making my application even slower. 使我的应用程序变得更慢。 Clarification: this is NOT about caching GORM/hibernate/queries to my db. 澄清:这不是关于将GORM /休眠/查询缓存到我的数据库中。

How can I cache these methods (persistence on disk gets bonus points...) in grails 2.1.0? 如何在grails 2.1.0中缓存这些方法(磁盘上的持久性获得奖励积分...)?

I have installed grails-cache-plugin but it doesn't seem to be working, or i configured it wrong (very hard to do since there are 2-5 lines to add only, but i've managed to do it in the past) 我已经安装了grails-cache-plugin,但是它似乎没有用,或者我配置错了(很难做,因为只添加了2-5行,但是我过去已经做到了)

I also tried setting up an nginx proxy cache in front of my app, but when i submit one of my forms with slight changes, I get the first submission as result. 我还尝试在应用程序前面设置nginx代理缓存,但是当我提交其中一个表单进行了微小的更改时,我得到的是第一个提交的结果。

Any suggestions/ideas will be greatly appreciated. 任何建议/想法将不胜感激。

EDIT : Current solution (based on Marcin's answer) 编辑 :当前解决方案(基于Marcin的答案)

My config.groovy: (the caching part only) 我的config.groovy :(仅缓存部分)

//caching
grails.cache.enabled = true
grails.cache.clearAtStartup = false

grails.cache.config = {
    defaults {
        timeToIdleSeconds 3600
        timeToLiveSeconds 2629740
        maxElementsInMemory 1
        eternal false
        overflowToDisk true
        memoryStoreEvictionPolicy 'LRU'
    }

    diskStore {
        path 'cache'
    }

    cache {
        name 'scoring'
    }
    cache {
        name 'query'
    }
}

The important parts are: 重要的部分是:

  • do not clear at startup (grails.cache.clearAtStartup = false) 在启动时不清除(grails.cache.clearAtStartup = false)
  • overflowToDisk=true persists all results over maxElementsInMemory overflowToDisk = true可将所有结果持久保存在maxElementsInMemory上
  • maxElementsInMemory=1 reduced number of elements in memory maxElementsInMemory = 1减少了内存中的元素数量
  • 'diskStore' should be writable by the user running the app. 运行应用程序的用户应可写“ diskStore”。

Grails Cache Plugin works quite well for me under Grails 2.3.11. Grails缓存插件在Grails 2.3.11下对我来说效果很好。 Documentation is pretty neat, but just to show you a draft... 文档非常整洁,但是只是为了向您展示草案...

I use the following settings in Config.groovy : 我在Config.groovy使用以下设置:

grails.cache.enabled = true
grails.cache.clearAtStartup = true

grails.cache.config = {
    defaults {
        maxElementsInMemory 10000
        overflowToDisk false
        maxElementsOnDisk 0
        eternal true
        timeToLiveSeconds 0
    }
    cache {
        name 'somecache'
    }
}

Then, in the service I use something like: 然后,在服务中,我使用类似以下内容:

@Cacheable(value = 'somecache', key = '#p0.id.toString().concat(#p1)')
def serviceMethod(Domain d, String s) {
    // ...
}

Notice the somecache part is reused. 注意somecache部分已被重用。 Also, it was important to use String as key in my case. 同样,在我的情况下,使用String作为键也很重要。 That's why I used toString() on id . 这就是为什么我在id上使用toString()的原因。

The plugin can be also set up to use disk storage, but I don't use it. 还可以将插件设置为使用磁盘存储,但我不使用它。

If it doesn't help, please provide more details on your issue. 如果没有帮助,请提供有关您的问题的更多详细信息。

This may not help, but if you upgrade the application to Grails 2.4.x you can use the @Memoize annotation. 这可能无济于事,但是如果将应用程序升级到Grails 2.4.x,则可以使用@Memoize批注。 This will automagically cache the results of each method call based upon the arguments passed into it. 这将根据传递给它的参数自动缓存每个方法调用的结果。

In order to store this "almost static" information you could use Memcached or Redis as a cache system. 为了存储“几乎静态”的信息,您可以使用Memcached或Redis作为缓存系统。 (There are many others) (还有很多)

This two cache systems allows you to store key-value data (in your case something like this "key_GET": JSON,XML,MAP,String ). 这两个缓存系统允许您存储键值数据(在您的情况下,类似于“ key_GET”:JSON,XML,MAP,String)。

Here is a related post: Memcached vs. Redis? 这是一篇相关的文章: Memcached与Redis?

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM