|
| 1 | += Caching And Performance |
| 2 | + |
| 3 | +=== Caching |
| 4 | + |
| 5 | +Rendering can be an expensive operation so you may need to implement caching (using the excellent http://grails.org/plugin/springcache[spring-cache] plugin). |
| 6 | + |
| 7 | +==== Document Caching |
| 8 | + |
| 9 | +Rendering works internally by creating a `org.w3c.dom.Document` instance from the GSP page via the `xhtmlDocumentService`. If you plan to render the same GSP as different output formats, you may want to cache the created Document. |
| 10 | + |
| 11 | +[source,groovy] |
| 12 | +---- |
| 13 | +import grails.plugin.springcache.annotations.Cacheable |
| 14 | +
|
| 15 | +class CouponDocumentService { |
| 16 | + def xhmlDocumentService |
| 17 | +
|
| 18 | + @Cacheable('couponDocumentCache') |
| 19 | + class getDocument(serial) { |
| 20 | + xhmlDocumentService.createDocument(template: '/coupon', model: [serial: serial]) |
| 21 | + } |
| 22 | +} |
| 23 | +---- |
| 24 | + |
| 25 | +All of the render methods can take a `document` parameter instead of the usual `template`/`model` properties. |
| 26 | + |
| 27 | +[source,groovy] |
| 28 | +---- |
| 29 | +class CouponController { |
| 30 | + |
| 31 | + def couponDocumentService |
| 32 | + |
| 33 | + def gif = { |
| 34 | + def serial = params.id |
| 35 | + def document = couponDocumentService.getDocument(serial) |
| 36 | + |
| 37 | + renderGif(filename: "${serial}.gif", document) |
| 38 | + } |
| 39 | +} |
| 40 | +---- |
| 41 | + |
| 42 | +==== Byte Caching |
| 43 | + |
| 44 | +You can take things further and actually cache the rendered bytes. |
| 45 | + |
| 46 | +[source,groovy] |
| 47 | +---- |
| 48 | +import grails.plugin.springcache.annotations.Cacheable |
| 49 | +
|
| 50 | +class CouponGifService { |
| 51 | +
|
| 52 | + def couponDocumentService |
| 53 | + def gifRenderingService |
| 54 | +
|
| 55 | + def getGif(serial) { |
| 56 | + def document = couponDocumentService.getDocument(serial) |
| 57 | + def byteArrayOutputStream = gifRenderingService.gif([:], document) |
| 58 | + byteArrayOutputStream.toByteArray() |
| 59 | + } |
| 60 | +} |
| 61 | +---- |
| 62 | + |
| 63 | +[source,groovy] |
| 64 | +---- |
| 65 | +class CouponController { |
| 66 | + |
| 67 | + def couponGifService |
| 68 | + |
| 69 | + def gif = { |
| 70 | + def serial = params.id |
| 71 | + def bytes = couponGifService.getGif(serial) |
| 72 | + |
| 73 | + renderGif(bytes: bytes, filename: "${serial}.gif") |
| 74 | + } |
| 75 | +} |
| 76 | +---- |
| 77 | + |
| 78 | +=== Avoiding Byte Copying |
| 79 | + |
| 80 | +When rendering to the response, the content is first written to a temp buffer before being written to the response. This is so the number of bytes can be determined and the `Content-Length` header can be set. |
| 81 | + |
| 82 | +This copy can be avoided and the render (or bytes) can be written directly to the response output stream. This means that the `Content-Length` header will not be set unless you manually specify the length. |
0 commit comments