Guava CacheLoader-如果同时设置了expireAfterWrite和expireAfterAccess,invalidate不会立即使条目无效 [英] Guava CacheLoader - invalidate does not immediately invalidate entry if both expireAfterWrite and expireAfterAccess are set

查看:1808
本文介绍了Guava CacheLoader-如果同时设置了expireAfterWrite和expireAfterAccess,invalidate不会立即使条目无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

CacheBuilder.newBuilder()
            .maximumSize(1000)
            .expireAfterAccess(1, TimeUnit.MINUTES)
            .expireAfterWrite(1, TimeUnit.MINUTES)
            .build(new CacheLoader<String, Object>() {
                @Override
                public Object load (String key) {
                    return ...;
                }
            });

如果在返回的实例上调用invalidate(key),则随后对getUnchecked()的调用将始终使用先前的值,直到至少经过1分钟为止,这将导致它再次调用load(key).在invalidate()之后立即调用cleanUp()似乎也无效.

If calling invalidate(key) on the returned instance, a subsequent call to getUnchecked() will ALWAYS use the previous value until at least 1 minute has passed in which cause it will call load(key) again. A call to cleanUp() right after invalidate() seems to have no effect either.

我使用此功能不正确还是对缓存的工作方式不了解?

Am I using this feature incorrectly or not understanding something about how the caching is working?

每个 http://code.google.com/p/guava- library/wiki/CachesExplained :

",您可以在任何时候显式地使高速缓存条目无效,而不是等待将条目逐出.可以这样做:

"At any time, you may explicitly invalidate cache entries rather than waiting for entries to be evicted. This can be done:

分别使用Cache.invalidate(key)"

individually, using Cache.invalidate(key)"

我认为无效的调用将具有优先权,并且即使时间少于1分钟,也总是使该条目有资格被驱逐(即,下一个getUnchecked调用).

I would assume that the call to invalidate would take precedence and always make the entry be eligible for eviction (ie, the very next getUnchecked call) even if the time is less than 1 minute.

:我想出了我的问题.上面的观察确实是正确的,并且是缓存的预期性质.但是,我的具体问题是,当我真的需要不带任何参数的调用invalidateAll()时(使所有内容无效),有时我会用一个空的List调用invalidateAll([])(这不会使任何内容无效).我几乎希望invalideAll([])等同于invalidateAll(),但是我可以看到在哪里会引起混乱.

: I figured out my issue. The above observation is indeed true and the expected nature of the cache. However, my specific was issue was that I was sometimes calling invalidateAll([]) with an empty List (which doesn't invalidate anything), when I really needed to call invalidateAll() with no parameters (which invalidates everything). I would almost expect invalideAll([]) to be equivalent to invalidateAll(), but I could see where that would lead to some confusion.

推荐答案

...我不清楚您是在描述自己的期望还是观察到的结果,但是invalidate应该立即删除该条目-不要等待另一个查询-并应强制在该键的下一个查询中重新加载该值.如果不是这种情况,那就是一个错误.

...I'm not clear whether you're describing your expectations or your observations, but invalidate should remove the entry immediately -- not waiting for another query -- and should force the value to get reloaded on the very next query to that key. If that's not what happens, then that's a bug.

这篇关于Guava CacheLoader-如果同时设置了expireAfterWrite和expireAfterAccess,invalidate不会立即使条目无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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