Reputation: 4742
What would be the syntax for memoizing a service method that is side-effect free and only does lookups? Would the memo persist from session to session or would it be kindof purposeless in the web world? Are there some examples of good places to use .memoize() in a grails app?
class DetermineStuffService{
def figureThisOut(def whatever){
//look up all sorts of stuff and do some heavy side-effect free processing
return nastyHashmap
}
}
So in a controller can I somehow call DetermineStuffService.figureThisOut(someRandomObject)
and take advantage of .memoize()
?
Upvotes: 1
Views: 976
Reputation: 66069
One problem with this is that memoize()
only works on closures. Closures are objects, so if you store one in your service, it is "state".
A better way of caching services in grails is with the Spring Cache plugin. Then to cache the result of a service method, just annotate the method with @Cacheable
. It has support for multiple caches, automatically flushing, and caching controller output as well.
Upvotes: 1