简体   繁体   English

每个Spring WebApp仅加载一次图像

[英]Loading images only one time per spring webapp

I need to load a bunch of images into my Spring webapp. 我需要将一堆图像加载到我的Spring Webapp中。 For performance reasons this should only be done after starting the webapp. 出于性能原因,仅应在启动webapp之后执行此操作。 The images should be saved in a singleton to access from multiple users, sessions etc 图片应单独保存,以供多个用户,会话等访问

I already build a Java Bean with an initalizer that loads the images, in my controller I inject this bean and get the images. 我已经用初始化器构建了一个Java Bean来加载图像,在控制器中,我注入了这个Bean并获取了图像。 Works, but injecting isn't the right thing for this. 可行,但是注入不是正确的选择。

How can I build a singleton bean to hold the images and only load the images one time per webapp? 如何构建单例bean来保存图像,并且每个Web应用程序仅加载一次图像?

Have you considered using EhCache built-in web caching ? 您是否考虑过使用EhCache内置的Web缓存 Just add it to your web.xml : 只需将其添加到您的web.xml

<filter>
    <filter-name>pageCachingFilter</filter-name>
    <filter-class>net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>pageCachingFilter</filter-name>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.gif</url-pattern>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.css</url-pattern>
</filter-mapping>

And configure SimplePageCachingFilter cache in your ehcache.xml : 并在ehcache.xml配置SimplePageCachingFilter缓存:

<cache name="SimplePageCachingFilter"
       maxElementsInMemory="1000"
       eternal="false"
       timeToIdleSeconds="31536000"
       timeToLiveSeconds="31536000"
       overflowToDisk="false"
       memoryStoreEvictionPolicy="LRU"
       statistics="true"
        />

EhCache will now intercept all client-side requests for static resources, read them once, put them in cache and return to the user. EhCache现在将拦截所有客户端对静态资源的请求,读取一次,将其放入缓存中并返回给用户。 To make it even better, it will even add HTTP Expiry headeres so the client won't call you again for the same resource. 为了使它更好,它甚至会添加HTTP Expiry标头,以便客户端不再为相同的资源调用您。

Isn't this approach better compared to manually-written loading and caching in a singleton? 与单例中手动编写的加载和缓存相比,这种方法难道不是更好吗?

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

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