寻找简单的 Java 内存缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/575685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looking for simple Java in-memory cache
提问by sanity
I'm looking for a simple Java in-memory cache that has good concurrency (so LinkedHashMap isn't good enough), and which can be serialized to disk periodically.
我正在寻找一个简单的 Java 内存缓存,它具有良好的并发性(因此 LinkedHashMap 不够好),并且可以定期序列化到磁盘。
One feature I need, but which has proved hard to find, is a way to "peek" at an object. By this I mean retrieve an object from the cache without causing the cache to hold on to the object any longer than it otherwise would have.
我需要但已证明很难找到的一个功能是一种“窥视”对象的方法。我的意思是从缓存中检索一个对象,而不会导致缓存保留该对象的时间比其他方式更长。
Update:An additional requirement I neglected to mention is that I need to be able to modify the cached objects (they contain float arrays) in-place.
更新:我忽略提到的另一个要求是我需要能够就地修改缓存对象(它们包含浮点数组)。
Can anyone provide any recommendations?
任何人都可以提供任何建议吗?
采纳答案by sanity
Since this question was originally asked, Google's Guava librarynow includes a powerful and flexible cache. I would recommend using this.
由于这个问题最初被问到,谷歌的 Guava 库现在包含一个强大而灵活的缓存。我会推荐使用这个。
回答by TofuBeer
How about this: https://commons.apache.org/proper/commons-jcs/(updated to new address, as JCS is now in Apache Commons)
这个怎么样:https: //commons.apache.org/proper/commons-jcs/(更新到新地址,因为 JCS 现在在 Apache Commons 中)
回答by Alex Miller
Ehcacheis a pretty good solution for this and has a way to peek (getQuiet()is the method) such that it doesn't update the idle timestamp. Internally, Ehcache is implemented with a set of maps, kind of like ConcurrentHashMap, so it has similar kinds of concurrency benefits.
Ehcache是一个很好的解决方案,并且有一种查看方法(getQuiet()是方法),这样它就不会更新空闲时间戳。在内部,Ehcache 是用一组映射实现的,有点像 ConcurrentHashMap,因此它具有类似的并发优势。
回答by Stephen
回答by Evan
If you're needing something simple, would this fit the bill?
如果您需要一些简单的东西,这是否符合要求?
Map<K, V> myCache = Collections.synchronizedMap(new WeakHashMap<K, V>());
It wont save to disk, but you said you wanted simple...
它不会保存到磁盘,但你说你想要简单......
Links:
链接:
(As Adam commented, synchronising a map has a performance hit. Not saying the idea doesn't have hairs on it, but would suffice as a quick and dirty solution.)
(正如 Adam 评论的那样,同步地图会影响性能。并不是说这个想法没有毛病,但作为一个快速而肮脏的解决方案就足够了。)
回答by Serhii Bohutskyi
Try this:
尝试这个:
import java.util.*;
public class SimpleCacheManager {
private static SimpleCacheManager instance;
private static Object monitor = new Object();
private Map<String, Object> cache = Collections.synchronizedMap(new HashMap<String, Object>());
private SimpleCacheManager() {
}
public void put(String cacheKey, Object value) {
cache.put(cacheKey, value);
}
public Object get(String cacheKey) {
return cache.get(cacheKey);
}
public void clear(String cacheKey) {
cache.put(cacheKey, null);
}
public void clear() {
cache.clear();
}
public static SimpleCacheManager getInstance() {
if (instance == null) {
synchronized (monitor) {
if (instance == null) {
instance = new SimpleCacheManager();
}
}
}
return instance;
}
}
回答by yusufaytas
回答by cruftex
Another option for an in-memory java cache is cache2k. The in-memory performance is superior to EHCache and google guava, see the cache2k benchmarks page.
内存中 java 缓存的另一个选项是cache2k。内存性能优于 EHCache 和 google guava,参见cache2k 基准测试页面。
The usage pattern is similar to other caches. Here is an example:
使用模式与其他缓存类似。下面是一个例子:
Cache<String,String> cache = new Cache2kBuilder<String, String>() {}
.expireAfterWrite(5, TimeUnit.MINUTES) // expire/refresh after 5 minutes
.resilienceDuration(30, TimeUnit.SECONDS) // cope with at most 30 seconds
// outage before propagating
// exceptions
.refreshAhead(true) // keep fresh when expiring
.loader(new CacheLoader<String, String>() {
@Override
public String load(final String key) throws Exception {
return ....;
}
})
.build();
String val = cache.peek("something");
cache.put("something", "hello");
val = cache.get("something");
If you have google guava as dependency then trying out guava cache, may be a good alternative.
如果您将 google guava 作为依赖项,那么尝试使用 guava 缓存可能是一个不错的选择。
回答by yegor256
Try @Cacheable
from jcabi-aspects. With a single annotation you make the entire method result cacheable in memory:
@Cacheable
从jcabi-aspects尝试。使用单个注释,您可以将整个方法结果缓存在内存中:
public class Resource {
@Cacheable(lifetime = 5, unit = TimeUnit.SECONDS)
public String load(URL url) {
return url.openConnection().getContent();
}
}
Also, read this article: http://www.yegor256.com/2014/08/03/cacheable-java-annotation.html
另外,阅读这篇文章:http: //www.yegor256.com/2014/08/03/cacheable-java-annotation.html