Skip to content
Open
Show file tree
Hide file tree
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
41 changes: 31 additions & 10 deletions src/main/java/part2/cache/CachingDataStorageImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package part2.cache;

import db.DataStorage;
import db.SlowCompletableFutureDb;

import java.util.concurrent.*;

Expand All @@ -10,7 +9,7 @@ public class CachingDataStorageImpl<T> implements CachingDataStorage<String, T>
private final DataStorage<String, T> db;
private final int timeout;
private final TimeUnit timeoutUnits;
// TODO can we use Map<String, T> here? Why?
// TODO can we use Map<String, T> here? Why? - cause we write for a multithreading app
private final ConcurrentMap<String, OutdatableResult<T>> cache = new ConcurrentHashMap<>();
private final ScheduledExecutorService scheduledExecutorService =
Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
Expand All @@ -32,12 +31,34 @@ public CachingDataStorageImpl(DataStorage<String, T> db, int timeout, TimeUnit t

@Override
public OutdatableResult<T> getOutdatable(String key) {
// TODO implement
// TODO use ScheduledExecutorService to remove outdated result from cache - see SlowCompletableFutureDb implementation
// TODO complete OutdatableResult::outdated after removing outdated result from cache
// 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();
final OutdatableResult<T> result =
new OutdatableResult<>(new CompletableFuture<>(), new CompletableFuture<>());
final OutdatableResult<T> previous = cache.putIfAbsent(key, result);

if (previous != null) {
return previous;
}

db.get(key).whenComplete((value, throwable) -> {

if (throwable != null) {
result.getResult().completeExceptionally(throwable);
} else {
result.getResult().complete(value);
}

scheduledExecutorService.schedule(
() -> {
cache.remove(key, result);
result.getOutdated().complete(null);
},
timeout,
timeoutUnits
);

});

return result;
}
}

}
62 changes: 61 additions & 1 deletion src/test/java/part2/cache/CachingDataStorageImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void after() {
}

@Test
public void expiration() throws InterruptedException, ExecutionException, TimeoutException {
public void expirationEmployee() throws InterruptedException, ExecutionException, TimeoutException {
final CachingDataStorageImpl<Employee> employeeCache =
new CachingDataStorageImpl<>(employeeDb, 100, TimeUnit.MILLISECONDS);

Expand Down Expand Up @@ -84,4 +84,64 @@ public void expiration() throws InterruptedException, ExecutionException, Timeou
assertEquals(person2, result3.getResult().get().getPerson());
}

@Test
public void expirationEmployer() throws InterruptedException, ExecutionException, TimeoutException {
final CachingDataStorageImpl<Employer> employerCache =
new CachingDataStorageImpl<>(employerDb, 100, TimeUnit.MILLISECONDS);

Map<String, Employer> values = new HashMap<>();
Employer google = Employer.Google;
values.put("a", google);
employerDb.setValues(values);

final OutdatableResult<Employer> result1 = employerCache.getOutdatable("a");

values = new HashMap<>();
Employer yandex = Employer.Yandex;
values.put("a", yandex);
employerDb.setValues(values);

Thread.sleep(10);
final OutdatableResult<Employer> result2 = employerCache.getOutdatable("a");

assertEquals(google, result1.getResult().get());
assertEquals(result1.getResult().get(), result2.getResult().get());

result1.getOutdated().get(100, TimeUnit.MILLISECONDS);

OutdatableResult<Employer> result3 = employerCache.getOutdatable("a");

assertEquals(yandex, result3.getResult().get());
}

@Test
public void expirationPosition() throws InterruptedException, ExecutionException, TimeoutException {
final CachingDataStorageImpl<Position> employerCache =
new CachingDataStorageImpl<>(positionDb, 100, TimeUnit.MILLISECONDS);

Map<String, Position> values = new HashMap<>();
Position dev = Position.DEV;
values.put("a", dev);
positionDb.setValues(values);

final OutdatableResult<Position> result1 = employerCache.getOutdatable("a");

values = new HashMap<>();
Position devOps = Position.DevOps;
values.put("a", devOps);
positionDb.setValues(values);

Thread.sleep(10);
final OutdatableResult<Position> result2 = employerCache.getOutdatable("a");

assertEquals(dev, result1.getResult().get());
assertEquals(result1.getResult().get(), result2.getResult().get());

result1.getOutdated().get(100, TimeUnit.MILLISECONDS);

OutdatableResult<Position> result3 = employerCache.getOutdatable("a");

assertEquals(devOps, result3.getResult().get());
}

}