Commit 1d9fb337 authored by amir.yosef's avatar amir.yosef

Unstable Storage management

parent 5a79f2a4
<component name="libraryTable">
<library name="io.quarkus.caffeine" type="repository">
<properties maven-id="io.quarkus:quarkus-caffeine:2.16.5.Final" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-caffeine/2.16.5.Final/quarkus-caffeine-2.16.5.Final.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-core/2.16.5.Final/quarkus-core-2.16.5.Final.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/enterprise/jakarta.enterprise.cdi-api/2.0.2/jakarta.enterprise.cdi-api-2.0.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/el/jakarta.el-api/3.0.3/jakarta.el-api-3.0.3.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/interceptor/jakarta.interceptor-api/1.2.5/jakarta.interceptor-api-1.2.5.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/ejb/jakarta.ejb-api/3.2.6/jakarta.ejb-api-3.2.6.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/transaction/jakarta.transaction-api/1.3.2/jakarta.transaction-api-1.3.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/inject/jakarta.inject-api/1.0/jakarta.inject-api-1.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-ide-launcher/2.16.5.Final/quarkus-ide-launcher-2.16.5.Final.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-development-mode-spi/2.16.5.Final/quarkus-development-mode-spi-2.16.5.Final.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/config/smallrye-config/2.13.3/smallrye-config-2.13.3.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/config/smallrye-config-core/2.13.3/smallrye-config-core-2.13.3.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/eclipse/microprofile/config/microprofile-config-api/2.0.1/microprofile-config-api-2.0.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-annotation/1.13.2/smallrye-common-annotation-1.13.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-expression/1.13.2/smallrye-common-expression-1.13.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-function/1.13.2/smallrye-common-function-1.13.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-constraint/1.13.2/smallrye-common-constraint-1.13.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-classloader/1.13.2/smallrye-common-classloader-1.13.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/ow2/asm/asm/9.4/asm-9.4.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/config/smallrye-config-common/2.13.3/smallrye-config-common-2.13.3.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jboss/logging/jboss-logging/3.5.0.Final/jboss-logging-3.5.0.Final.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jboss/logmanager/jboss-logmanager-embedded/1.0.11/jboss-logmanager-embedded-1.0.11.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jboss/logging/jboss-logging-annotations/2.2.1.Final/jboss-logging-annotations-2.2.1.Final.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jboss/threads/jboss-threads/3.4.3.Final/jboss-threads-3.4.3.Final.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jboss/slf4j/slf4j-jboss-logmanager/1.2.0.Final/slf4j-jboss-logmanager-1.2.0.Final.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/graalvm/sdk/graal-sdk/22.3.0/graal-sdk-22.3.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/wildfly/common/wildfly-common/1.5.4.Final-format-001/wildfly-common-1.5.4.Final-format-001.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-bootstrap-runner/2.16.5.Final/quarkus-bootstrap-runner-2.16.5.Final.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-io/1.13.2/smallrye-common-io-1.13.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/github/crac/org-crac/0.1.3/org-crac-0.1.3.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-fs-util/0.0.9/quarkus-fs-util-0.0.9.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/github/ben-manes/caffeine/caffeine/3.1.1/caffeine-3.1.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/google/errorprone/error_prone_annotations/2.14.0/error_prone_annotations-2.14.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-caffeine/2.16.5.Final/quarkus-caffeine-2.16.5.Final-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-core/2.16.5.Final/quarkus-core-2.16.5.Final-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/enterprise/jakarta.enterprise.cdi-api/2.0.2/jakarta.enterprise.cdi-api-2.0.2-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/el/jakarta.el-api/3.0.3/jakarta.el-api-3.0.3-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/interceptor/jakarta.interceptor-api/1.2.5/jakarta.interceptor-api-1.2.5-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/ejb/jakarta.ejb-api/3.2.6/jakarta.ejb-api-3.2.6-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/transaction/jakarta.transaction-api/1.3.2/jakarta.transaction-api-1.3.2-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/jakarta/inject/jakarta.inject-api/1.0/jakarta.inject-api-1.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-ide-launcher/2.16.5.Final/quarkus-ide-launcher-2.16.5.Final-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-development-mode-spi/2.16.5.Final/quarkus-development-mode-spi-2.16.5.Final-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/config/smallrye-config/2.13.3/smallrye-config-2.13.3-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/config/smallrye-config-core/2.13.3/smallrye-config-core-2.13.3-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/eclipse/microprofile/config/microprofile-config-api/2.0.1/microprofile-config-api-2.0.1-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-annotation/1.13.2/smallrye-common-annotation-1.13.2-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-expression/1.13.2/smallrye-common-expression-1.13.2-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-function/1.13.2/smallrye-common-function-1.13.2-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-constraint/1.13.2/smallrye-common-constraint-1.13.2-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-classloader/1.13.2/smallrye-common-classloader-1.13.2-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/ow2/asm/asm/9.4/asm-9.4-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/config/smallrye-config-common/2.13.3/smallrye-config-common-2.13.3-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jboss/logging/jboss-logging/3.5.0.Final/jboss-logging-3.5.0.Final-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jboss/logmanager/jboss-logmanager-embedded/1.0.11/jboss-logmanager-embedded-1.0.11-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jboss/logging/jboss-logging-annotations/2.2.1.Final/jboss-logging-annotations-2.2.1.Final-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jboss/threads/jboss-threads/3.4.3.Final/jboss-threads-3.4.3.Final-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jboss/slf4j/slf4j-jboss-logmanager/1.2.0.Final/slf4j-jboss-logmanager-1.2.0.Final-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/graalvm/sdk/graal-sdk/22.3.0/graal-sdk-22.3.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/wildfly/common/wildfly-common/1.5.4.Final-format-001/wildfly-common-1.5.4.Final-format-001-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-bootstrap-runner/2.16.5.Final/quarkus-bootstrap-runner-2.16.5.Final-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/smallrye/common/smallrye-common-io/1.13.2/smallrye-common-io-1.13.2-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/github/crac/org-crac/0.1.3/org-crac-0.1.3-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/io/quarkus/quarkus-fs-util/0.0.9/quarkus-fs-util-0.0.9-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/github/ben-manes/caffeine/caffeine/3.1.1/caffeine-3.1.1-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/google/errorprone/error_prone_annotations/2.14.0/error_prone_annotations-2.14.0-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
...@@ -27,5 +27,6 @@ ...@@ -27,5 +27,6 @@
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="library" name="io.quarkus.junit5.mockito" level="project" /> <orderEntry type="library" name="io.quarkus.junit5.mockito" level="project" />
<orderEntry type="library" name="io.quarkus.caffeine" level="project" />
</component> </component>
</module> </module>
\ No newline at end of file
package storage;
public interface Cacheable<K, V> {
void save(K key, V value);
void save(K key, V value, Long expirationTime);
V get(K key);
void delete(K key);
}
package storage;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalCause;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
public class CaffeineCachePolicy<K, V> implements CachePolicy<K, V> {
private final Cache<K, V> cache;
private static final Logger logger = Logger.getLogger(CaffeineCachePolicy.class.getName());
public CaffeineCachePolicy(int maxCapacity) {
this.cache = Caffeine.newBuilder()
.maximumSize(maxCapacity)
.expireAfterAccess(60, TimeUnit.MINUTES)
.evictionListener((K key, V value, RemovalCause cause) ->
logger.info("Evicted key: " + key + ", Value: " + value + ", Reason: " + cause))
.build();
}
@Override
public void add(K key, V value) {
cache.put(key, value);
}
@Override
public V retrieve(K key) {
return cache.getIfPresent(key);
}
@Override
public void delete(K key) {
cache.invalidate(key);
}
@Override
public void runMaintenance() {
cache.cleanUp();
}
}
...@@ -227,4 +227,4 @@ public class LRUCachePolicy<K, V> implements CachePolicy<K, V> { ...@@ -227,4 +227,4 @@ public class LRUCachePolicy<K, V> implements CachePolicy<K, V> {
this.value = value; this.value = value;
} }
} }
} }
\ No newline at end of file
...@@ -2,24 +2,22 @@ package storage; ...@@ -2,24 +2,22 @@ package storage;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
public class Storage { public class Storage {
private final int capacity = 1000000; private final int capacity = 1000000;
private static final Logger logger = Logger.getLogger(Storage.class.getName());
private final CachePolicy<String, String> storage; private final CachePolicy<String, String> 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(int threshold) { private Storage() {
this.storage = new LRUCachePolicy<>(capacity, threshold); this.storage = new CaffeineCachePolicy<>(capacity);
RdbFileReader<String, String> reader = new RdbFileReader<>(); RdbFileReader<String, String> reader = new RdbFileReader<>();
Map<String, Long> stringStringMap = reader.getKeysExpiration(); Map<String, Long> stringStringMap = reader.getKeysExpiration();
this.timeToExpiration.putAll(stringStringMap); this.timeToExpiration.putAll(stringStringMap);
} }
private static final class StorageHolder { private static final class StorageHolder {
private static final Storage instance = new Storage(800000); private static final Storage instance = new Storage();
} }
public static Storage getInstance() { public static Storage getInstance() {
...@@ -45,14 +43,6 @@ public class Storage { ...@@ -45,14 +43,6 @@ public class Storage {
} }
private boolean isExpired(String key) { private boolean isExpired(String key) {
if (timeToExpiration.containsKey(key) && !currentTimeForKey.containsKey(key)) {
long currentTime = System.currentTimeMillis();
long expirationTime = timeToExpiration.get(key);
if (currentTime > expirationTime) {
logger.info("Key expired: " + key);
return true;
}
}
if (currentTimeForKey.containsKey(key)) { if (currentTimeForKey.containsKey(key)) {
long currTime = System.currentTimeMillis(); long currTime = System.currentTimeMillis();
long expirationTime = timeToExpiration.get(key); long expirationTime = timeToExpiration.get(key);
......
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