如何使用每个HTTP会话和超时缓存一块GSP?

时间:2014-08-15 10:46:43

标签: grails caching gsp

我正在使用Grails 2.2.0 GSP&#s异步包含:

<g:include action="asyncAction" />

问题:如何缓存&#34; asyncAction&#34;生成的内容a)在一定时间内,b)每个用户的会话?无需编写自定义标记。


我强制客户端缓存&#34; asyncAction&#34;,但每次Chrome / Firefox都会一直请求它:

response.addHeader("Cache-Control", "max-age=1600, public")

使用Grails&#34; cache&#34;提供的<cache:block>包裹了包含。插件 - 让它按应用程序缓存并永远。它根本不可定制。

1 个答案:

答案 0 :(得分:0)

<g:include action="asyncAction" />

调用asyncAction操作并将输出添加到页面。因此,如果您安装缓存插件,则应将@Cacheable注释添加到此操作

@Cacheable('someCache') 
def asyncAction() {

}

然后在Config.groovy中指定名为someCache

的缓存的超时
someCache {
    name 'someCache'

    timeToIdleSeconds 60 * 60  // item will be removed if not requested at least this often
    timeToLiveSeconds 60 * 120 // item will be always be removed after this period
}