如何让Ehcache为无限高速缓存保持堆大小字节数统计? [英] How Can I Get Ehcache To Keep Heap Size Bytes Statistics For Unbounded Caches?

查看:620
本文介绍了如何让Ehcache为无限高速缓存保持堆大小字节数统计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ehcache(版本2.7.1),并且希望定期检索统计信息,例如缓存中的元素数量和缓存大小(以字节为单位)。我遇到的问题是使用 net.sf.ehcache.statistics.StatisticsGateway.getLocalHeapSizeInBytes()(通过调用 net检索StatisticsGateway .sf.ehcache.Ehcache.getStatistics()非常长时间使用15000个元素,总大小约为536MB。在我的本地机器上的一个例子中,获得此统计数据花了超过21秒。

I'm using Ehcache (version 2.7.1) and would like to periodically retrieve stats such as number of elements in the cache and the size of the cache in bytes. The problem that I'm running into though is that using net.sf.ehcache.statistics.StatisticsGateway.getLocalHeapSizeInBytes() (StatisticsGateway retrieved by calling net.sf.ehcache.Ehcache.getStatistics()) takes a very long time with 15000 elements with a total size of about 536MB. In one example on my local machine, it took over 21 seconds to get this statistic.

在我经历了这个之后,我想,世界上怎么做 maxBytesLocalHeap 缓存设置是否需要很长时间才能给出堆大小统计信息?我的缓存(如下所示)未设置 maxBytesLocalHeap ,而是设置 maxElementsInMemory 设置。所以,我决定使用 maxBytesLocalHeap 设置而瞧,现在需要大约1ms来获取统计数据。

After I experienced this I thought, "How in the world does the maxBytesLocalHeap cache setting work if it takes this long to give me the heap size stats?" My cache, seen below, did not set the maxBytesLocalHeap, but rather the maxElementsInMemory setting. So, I decided to use the maxBytesLocalHeap setting instead and voila, now it takes about ~1ms to get me the stat.

因此,如果缓存不使用 maxBytesLocalHeap 设置,Ehcache很可能不会保存堆大小的统计信息。相反,它为我为该统计数据进行的每次通话一次又一次地计算每个对象的大小。我想要这些统计数据,我只是不想将它们作为驱逐政策的一部分。所以我接着尝试设置 statistics =true,但是我仍然没有更快地获得我的堆大小统计数据。我已经尝试搜索Ehcache文档以找到答案,甚至还查看了 ehcache.xsd 以了解我的设置可能会遗漏,但我没有发现任何相关内容。

So it seems very likely that Ehcache is not saving statistics on heap size if the cache does not use the maxBytesLocalHeap setting. Instead, it's computing the size of every object again and again for every call I make for that stat. I would like these stats though, I just don't want to use them as part of an eviction policy. So I then tried to set statistics="true", however I still am not getting my heap size stats back any faster. I've tried searching Ehcache documentation to find an answer and even looked through the ehcache.xsd for settings that I'm potentially missing, but I didn't find anything relevant.

有人知道如何让Ehcache保持堆大小统计数据,即使 maxBytesLocalHeap 未使用缓存设置?

Does anyone know how to get Ehcache to keep heap size statistics even when the maxBytesLocalHeap cache setting is not used?

<cache name="myCache"
    timeToIdleSeconds="1800"
    overflowToDisk="false"
    memoryStoreEvictionPolicy="LRU"
    diskPersistent="false"
    maxElementsInMemory="0"
    statistics="true"/>


推荐答案

我开始思考我在问什么因为Ehcache是​​不可能的(至少是我的版本)

I'm beginning to think what I'm asking for isn't possible with Ehcache (at least the version I'm on)

这是 net.sf源代码中的池配置.ehcache.Cache.class

// on-heap pool configuration
Pool onHeapPool;
if (configuration.getMaxBytesLocalHeap() > 0) {
    PoolEvictor evictor = new FromLargestCachePoolEvictor();
    SizeOfEngine sizeOfEngine = cacheManager.createSizeOfEngine(this);
    onHeapPool = new BoundedPool(configuration.getMaxBytesLocalHeap(), evictor, sizeOfEngine);
} else if (getCacheManager() != null && getCacheManager().getConfiguration().isMaxBytesLocalHeapSet()) {
    onHeapPool = getCacheManager().getOnHeapPool();
} else {
    onHeapPool = new UnboundedPool();
}

稍后 net.sf.ehcache.store .MemoryStore 是使用 net.sf.ehcache.store.MemoryStore.MemoryStore(Ehcache,Pool,BackingFactory,SearchManager)从此池创建的。以下行创建 net.sf.ehcache.pool.PoolAccessor

Later on a net.sf.ehcache.store.MemoryStore is created from this pool, using net.sf.ehcache.store.MemoryStore.MemoryStore(Ehcache, Pool, BackingFactory, SearchManager). The following lines create the net.sf.ehcache.pool.PoolAccessor:

if (pool instanceof UnboundedPool) {
    this.poolAccessor = pool.createPoolAccessor(null, null);
} else {
    this.poolAccessor = pool.createPoolAccessor(new Participant(),
        SizeOfPolicyConfiguration.resolveMaxDepth(cache),
        SizeOfPolicyConfiguration.resolveBehavior(cache).equals(SizeOfPolicyConfiguration.MaxDepthExceededBehavior.ABORT));
}

由于池是UnboundedPool(未指定堆大小), PoolAccessor 是在没有 net.sf.ehcache.pool.SizeOfEngine 的情况下创建的,但更重要的是类型为 net.sf.ehcache.pool.impl.UnboundedPool.UnboundedPoolAccessor 。此类型的add方法不跟踪大小,而为有界池创建的PoolAccessor类型也是如此。 (参见 net.sf.ehcache.pool.impl.AbstractPoolAccessor.add(Object,Object,Object,boolean))。

Since the pool is an UnboundedPool (no heap size was specified), the PoolAccessor is created without a net.sf.ehcache.pool.SizeOfEngine, but more importantly the type is net.sf.ehcache.pool.impl.UnboundedPool.UnboundedPoolAccessor. The add method for this type does not track size, while the type of PoolAccessor that's created for for a bounded pool does. (see net.sf.ehcache.pool.impl.AbstractPoolAccessor.add(Object, Object, Object, boolean)).

所以,对于Ehcache有一个我可以使用的设置而言,我运气不好,但是,如果像我一样,你正在寻求一个无限的缓存,那么实现这个解决方案有一种愚蠢的方法。下面将跟踪添加的内存统计信息,但会允许无限制添加到缓存中:

So, I'm out of luck in terms of Ehcache having a setting that I can use, however there is a hacky way to achieve this solution if, like myself, you are seeking an unbounded cache. The below will keep track of memory stats as they are added, but will allow unbounded additions to the cache:

<cache name="myCache"
    timeToIdleSeconds="1800"
    memoryStoreEvictionPolicy="LRU"
    overflowToDisk="false"
    overflowToOffHeap="false"
    maxBytesLocalHeap="1">
    <pinning store="inCache" />
</cache>

这篇关于如何让Ehcache为无限高速缓存保持堆大小字节数统计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆