假设我有一个域类
class Profile{
String name
byte[] logo
}
和一个 Controller :
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)
}
}
}
和一个gsp:
<img width="48"
height="48"
src="${createLink(controller: 'image', action: 'renderImage', id: 1)}">
好的,到目前为止,一切都很好。图像效果很好,我很高兴。但是,由于gsp代码段是我主要布局的一部分,因此每次重新加载页面时都会渲染图像。
我的问题:有没有办法缓存此镜像(blob mysql)?我打开了二级缓存等。但是我不确定是否缓存了图像。你会怎么做?
谢谢。
最佳答案
将static mapping = { cache true }
添加到您的Profile域类中以打开缓存。只要您使用get(id)
来获取Profile对象,它将使用缓存的版本。如果您使用的是更复杂的查询,则需要让grails知道该查询也是可缓存的。
请参阅Grails手册中的Caching section。另外,打开hibernate SQL logging以确认是否已缓存对象可能会有所帮助。
关于caching - GORM(纹理)-缓存Blob图片或其他内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10092756/