轻量级 Java 对象缓存 API

提问

我正在寻找一个 Java 内存对象缓存 API。有什么建议吗? 你过去使用过什么解决方案?

现在

现在,我只是在用地图:

Map cache = new HashMap<String, Object>();
cache.put("key", value);

规定

我需要扩展高速缓存,使其包含以下基本特性:

  • 最大尺寸
  • 活下去的时间到了

然而,我不需要更复杂的功能,比如:

  • 从多个进程访问(缓存服务器)
  • 持久性(对磁盘)

建议

内存缓存:

  • Guava CacheBuilder-活动开发。
  • LRUMap -通过 API 进行配置。没有 TTL。不是专门用于缓存的。
  • 电子邮件缓存 -XML config. 邮件列表。最后更新于2006年。
  • Ache4j -XML config. 俄文文档。最后更新于2006年。

企业缓存:

  • JCS -Properties config. 大量文档。
  • Ehcache -XML config. 大量的文档。
77861 次浏览

EHCache is very nice. You can create an in memory cache. Check out their code samples for an example of creating an in memory cache. You can specify a max size, and a time to live.

EHCache does offer some advanced features, but if your not interested in using them - don't. But it's nice to know they are there if your requirements ever change.

Here is an in memory cache. Created in code, with no configuration files.

CacheManager cacheManager = CacheManager.getInstance();
int oneDay = 24 * 60 * 60;
Cache memoryOnlyCache = new Cache("name", 200, false, false, oneDay, oneDay);
cacheManager.addCache(memoryOnlyCache);

Creates a cache that will hold 200 elements, and has a ttl of 24 hours.

JCS is tried and true. Even though it is light as far as caching mechanisms go, you might dig into the actual code and mimic what they do with HashMap under the covers to exactly what you need and no more. You seem to have a pretty good idea of what you are looking for.

You can check out LinkedHashMap to implement a simple cache without third party jars:

    Map <String, Foo> cache = new LinkedHashMap<String, Foo>(MAX_ENTRIES + 1, .75F, true) {


public boolean removeEldestEntry(Map.Entry<String, Foo> eldest) {
return size() > MAX_ENTRIES;
}
};

then you can get from the cache like

    Foo foo = cache.get(key);
if (foo == null && !cache.containsKey(key)) {
try {
FooDAO fooDAO = DAOFactory.getFooDAO(conn);
foo = fooDAO.getFooByKey(key);
cache.put(key, foo);
} catch (SQLException sqle) {
logger.error("[getFoo] SQL Exception when accessing Foo", sqle);
}
}

rest left as exercise for reader :)

You can also check out my little cache library called KittyCache at:

https://github.com/treeder/kitty-cache

There are some performance benchmarks vs ehcache.

It's used in the SimpleJPA project as a second level cache.

I really like the MapMaker that comes with Google Guava (API)

The JavaDoc has a pretty neat example that demonstrates both its ease of use and its power:

ConcurrentMap<Key, Graph> graphs = new MapMaker()
.concurrencyLevel(32)
.softKeys()
.weakValues()
.expiration(30, TimeUnit.MINUTES)
.makeComputingMap(
new Function<Key, Graph>() {
public Graph apply(Key key) {
return createExpensiveGraph(key);
}
});

Furthermore, release 10.0 of Guava introduced the much more extensive com.google.common.cache package (there's a nice wiki entry on how to use them).

memcached has client for Java. http://www.danga.com/memcached/ Requires separate process to be a caching server, but powerful thing.

Guava's MapMaker has been replaced by their CacheBuilder class.