Commit 03b93694 authored by amir.yosef's avatar amir.yosef

Update for runMaintenance functionality

parent 921ae604
......@@ -3,5 +3,5 @@ package command;
import java.io.IOException;
public interface CommandHandler {
byte [] execute( ) throws IOException;
byte[] execute() throws IOException;
}
......@@ -6,13 +6,13 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public class LRUCachePolicy<K, V> implements CachePolicy<K, V> {
private final int maxCapacity;
private final int threshold;
private final Map<K, V> cacheMap;
private final Queue<K> accessOrder;
public LRUCachePolicy(int maxCapacity) {
this.maxCapacity = maxCapacity;
public LRUCachePolicy(int maxCapacity, int threshold) {
this.cacheMap = new ConcurrentHashMap<>(maxCapacity);
this.threshold = threshold;
this.accessOrder = new ConcurrentLinkedQueue<>();
}
......@@ -39,7 +39,7 @@ public class LRUCachePolicy<K, V> implements CachePolicy<K, V> {
@Override
public void runMaintenance() {
while (cacheMap.size() > maxCapacity) {
while (cacheMap.entrySet().size() > threshold) {
evictLeastRecentlyUsed();
}
}
......
......@@ -9,12 +9,12 @@ public class Storage {
private final Map<String, Long> timeToExpiration = new ConcurrentHashMap<>(capacity);
private final Map<String, Long> currentTimeForKey = new ConcurrentHashMap<>(capacity);
private Storage() {
this.storage = new LRUCachePolicy<>(capacity);
private Storage(int threshold) {
this.storage = new LRUCachePolicy<>(capacity, threshold);
}
private static final class StorageHolder {
private static final Storage instance = new Storage();
private static final Storage instance = new Storage(8000);
}
public static Storage getInstance() {
......
......@@ -12,7 +12,7 @@ public class StorageManager {
public StorageManager() {
this.storage = Storage.getInstance();
this.scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(this::performMaintenance, 2, 2, TimeUnit.MINUTES);
scheduler.scheduleAtFixedRate(this::performMaintenance, 30, 30, TimeUnit.SECONDS);
}
private void performMaintenance() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment