Caching 函数/闭包上的Groovy/Grails缓存

Caching 函数/闭包上的Groovy/Grails缓存,caching,grails,groovy,Caching,Grails,Groovy,我想知道如何向groovy函数添加缓存功能。它应该进入控制器还是服务?示例:如何缓存myService.getData()结果10秒 class myController { def getDataAsJson { String result = myService.getData(id) // returns JSON render result } } class myService { def getData(String id)

我想知道如何向groovy函数添加缓存功能。它应该进入控制器还是服务?示例:如何缓存myService.getData()结果10秒

class myController {
    def getDataAsJson {
        String result = myService.getData(id) // returns JSON
        render result
    }
}

class myService {
    def getData(String id) {
        return '{"hello":"world"}'
    }
}
我正在使用Grails3.3.2和Groovy2.4.13


干杯

通常,您会创建一个缓存服务。这样,它就可以在其他类或函数中使用。看看我是如何在这里创建缓存服务的:一般来说,您创建的是缓存服务。这样,它就可以在其他类或函数中使用。看看我是如何在这里创建缓存服务的:直截了当