假设我有一个域类
class Profile{
String name
byte[] logo
}
和一个控制器:
class ImageController {
def renderImage ={
def image = Profile.get(params.id).logo
if (image) {
response.setContentLength(image.length)
response.getOutputStream().write(image)
} else {
response.sendError(404)
}
}
}
和普惠制:
<img width="48"
height="48"
src="${createLink(controller: 'image', action: 'renderImage', id: 1)}">
好的,到目前为止一切顺利。图像渲染良好,我很高兴。但是,由于 gsp 代码段是我主布局的一部分,因此每次重新加载页面时都会呈现图像。
我的问题:有没有办法缓存这个图像(blob mysql)?我打开了二级缓存等。但我不确定图像是否被缓存。你会怎么做?
谢谢。
将
static mapping = { cache true }
添加到配置文件域类以启用缓存。 只要您使用 get(id)
获取配置文件对象,它就会使用缓存的版本。 如果您使用的是更复杂的查询,则需要让 grails 知道该查询也是可缓存的。
请参阅 Grails 手册中的缓存部分。此外,打开休眠 SQL 日志记录以确认对象已缓存也会很有帮助。