最大尺寸1的番石榴缓存。

时间:2020-11-28 20:44:54

Is it ok to use a Guava Cache of maximum size 1?

是否可以使用最大尺寸1的Guava缓存?

I'm reading that sometimes there can be evictions even before that maximum size is reached.

我读到,有时甚至在达到最大规模之前就会有驱逐。

However, I only need one cache entry. So I'm wondering what value to set for maximum size that would be safe, but not excessive.

但是,我只需要一个缓存条目。因此,我想知道,为安全而不过量设置最大尺寸的值是多少。

1 个解决方案

#1


1  

You should be able to use Cache.cleanUp() as explained in When Does Cleanup Happen? and test whether a maximum size of 1 suites your needs or not.

您应该能够使用Cache.cleanUp()来解释清理何时发生。并测试一个最大大小的套件是否满足您的需求。

e.g. The following shows that using a LoadingCache with maximum size of 1 will not evict an existing entry until a different entry is loaded to take its place:

下面的例子显示,使用最大大小为1的LoadingCache将不会驱逐现有的条目,直到加载不同的条目来取代它的位置:

final LoadingCache<Character, Integer> loadingCache = CacheBuilder.newBuilder()
        .maximumSize(1)
        .build(new CacheLoader<Object, Integer>() {
            private final AtomicInteger loadInvocationCount = new AtomicInteger();

            @Override
            public Integer load(Object key) throws Exception {
                return loadInvocationCount.getAndIncrement();
            }
        });
assert loadingCache.size() == 0;

assert loadingCache.getUnchecked('a') == 0;
assert loadingCache.size() == 1;

loadingCache.cleanUp();
assert loadingCache.size() == 1;

assert loadingCache.getUnchecked('a') == 0;
assert loadingCache.size() == 1;

assert loadingCache.getUnchecked('b') == 1;
assert loadingCache.size() == 1;

loadingCache.cleanUp();
assert loadingCache.size() == 1;

assert loadingCache.getUnchecked('a') == 2;
assert loadingCache.size() == 1;

loadingCache.cleanUp();
assert loadingCache.size() == 1;

Note that this may be specific to the type of LoadingCache being built so you will need to test whatever configuration you plan to use.

注意,这可能针对正在构建的LoadingCache类型,因此您需要测试您计划使用的任何配置。

#1


1  

You should be able to use Cache.cleanUp() as explained in When Does Cleanup Happen? and test whether a maximum size of 1 suites your needs or not.

您应该能够使用Cache.cleanUp()来解释清理何时发生。并测试一个最大大小的套件是否满足您的需求。

e.g. The following shows that using a LoadingCache with maximum size of 1 will not evict an existing entry until a different entry is loaded to take its place:

下面的例子显示,使用最大大小为1的LoadingCache将不会驱逐现有的条目,直到加载不同的条目来取代它的位置:

final LoadingCache<Character, Integer> loadingCache = CacheBuilder.newBuilder()
        .maximumSize(1)
        .build(new CacheLoader<Object, Integer>() {
            private final AtomicInteger loadInvocationCount = new AtomicInteger();

            @Override
            public Integer load(Object key) throws Exception {
                return loadInvocationCount.getAndIncrement();
            }
        });
assert loadingCache.size() == 0;

assert loadingCache.getUnchecked('a') == 0;
assert loadingCache.size() == 1;

loadingCache.cleanUp();
assert loadingCache.size() == 1;

assert loadingCache.getUnchecked('a') == 0;
assert loadingCache.size() == 1;

assert loadingCache.getUnchecked('b') == 1;
assert loadingCache.size() == 1;

loadingCache.cleanUp();
assert loadingCache.size() == 1;

assert loadingCache.getUnchecked('a') == 2;
assert loadingCache.size() == 1;

loadingCache.cleanUp();
assert loadingCache.size() == 1;

Note that this may be specific to the type of LoadingCache being built so you will need to test whatever configuration you plan to use.

注意,这可能针对正在构建的LoadingCache类型,因此您需要测试您计划使用的任何配置。