Skip to content

Drozdov Igor part2 #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/main/java/part2/cache/CachingDataStorageImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package part2.cache;

import com.sun.javafx.geom.AreaOp;
import db.DataStorage;
import db.SlowCompletableFutureDb;

Expand Down Expand Up @@ -38,6 +39,30 @@ public OutdatableResult<T> getOutdatable(String key) {
// TODO don't use obtrudeException on result - just don't
// TODO use remove(Object key, Object value) to remove target value
// TODO Start timeout after receiving result in CompletableFuture, not after receiving CompletableFuture itself
throw new UnsupportedOperationException();

OutdatableResult<T> res = new OutdatableResult<>(new CompletableFuture<>(), new CompletableFuture<>());
OutdatableResult<T> tOutdatableResult = cache.putIfAbsent(key, res);

if (tOutdatableResult == null) {
db.get(key).whenComplete(
(t, e) -> {
if (e != null) {
res.getResult().completeExceptionally(e);
} else {
res.getResult().complete(t);
}
scheduledExecutorService.schedule(() -> {
cache.remove(key, cache.get(key));
res.getOutdated().complete(null);
},
timeout,
timeoutUnits
);
}
);
return res;
} else {
return tOutdatableResult;
}
}
}