ConcurrentMap<Key, Graph> graphs = new MapMaker()
.weakKeys()
.maximumSize(10000)
.makeComputingMap(
new Function<Key, Graph>() {
public Graph apply(Key key) {
try { return createExpensiveGraph(key);
} catch (SomeCheckedException e) {
// now what
}
}
});
Cache<Key, Graph> graphs = CacheBuilder.newBuilder()
.weakKeys()
.maximumSize(10000)
.build(
new CacheLoader<Key, Graph>() {
public Graph load(Key key) throws SomeCheckedException {
return createExpensiveGraph(key);
}
});
Concurrent Caching at GoogleMonday, September 19th, 10:30-11:20am
Am 12.09.2011 19:16, schrieb Charles Fry:
> tl;dr: common.cache.CacheBuilder is better at making caches than MapMaker
[...]
> For example, we now expose detailed
> statistics<http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheStats.html>about
> cache performance.
CacheBuilder really looks cool, especially the statistics. Thanks!
However, one thing I wonder about (which also applies to MapMaker) is
whether I have a performance penalty compared to a simple HashMap when I
am in a strictly sequential environment (not even concurrent reads)?
Of course I would set concurrency level to 1, but as this allows
concurrent reads, there probably is still some synchronization.
Does anyone know about that or do I have to try it out?
And if there is indeed a performance penalty: Would there be a chance to
have support for a non-current map/cache from MapMaker/CacheBuilder?
Greetings, Philipp
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
Am 12.09.2011 21:28, schrieb Charles Fry:
> Right now we use ReentrantLock. In the future we are considering
> implementing concurrencyLevel(0), but we don't have any hard numbers about
> the potential gain. If all you want is a Map then I'd stick with HashMap. If
> what you want is a cache then I'd consider CacheBuilder.
Ok, so I think I'll use CacheBuilder after Guava 10 is released and see
if I can produce some numbers comparing it to a Map in my application.
Greetings, Philipp
--
Is there a Cache implementation off-heap, contents backed up by a file implementation in guava?
Very excited about the CacheBuilder - I'm glad Guava has identified
and supported MapMaker's caching demographic.
I'm wondering if it will be possible for entries to be kept strongly
reachable up until expiry, but then just be weak rather than evicted?
In this way instances uniquely identified by their key will not risk
being introduced into an environment twice if a long-running process
keeps an expired value from being garbage collected and it is
meanwhile reloaded elsewhere. See my SO question for more explanation
on the use-case and dilemma: http://stackoverflow.com/questions/6778743/my-ideal-cache-using-guava
Thanks for putting this together. We are already using this in
jclouds for caching expensive calls to EC2 for example. Seems like
the right path, and I especially like the exception design, as it will
likely remove several or more lines from stack traces from MapMaker.
A couple notes:
1. Currently, for we have images keyed on string. We'd like to
preseed the cache so that asMap().keySet() returns all available
images. Important is that we can get all images in a single command.
Seems a Map<K,V> seed() method in the CacheLoader would be nice for
things like adding in a bulk of stuff.
2. Would be handy to be able to specify a CacheLoader where load
returns Future<V>, similarly seed could return Map<K, Future<V>>. All
of our apis return futures, so this would be uber efficient and handy.
--