![](/img/trans.png)
[英]Setup Zookeeper to deliver configuration one time only per application instance [Spring]
[英]Loading images only one time per spring webapp
我需要將一堆圖像加載到我的Spring Webapp中。 出於性能原因,僅應在啟動webapp之后執行此操作。 圖片應單獨保存,以供多個用戶,會話等訪問
我已經用初始化器構建了一個Java Bean來加載圖像,在控制器中,我注入了這個Bean並獲取了圖像。 可行,但是注入不是正確的選擇。
如何構建單例bean來保存圖像,並且每個Web應用程序僅加載一次圖像?
您是否考慮過使用EhCache內置的Web緩存 ? 只需將其添加到您的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>
並在ehcache.xml
配置SimplePageCachingFilter
緩存:
<cache name="SimplePageCachingFilter"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="31536000"
timeToLiveSeconds="31536000"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"
statistics="true"
/>
EhCache現在將攔截所有客戶端對靜態資源的請求,讀取一次,將其放入緩存中並返回給用戶。 為了使它更好,它甚至會添加HTTP Expiry
標頭,以便客戶端不再為相同的資源調用您。
與單例中手動編寫的加載和緩存相比,這種方法難道不是更好嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.