春季缓存默认大小

huangapple go评论70阅读模式
英文:

Spring cache default size

问题

默认的 Spring 缓存大小是多少?

如果有另一个可以被缓存的条目,会发生什么?它会忽略它还是会移除一个已缓存的条目以容纳新条目?如果它删除条目,那么它如何决定要删除哪一个?

我们如何手动提供缓存大小?
我指的不是其他缓存,比如 Caffeine、EHCache 等。
我指的是默认的 Spring 缓存。

英文:

What is the default size of spring cache ?
What happens if another entry comes which can be cached ? Does it ignore it or it removes an entry from cache to accommodate a new one ? If it deletes then how does it decide which one to delete ?

How can we provide a cache size manually ?
I am not referring to any other cache like Caffeine, EHCache etc.
I am referring the default spring cache.

答案1

得分: 3

我们可以按照以下方式添加自己的到期时间和最大限制:

public CacheManager cacheManager() {
    ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager() {
        @Override
        protected Cache createConcurrentMapCache(final String name) {
            return new ConcurrentMapCache(name, CacheBuilder.newBuilder().expireAfterWrite(7200,
                TimeUnit.SECONDS).maximumSize(500L).build().asMap(), false);
        }
    };
英文:
We can add our own expiry and max limit as follows :

 public CacheManager cacheManager() {
ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager() {
  @Override
  protected Cache createConcurrentMapCache(final String name) {
    return new ConcurrentMapCache(name, CacheBuilder.newBuilder().expireAfterWrite(7200,
        TimeUnit.SECONDS).maximumSize(500L).build().asMap(), false);
  }
};

答案2

得分: 2

Spring的缓存抽象并不涉及“管理”缓存内容(例如大小、驱逐或过期策略)的特定语义和细节。因此,所有这些设置必须在缓存提供程序级别(如ehcache、hazelcast、infinispan等)定义。

Spring缓存的默认实现是一个简单的ConcurrentHashMap

英文:

Spring's Cache Abstraction does not deal with the particular semantics and details of "managing" a cache's contents (such as as size, eviction or expiration policies). So all these settings must be define at the cache provider level(ehcache, hazelcast, infinispan..).

Default implementation of spring cache is a simple ConcurrentHashMap.

huangapple
  • 本文由 发表于 2020年5月5日 04:06:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61600694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定