简体   繁体   English

如何在spring java应用程序中缓存图像文件?

[英]How to cache image files in spring java application?

In my public facing web app, images ( along with meta data) are shown like in photo album app which is being developed using Spring framework.在我面向公众的 Web 应用程序中,图像(连同元数据)的显示方式就像使用 Spring 框架开发的相册应用程序一样。 What I want is to cache in RAM all images ( thousands of them) from the file system so that when image tag like is encountered in HTML:我想要的是将文件系统中的所有图像(数以千计)缓存在 RAM 中,以便在 HTML 中遇到类似图像标记时:

<img src="images/folder1/subfolder/myimage.jpg" />

the image is served from cache memory and not from disk by tomcat webserver for high performance.图像是从缓存内存中提供的,而不是由 tomcat 网络服务器从磁盘提供的,以实现高性能。

How to achieve this in spring framework web application?如何在 spring 框架 web 应用程序中实现这一点?

If mongodb is used to store web content like image meta data, how to achieve above in this scenario in the same spring web app?如果使用 mongodb 来存储图像元数据等 Web 内容,如何在同一个 Spring Web 应用程序中实现上述场景?

So there are two answers one involving no mongodb and one involving mongodb.所以有两个答案,一个不涉及 mongodb,一个涉及 mongodb。

Spring Cache is easy to setup initially and is quite configurable. Spring Cache 最初很容易设置,并且是非常可配置的。 If you provide a dedicated caching library, it will use that;如果您提供专用缓存库,它将使用它; if not, it will use an in memory concurrent hash map to hold cached data.如果没有,它将使用内存中的并发哈希映射来保存缓存数据。 You will need a dedicated controller, that may look similar to this:您将需要一个专用控制器,它可能类似于:

@ResponseBody
@RequestMapping(value = "/images/{path}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
@Cacheable("images")
public byte[] image(@PathVariable path) throws IOException {
    InputStream in = servletContext.getResourceAsStream("images/"+path);
    return IOUtils.toByteArray(in);
}

This loads the image first time is called and caches it.这第一次加载图像被调用并缓存它。 Each time method is called with the same parameters cache value is returned.每次调用方法时都会返回相同的参数缓存值。

I would first make sure this image caching is actually a boost to the performance.我首先要确保这个图像缓存实际上是对性能的提升。 But if you decide to use it, probably you want to use a dedicated library for the cache, not the default one that could fill the heap very fast.但是如果你决定使用它,可能你想使用一个专用的缓存库,而不是可以很快填满堆的默认库。 But of course it depends on the application.但当然这取决于应用程序。

Read about the Spring Cache https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html阅读 Spring Cache https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html

@Christaan's answer can also be extended to cache externally referenced images locally to your solution, for use in say captive environments. @Christaan​​ 的答案也可以扩展到将外部引用的图像本地缓存到您的解决方案中,以在圈养环境中使用。 For example:例如:

@ResponseBody
@RequestMapping(value = "/sample-image", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
@Cacheable("images")
public byte[] image() throws IOException {

            URL url = new URL("http://iminsys.com/assets/images/pages/about/edrich.jpg");
            InputStream in = new BufferedInputStream(url.openStream());
            return FileCopyUtils.copyToByteArray(in); // using Spring's FileCopyUtils
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM