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

Update for runMaintenance functionality

parent 921ae604
...@@ -3,5 +3,5 @@ package command; ...@@ -3,5 +3,5 @@ package command;
import java.io.IOException; import java.io.IOException;
public interface CommandHandler { public interface CommandHandler {
byte [] execute( ) throws IOException; byte[] execute() throws IOException;
} }
...@@ -6,13 +6,13 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -6,13 +6,13 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
public class LRUCachePolicy<K, V> implements CachePolicy<K, V> { 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 Map<K, V> cacheMap;
private final Queue<K> accessOrder; private final Queue<K> accessOrder;
public LRUCachePolicy(int maxCapacity) { public LRUCachePolicy(int maxCapacity, int threshold) {
this.maxCapacity = maxCapacity;
this.cacheMap = new ConcurrentHashMap<>(maxCapacity); this.cacheMap = new ConcurrentHashMap<>(maxCapacity);
this.threshold = threshold;
this.accessOrder = new ConcurrentLinkedQueue<>(); this.accessOrder = new ConcurrentLinkedQueue<>();
} }
...@@ -39,7 +39,7 @@ public class LRUCachePolicy<K, V> implements CachePolicy<K, V> { ...@@ -39,7 +39,7 @@ public class LRUCachePolicy<K, V> implements CachePolicy<K, V> {
@Override @Override
public void runMaintenance() { public void runMaintenance() {
while (cacheMap.size() > maxCapacity) { while (cacheMap.entrySet().size() > threshold) {
evictLeastRecentlyUsed(); evictLeastRecentlyUsed();
} }
} }
......
...@@ -9,12 +9,12 @@ public class Storage { ...@@ -9,12 +9,12 @@ public class Storage {
private final Map<String, Long> timeToExpiration = new ConcurrentHashMap<>(capacity); private final Map<String, Long> timeToExpiration = new ConcurrentHashMap<>(capacity);
private final Map<String, Long> currentTimeForKey = new ConcurrentHashMap<>(capacity); private final Map<String, Long> currentTimeForKey = new ConcurrentHashMap<>(capacity);
private Storage() { private Storage(int threshold) {
this.storage = new LRUCachePolicy<>(capacity); this.storage = new LRUCachePolicy<>(capacity, threshold);
} }
private static final class StorageHolder { private static final class StorageHolder {
private static final Storage instance = new Storage(); private static final Storage instance = new Storage(8000);
} }
public static Storage getInstance() { public static Storage getInstance() {
......
...@@ -12,7 +12,7 @@ public class StorageManager { ...@@ -12,7 +12,7 @@ public class StorageManager {
public StorageManager() { public StorageManager() {
this.storage = Storage.getInstance(); this.storage = Storage.getInstance();
this.scheduler = Executors.newSingleThreadScheduledExecutor(); this.scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(this::performMaintenance, 2, 2, TimeUnit.MINUTES); scheduler.scheduleAtFixedRate(this::performMaintenance, 30, 30, TimeUnit.SECONDS);
} }
private void performMaintenance() { 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