2017-09-04 71 views
0

要求:在緩存過程中,如果爲緩存提供的最大堆大小超過,它應該溢出到磁盤並從那裏獲取它。 我的Ehcache配置:彈簧ehcache - 緩存沒有從磁盤中檢索

cache: 
timeToLiveSeconds: 3600 
ehcache: 
maxBytesLocalHeap: 1M 
setMaxBytesLocalDisk: 5G 
diskStore: cacheDiskStore 

這是ehcache.xml中配置(其中通過在陽明海運提供的值纏身):

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" name="CM1" 
updateCheck="false" maxBytesLocalHeap="1M" maxBytesLocalDisk="1M" maxEntriesLocalDisk="100000"> 

<!-- This is a default configuration, it is re-configured by the CacheConfiguration 
    Spring Bean, using the properties from the resources/config/*.yml files. --> 

<diskStore path="cacheDiskStore" /> 

<defaultCache eternal="false" overflowToDisk="true" /> 
<cache name="cache1" > 
    <persistence strategy="localTempSwap" /> 
</cache> 
<cache name="cache2" > 
    <persistence strategy="localTempSwap" /> 
</cache> 

我緩存大型媒體文件。但是,當我嘗試從緩存中檢索元素時,它始終爲空。這是我如何從緩存中檢索:

Element elt = CacheManager.getInstance() 
      .getEhcache("<cacheName>").get(<key>); 
    if (elt != null) { 
     System.out.println("**** Retrieved from cache *****"); 
     return elt; 
    } else { 
     System.out.println("**** NOT FOUND IN CACHE *****"); 
     return null; 
    } 

它不從緩存中進行拾取。 如果我增加緩存堆大小,它工作正常。任何幫助?

回答

1

在Ehcache 2.x中,磁盤存儲仍然佔用堆的密鑰。因此,有些緩存內容完全有可能被驅逐,因爲在磁盤上映射的鍵和堆上的映射之間,最終會導致驅逐。

還要注意,如果應用程序退出時高速緩存管理器發生乾淨關閉,開放源代碼磁盤存儲將僅在重新啓動時恢復數據。必須調用CacheManager.shutdown()

+0

嗨。謝謝。那麼如何避免這種驅逐呢? – hack

+0

當你在你的問題中陳述自己時,通過增加堆大小給緩存留出更多空間。確切的數量和最適合您的用例必須通過測試來確定。 –