Quantcast

Grails optional cache layer

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Grails optional cache layer

calugaru.cristi
Hi,

We have a Grails Web App, and we wanted to add a cache layer for some of the more expensive-computation calls.
We settled to the base grails cache plugin, plus the redis cache implementation.

How do you configure the cache as an optional layer? If a request comes, and does not find the cache being "up"(for ex the redis process is down, or the cache machine is not accessible) it automatically fails over to the database and does the query? I see the cache as just an optional layer which if exists, should be used, but it should not make the web app fully dependent on it (which seems to be the default behavior with the cache plugin).

Is there a  convenient way to do this?

Thanks for any suggestions.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Grails optional cache layer

Jeff Brown-3

On Sep 22, 2012, at 7:18 AM, calugaru.cristi <[hidden email]> wrote:

> Hi,
>
> We have a Grails Web App, and we wanted to add a cache layer for some of the
> more expensive-computation calls.
> We settled to the base grails cache plugin, plus the redis cache
> implementation.
>
> How do you configure the cache as an optional layer? If a request comes, and
> does not find the cache being "up"(for ex the redis process is down, or the
> cache machine is not accessible) it automatically fails over to the database
> and does the query? I see the cache as just an optional layer which if
> exists, should be used, but it should not make the web app fully dependent
> on it (which seems to be the default behavior with the cache plugin).
>
> Is there a  convenient way to do this?
>
> Thanks for any suggestions.
>
>

You could have 2 code paths to retrieve the data, a caching path and a non caching path.  You could invoke the caching path and if it fails then invoke the non caching path.  You have to decide what "fails" means.  For example, if you attempt to retrieve data from the cache and the data isn't there, is that a failure or does that just mean the requested data doesn't exist (in the cache or anywhere else)?  

There may be important details about your application and environment that need to be factored in here but a simplistic approach if your failures manifest with an exception might look something like this pseudo code…

class MyService {

    def dataService

    def getData() {
        def data
        try {
            data = dataService.getDataFromCache()
        } catch (SomeExceptionType e) {
            data = dataService.getDataFromDatabase()
        }
        data
    }
}




jsb
--
Jeff Brown
[hidden email]
SpringSource - A Division Of VMware
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Grails optional cache layer

calugaru.cristi
Thanks for the quick reply.

I am thinking here of infrastructure problems (hardware problems with that machine - if hardware fails after some time, if something happens with the redis process itself).
For example, in the case when we don't have resources/time to setup a cluster and fail-over for the caching layer, but we would still want to benefit from just one 'insecure' cache machine.

Regarding what 'fail' means, if data does not exist in the cache that is by no means a failure in our acceptance. It should be fetched from the database and then also inserted in the cache for subsequent identical calls, as it actually happens now.

I just see a cache layer as something which would fail silently to the actual data source, in case of any connection/hardware/OS problems. Am I completely wrong in my assumption?

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Grails optional cache layer

Jeff Brown-4




I just see a cache layer as something which would fail *silently *to the
actual data source, in case of any connection/hardware/OS problems. Am I
completely wrong in my assumption?




It may be that your 2nd level cache is really what you are describing. Beyond that, if you are asking if Grails supports some declarative way to express that behavior, the answer is "no".



JSB
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Grails optional cache layer

calugaru.cristi
And what would be a viable approach for doing this in a programmatic fashion?

We currently have a global exception handler, which gets triggered at any raised exception.
Should we maybe insert some logic there, and based on some higher level spring exception types, decide to route the call towards the database in case of a problem with the cache?

Or, maybe create a totally different service interceptor, which would intercept only the Cacheable annotated methods, and do the proper routing there?
Loading...