请参阅guava的CacheBuilder上的javadoc(它包含示例):
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheBuilder.html
这个: https://code.google.com/p/guava-libraries/wiki/CachesExplained
的 UPDATE 强> :没有必要使用缓存加载器,或者有一个加载方法。此测试用例通过:
@Test public void testManualInsertion() { Cache<String, String> cache = CacheBuilder.newBuilder() .maximumSize(1000) .expireAfterWrite(10, TimeUnit.MINUTES) .removalListener(new MyRemovalListener()) .concurrencyLevel(4) .recordStats() .build(); cache.put("someKey", "someValue"); assertEquals("someValue", cache.getIfPresent("someKey")); }