in sdk1/src/main/java/com/amazonaws/services/dynamodbv2/datamodeling/internal/TTLCache.java [98:129]
public T load(final String key, Function<String, T> f) {
final LockedState<T> ls = cache.get(key);
if (ls == null) {
// The entry doesn't exist yet, so load a new one.
return loadNewEntryIfAbsent(key, f);
} else if (clock.timestampNano() - ls.getState().lastUpdatedNano
> ttlInNanos + TTL_GRACE_IN_NANO) {
// The data has expired past the grace period.
// Evict the old entry and load a new entry.
cache.remove(key);
return loadNewEntryIfAbsent(key, f);
} else if (clock.timestampNano() - ls.getState().lastUpdatedNano <= ttlInNanos) {
// The data hasn't expired. Return as-is from the cache.
return ls.getState().data;
} else if (!ls.tryLock()) {
// We are in the TTL grace period. If we couldn't grab the lock, then some other
// thread is currently loading the new value. Because we are in the grace period,
// use the cached data instead of waiting for the lock.
return ls.getState().data;
}
// We are in the grace period and have acquired a lock.
// Update the cache with the value determined by the loading function.
try {
T loadedData = f.apply(key);
ls.update(loadedData, clock.timestampNano());
return ls.getState().data;
} finally {
ls.unlock();
}
}