Remove user from cache in Grails app using Acegi Plugin

This memo shows how to remove a user from cache in a Grails application using Acegi Plugin

The Grails Acegi Plugin has user caching activated by default. If you make any changes to a user those changes won't be available immediately. Here are two solutions to change this behavior.

Remove the user from cache

The Acegi plugin provides a userCache bean which can be injected automatically. With a removeUserFromCache method call on the userCache bean a user can be removed from the cache. The next time the user is requested by the system the user will be retrieved from the database and all updates to the instance are available immediately. Following code fragment shows the described solution.

def userCache

def someAction = {
	GrailsUser user = authenticateService.principal() as GrailsUser

Disable the cache

The second solution is to simply disable the cache. This should not have any impact on applications with little usage. This of cause increases the communication between your application and a database server. To disable the cache add the following line of code to the <app-root>/grails-app/conf/SecurityConfig.groovy file.

cacheUsers = false

Date published:
Date modified: 2010-12-04
Keywords: Grails, Acegi, Cache, Grails