Skip to content

Instantly share code, notes, and snippets.

@ecowden
Last active December 11, 2015 20:38
Show Gist options
  • Save ecowden/4656695 to your computer and use it in GitHub Desktop.
Save ecowden/4656695 to your computer and use it in GitHub Desktop.
Configure cache headers for Grails controllers
/*
* Setting cache headers appropriately is both a performance issue and is necessary
* to avoid a slew of bugs from when files or data change over time.
*
* For this example, I'm using the Grails cache-headers plugin:
* http://grails.org/plugin/cache-headers
*/
/*
* First, we've designated two cache categories in the Config.groovy. This lets us centralize configuration
* and set environment-specific settings later.
*/
cache.headers.presets = [
never: false, //don't cache
reference: [shared:true, validFor: 3600] // store rarely-changing reference data for one hour
]
/*
* Then we can tell any controller method to use one of the above strategies:
*/
def doNotCacheMe() {
cache "never"
render whatever as JSON
}
def cacheMeForAWhile() {
cache "reference"
render whatever as JSON
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment