mkodekar / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Cache try to refresh value on getIfPresent(key) and asMap().get(key) #1436

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Currently if entry in Cache is stale (i.e. now - entry.getWriteTime() > 
map.refreshNanos)
cache.getIfPresent(key) and cache.asMap().get(key) will try to refresh a value.
It may cause problems if CacheLoader isn't implemented to be asynchronous and 
contradict with:

http://code.google.com/p/guava-libraries/wiki/CachesExplained#asMap
- asMap().get(key) is essentially equivalent to cache.getIfPresent(key), and 
never causes values to be loaded. This is consistent with the Map contract.

final AtomicLong now = new AtomicLong(0);
Ticker ticker = new Ticker() {
    public long read() {
        return now.get();
    }
};
final AtomicLong countdown = new AtomicLong(1);
LoadingCache<String, String> cache = CacheBuilder.newBuilder()
    .refreshAfterWrite(1, TimeUnit.HOURS)
    .ticker(ticker)
    .build(new CacheLoader<String, String>() {
        public String load(String key) throws Exception {
            if (countdown.getAndDecrement() == 0) {
                throw new Exception("Bomb in log!");
            }
            return key;
        }
    });

String nullStr = cache.getIfPresent("1"); // OK
String oneStr = cache.get("1");
now.set(2 * TimeUnit.HOURS.toNanos(1));
String oneBomb = cache.getIfPresent("1"); // Bomb in log!

Original issue reported on code.google.com by jekam...@gmail.com on 31 May 2013 at 3:12

GoogleCodeExporter commented 9 years ago

Original comment by kurt.kluever on 31 May 2013 at 3:52

GoogleCodeExporter commented 9 years ago
This behavior seems to be working as intended in the sense that it is actually 
explicitly tested: 
https://code.google.com/p/guava-libraries/source/browse/guava-tests/test/com/goo
gle/common/cache/CacheLoadingTest.java#268

There seem to be two questions, then: is this the behavior we want, and if so, 
what docs need to be clarified?

Original comment by lowas...@google.com on 8 Aug 2013 at 11:00

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08