Last active
December 11, 2015 20:38
-
-
Save ecowden/4656695 to your computer and use it in GitHub Desktop.
Configure cache headers for Grails controllers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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