英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论