Originally we were using the MimeTypeAwareKeyGenerator class to generate the cache keys, we used this generator because some of our actions are used as REST services as well that can be used with JSON or XML, so what we did was to create a new generator that extended this class and made it not only mime type aware but also user aware, our class looked like this:
public class MimeTypeAndAuthenticationAwareKeyGenerator extends MimeTypeAwareKeyGenerator {
@Override
protected void generateKeyInternal(CacheKeyBuilder builder, FilterContext context) {
super.generateKeyInternal(builder, context)
def springSecurityService = ApplicationHolder.application.mainContext.getBean('springSecurityService')
if (springSecurityService?.isLoggedIn()) {
builder << "authUserId=${springSecurityService.principal.getId()}".toString()
}
}
}
With this approach we are considering the user id in the key for the caching allowing us to create separate cache entries if the user is logged in or not. So, now if you access a page and you are not logged in you will get a cached page with no user name on the top, but if you logged in you will get a page with your user name on the top that will be cached for the next time you access it.
Now you need to tell your Grails application to use this new class you created and to do it you just need to set the keyGenerator attribute of the springcacheFilter, you can do this via the Config.groovy, like this:
beans {
springcacheFilter {
keyGenerator = new MimeTypeAndAuthenticationAwareKeyGenerator()
}
}
How do you set this class to be the key generator that is used?
ReplyDeleteThat is a great question, I can't believe I forgot that part on the actual blog post. The way to do it is through the Config.groovy, like this:
ReplyDeletebeans {
springcacheFilter {
keyGenerator = new MimeTypeAndAuthenticationAwareKeyGenerator()
}
}
I am going to update the post so that information is available there. Thanks!
This seems to be a cool idea.
ReplyDeleteCan anyone tell ne where to create MimeTypeAndAuthenticationAwareKeyGenerator class?
Is there any dependencies for this? Because, I'm not able to find CacheKeyBuilder, FilterContext etc.
I installed spring cache plugin.
ReplyDeleteI create MimeTypeAuthenticationAwareKeyGenerator under /src/groovy folder.
import org.codehaus.groovy.grails.commons.ApplicationHolder
//import grails.plugin.springcache.web.key.AbstractKeyGenerator
//import grails.plugin.springcache.web.FilterContext
import grails.plugin.springcache.web.ContentCacheParameters
import grails.plugin.springcache.key.CacheKeyBuilder
// import not found MimeTypeAwareKeyGenerator !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
class MimeTypeAndAuthenticationAwareKeyGenerator extends MimeTypeAwareKeyGenerator {
@Override
protected void generateKeyInternal(CacheKeyBuilder builder, ContentCacheParameters context) {
super.generateKeyInternal(builder, context)
def springSecurityService = ApplicationHolder.application.mainContext.getBean('springSecurityService')
if (springSecurityService?.isLoggedIn()) {
builder << "authUserId=${springSecurityService.currentUser.username}".toString()
}
}
}
But, MimeTypeAwareKeyGenerator is not available in this plugin.
Any idea?
Hi,
ReplyDeleteSome of these classes changed when a new version of the plug in was released, please review this other post http://maricel-tech.blogspot.com/2011/06/grails-spring-security-with-spring.html, which is an updated version of these that includes these changes and how to tackle them.
Thanks,
Maricel.
Hey thanks.
ReplyDeleteI'll look into it.
Hi,
ReplyDeleteI have created a sample grails project (v1.3.5) to test caching contents per user. But, it is not working as I expected. You can find more detail on the problem in below link:
https://docs.google.com/leaf?id=0B3j9inzR_LLhNDY3N2M5OTItM2Y3Mi00YmZlLTlmNTctZmFlZGRiODI0M2Ni&hl=en_US
I have uploaded the source code for this project at below location. It will be grateful if someone looks into it & let me know where I'm wrong.
https://docs.google.com/leaf?id=0B3j9inzR_LLhYjhiMmY3MDUtNTVlYi00ZTE2LWIyYTctZDAxMjQ4YTg2NTVi&hl=en_US
Thanks,
Kishore