简体   繁体   English

是否可以将ehcache配置为仅按时间顺序缓存一些元素?

[英]Is there a way to configure ehcache to only cache some numbers of elements in time order?

My current config is as below, I intended to only cache up to 30 elements and evict the oldest one when the number is more than 30: 我当前的配置如下,我打算最多缓存30个元素,并在数量大于30时逐出最旧的元素:

<ehcache>
    <diskStore path="/path/to/store/"></diskStore>
    <cache name="myCache"
       eternal="false"
       maxEntriesLocalHeap="30"
       maxEntriesLocalDisk="30"
       memoryStoreEvictionPolicy="FIFO">
        <persistence strategy="localTempSwap"/>
    </cache>
</ehcache>

I have another scheduled job which runs every minute to put a new element into the cache. 我还有另一个计划的作业,该作业每分钟运行一次,以将新元素放入缓存。 So I expect to get only 30 elements within recent 30 minutes. 因此,我希望在最近30分钟内仅能获得30个元素。 But the expiring/eviction was not as expected. 但是到期/撤离与预期的不同。 Some very old elements were still kept while some elements within recent 30 minutes were unexpectedly evicted. 仍然保留了一些非常古老的元素,而最近30分钟内的某些元素却被意外逐出。 Is there anything I missed here? 我有什么想念的吗?

I've read through the expiring/eviction related documentation in ehcache, but did not find any clue. 我已经阅读了ehcache中与到期/收回相关的文档,但没有发现任何线索。 Hope someone can help :) 希望有人可以帮助:)

BTW, the ehcache version is 2.6.6 顺便说一句,ehcache版本是2.6.6

Ehcache eviction strategies have always had some level of heuristics in them. Ehcache驱逐策略始终在其中具有一定程度的启发式。 For example the eviction policy is not applied on the entire population of the cache, that would be too costly for large caches, but rather on a sample. 例如,逐出策略未应用于高速缓存的整个填充,这对于大型高速缓存而言代价太高,而仅适用于样本。

That's why you are experiencing an inexact outcome compared to your requirements. 因此,与您的要求相比,您得到的结果不准确。

Now given the limited number of elements you want to keep, a LinkedHashMap sounds like a great choice, although it is not safe for multi threaded access. 现在,鉴于您要保留的元素数量有限, LinkedHashMap听起来是一个不错的选择,尽管对于多线程访问来说并不安全。

Thanks @Louis for the answer and suggestion. 感谢@Louis的回答和建议。

In ehcache doc, I learned that memoryStoreEvictionPolicy is only for memory store , while in my case disk store is used. 在ehcache doc中,我了解到memoryStoreEvictionPolicy仅用于memory store ,而在我的情况下使用磁盘存储。 And also eviction policy for disk store is by default LFU and not configurable . 而且,默认情况下,磁盘存储的逐出策略是LFU且不可配置

In Ehcache, the MemoryStore may be limited in size (see How to Size Caches for more information). 在Ehcache中,MemoryStore的大小可能受到限制(有关更多信息,请参见如何调整缓存大小)。 When the store gets full, elements are evicted. 当商店装满时,将逐出元素。 The eviction algorithms in Ehcache determine which elements are evicted. Ehcache中的逐出算法确定逐出哪些元素。 The default is LRU. 默认值为LRU。

... The DiskStore eviction algorithm is not configurable. ... DiskStore逐出算法不可配置。 It uses LFU. 它使用LFU。

So to achieve what I am expecting, I changed to use memory store only, given that the current persistent strategy for disk store is not restartable (localTempswap), which is equivalent in using memory store. 因此,为了达到我的期望,我更改为仅使用内存存储,因为磁盘存储的当前持久性策略不可重新启动(localTempswap),这与使用内存存储等效。 The final config is as: 最终配置如下:

<cache name="myCache"
       eternal="false"
       maxEntriesLocalHeap="30"
       memoryStoreEvictionPolicy="FIFO">
</cache>

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

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