Grails Caching...

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Grails Caching...

webmediauk
Hey Guys,
I'm having real trouble getting my head around how Grails Caching can be implemented to key on session and request params? Surely this should be really easy to do:

I have my controller methods annotated like so:

@grails.plugin.springcache.annotations.Cacheable("showLandingPageFormCache")

As far as I can see I can't add params or session variables to the cache store name like:

@grails.plugin.springcache.annotations.Cacheable("showLandingPageFormCache-${session.userid}")

or

@grails.plugin.springcache.annotations.Cacheable("showLandingPageFormCache-${params.id}")

Can anyone show me some examples of how I could do this?
Many Thanks
Baz
Reply | Threaded
Open this post in threaded view
|

Re: Grails Caching...

Ken Liu
Hi Baz -

The Cacheable annotation using a single string as an argument specifies the name of the cache. I think what you are looking for is to customize the cache key, not the cache name, right?

You can use SpEL notation to customize cache key generation. For example, if you have a controller action:

def showLandingPage(String id) {}

you can use the param as the cache key in the annotation like this:

@Cacheable(value='showLandingPageFormCache', key='id') 

See the Spring documentation for further information:

However, I don't know if you can use SpEL notation to use session params. You might try something like key = 'session.myvalue' but I suspect this would not work. In the worst case you can create your own custom cache key generator and specify this as part of the cache plugin configuration.

Let me know if this helps - I can come up with some better examples if you need them.

Ken 


On Mon, Jul 29, 2013 at 9:36 AM, webmediauk <[hidden email]> wrote:
Hey Guys,
I'm having real trouble getting my head around how Grails Caching can be
implemented to key on session and request params? Surely this should be
really easy to do:

I have my controller methods annotated like so:

*@grails.plugin.springcache.annotations.Cacheable("showLandingPageFormCache")*

As far as I can see I can't add params or session variables to the cache
store name like:

*@grails.plugin.springcache.annotations.Cacheable("showLandingPageFormCache-${session.userid}")*

or

*@grails.plugin.springcache.annotations.Cacheable("showLandingPageFormCache-${params.id}")*

Can anyone show me some examples of how I could do this?
Many Thanks
Baz



--
View this message in context: http://grails.1312388.n4.nabble.com/Grails-Caching-tp4647332.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Grails Caching...

webmediauk
Thanks for your help Ken,
It looks like my actions are being cached by the parameters by default? is this usual?
If that's the case I can simply use a cache buster parameter on the url.
I'm going to try this approach and let you know if it works.
I'll let you know.
Reply | Threaded
Open this post in threaded view
|

Re: Grails Caching...

Ken Liu
Hi Baz -

By default, the query string is used as part of the key when generating keys for Controller actions.

source here:

I would be careful about using a cache buster parameter; the cache will grow indefinitely if you are inserting a lot of randomly generated keys, eventually causing out of memory errors.

Ken


On Tue, Jul 30, 2013 at 4:55 AM, webmediauk <[hidden email]> wrote:
Thanks for your help Ken,
It looks like my actions are being cached by the parameters by default? is
this usual?
If that's the case I can simply use a cache buster parameter on the url.
I'm going to try this approach and let you know if it works.
I'll let you know.



--
View this message in context: http://grails.1312388.n4.nabble.com/Grails-Caching-tp4647332p4647366.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email