int maxCacheAge) {
if (collection != Collection.NODES) {
return findUncached(collection, key);
}
CacheValue cacheKey = new StringValue(key);
NodeDocument doc;
if (maxCacheAge > 0) {
// first try without lock
doc = nodesCache.getIfPresent(cacheKey);
if (doc != null) {
if (maxCacheAge == Integer.MAX_VALUE ||
System.currentTimeMillis() - doc.getCreated() < maxCacheAge) {
if (doc == NodeDocument.NULL) {
return null;
}
return (T) doc;
}
}
}
try {
Lock lock = getAndLock(key);
try {
if (maxCacheAge == 0) {
invalidateCache(collection, key);
}
while (true) {
doc = nodesCache.get(cacheKey, new Callable<NodeDocument>() {
@Override
public NodeDocument call() throws Exception {
NodeDocument doc = (NodeDocument) findUncached(collection, key);
if (doc == null) {
doc = NodeDocument.NULL;
}
return doc;
}
});
if (maxCacheAge == 0 || maxCacheAge == Integer.MAX_VALUE) {
break;
}
if (System.currentTimeMillis() - doc.getCreated() < maxCacheAge) {
break;
}
// too old: invalidate, try again
invalidateCache(collection, key);
}