diff --git a/README.md b/README.md index 15448829..c0bdc05e 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ For Windows Command Prompt, the syntax is slightly different: set SPRING_APPLICATION_JSON={"spring.datasource.driver-class-name":"org.postgresql.Driver","spring.datasource.url":"jdbc:postgresql://localhost:5432/scholars","spring.jpa.database-platform":"org.hibernate.dialect.PostgreSQLDialect","spring.sql.init.platform":"postgres"} && mvn spring-boot:run ``` -Run with schematize and indexing turned on: +Run with schematize and indexing turned on (default): ``` set SPRING_APPLICATION_JSON={"spring.datasource.driver-class-name":"org.postgresql.Driver","spring.datasource.url":"jdbc:postgresql://localhost:5432/scholars","spring.jpa.database-platform":"org.hibernate.dialect.PostgreSQLDialect","spring.sql.init.platform":"postgres","middleware.index.schematize":true,"middleware.index.onStartup":true} && mvn spring-boot:run ``` @@ -156,6 +156,11 @@ Run with schematize and indexing turned off and cache enabled: set SPRING_APPLICATION_JSON={"spring.datasource.driver-class-name":"org.postgresql.Driver","spring.datasource.url":"jdbc:postgresql://localhost:5432/scholars","spring.jpa.database-platform":"org.hibernate.dialect.PostgreSQLDialect","spring.sql.init.platform":"postgres","middleware.index.schematize":false,"middleware.index.onStartup":false,"middleware.index.cacheEnabled":true} && mvn spring-boot:run ``` +Run with schematize and indexing turned off and cache enabled read-only: +``` +set SPRING_APPLICATION_JSON={"spring.datasource.driver-class-name":"org.postgresql.Driver","spring.datasource.url":"jdbc:postgresql://localhost:5432/scholars","spring.jpa.database-platform":"org.hibernate.dialect.PostgreSQLDialect","spring.sql.init.platform":"postgres","middleware.index.schematize":false,"middleware.index.onStartup":false,"middleware.index.cacheEnabled":true,"middleware.index.cacheReadOnly":true} && mvn spring-boot:run +``` + For Windows PowerShell: ``` diff --git a/src/main/java/edu/tamu/scholars/middleware/config/model/IndexConfig.java b/src/main/java/edu/tamu/scholars/middleware/config/model/IndexConfig.java index a03f5c88..01896ff9 100644 --- a/src/main/java/edu/tamu/scholars/middleware/config/model/IndexConfig.java +++ b/src/main/java/edu/tamu/scholars/middleware/config/model/IndexConfig.java @@ -30,6 +30,8 @@ public class IndexConfig { private boolean cacheEnabled = false; + private boolean cacheReadOnly = false; + private boolean clearCache = true; private String cacheLocation = "src/test/resources"; @@ -106,6 +108,14 @@ public void setCacheEnabled(boolean cacheEnabled) { this.cacheEnabled = cacheEnabled; } + public boolean isCacheReadOnly() { + return cacheReadOnly; + } + + public void setCacheReadOnly(boolean cacheReadOnly) { + this.cacheReadOnly = cacheReadOnly; + } + public boolean isClearCache() { return clearCache; } diff --git a/src/main/java/edu/tamu/scholars/middleware/service/CachingSolrClient.java b/src/main/java/edu/tamu/scholars/middleware/service/CachingSolrClient.java index b1da7826..e79cfdab 100644 --- a/src/main/java/edu/tamu/scholars/middleware/service/CachingSolrClient.java +++ b/src/main/java/edu/tamu/scholars/middleware/service/CachingSolrClient.java @@ -6,17 +6,13 @@ import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.exc.StreamReadException; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.DatabindException; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -37,6 +33,26 @@ import edu.tamu.scholars.middleware.service.builder.ClaimBuilder; import edu.tamu.scholars.middleware.utility.JavaObjectStorageFileUtility; +/** + * SorlClient cache. + * + * Configure with {@link IndexConfig}. + * + * To enable the cache: + * middleware.index.cacheEnabled: false + * + * To only allow to read from cache: + * middleware.index.cacheReadOnly: false + * + * To clear the cache on startup: + * middleware.index.clearCache: true + * + * Location to store cache: + * middleware.index.cacheLocation: src/test/resources + * + * The cache storage is per API endpoint mapping. + * + */ public class CachingSolrClient extends SolrClient { private static final Logger logger = LoggerFactory.getLogger(CachingSolrClient.class); @@ -85,7 +101,7 @@ public CachingSolrClient(C client, JwtTokenService jwtTokenService, IndexConfig @PostConstruct public void clearCache() { - if (index.isCacheEnabled()) { + if (index.isCacheEnabled() && !index.isCacheReadOnly()) { File cacheDirectory = new File(StringUtils.removeEnd(index.getCacheLocation(), FORWARD_SLASH)); if (cacheDirectory.exists() && !cacheDirectory.isDirectory()) { throw new RuntimeException(String.format("Cache location %s is not a directory!", index.getCacheLocation())); @@ -186,18 +202,20 @@ public NamedList request(SolrRequest request, String collection) long startTime = System.currentTimeMillis(); response = client.request(request, collection); - JavaObjectStorageFileUtility.writeObject(response, filename); + if (index.isCacheReadOnly()) { + JavaObjectStorageFileUtility.writeObject(response, filename); - logger.info("{}:{}: {} seconds", uuid, "QUERY RESPONSE", (System.currentTimeMillis() - startTime) / (double) 1000); + logger.info("{}:{}: {} seconds", uuid, "QUERY RESPONSE", (System.currentTimeMillis() - startTime) / (double) 1000); - long startUdateLookupTable = System.currentTimeMillis(); + long startUdateLookupTable = System.currentTimeMillis(); - innerMap.put(jwt, uuid); - objectMapper.writerWithDefaultPrettyPrinter().writeValue(lookupFile, innerMap); + innerMap.put(jwt, uuid); + objectMapper.writerWithDefaultPrettyPrinter().writeValue(lookupFile, innerMap); - logger.info("{}:{}: {} seconds", uuid, "UPDATE LOOKUP TABLE", (System.currentTimeMillis() - startUdateLookupTable) / (double) 1000); + logger.info("{}:{}: {} seconds", uuid, "UPDATE LOOKUP TABLE", (System.currentTimeMillis() - startUdateLookupTable) / (double) 1000); - logger.info("{}:{}:{} {}", uuid, "REQUEST", uuid, requestAsObject.toPrettyString()); + logger.info("{}:{}:{} {}", uuid, "REQUEST", uuid, requestAsObject.toPrettyString()); + } } logger.info("{}:{}: {} seconds", uuid, "ACTUAL RESPONSE", (System.currentTimeMillis() - start) / (double) 1000); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e1f21efe..58826406 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -38,6 +38,7 @@ middleware: onStartupDelay: 10000 batchSize: 10000 cacheEnabled: false + cacheReadOnly: false clearCache: true cacheLocation: src/test/resources triplestore: diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 6b968c7a..562d259d 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -34,6 +34,7 @@ middleware: enableIndividualOnBatchFail: false batchSize: 1000 cacheEnabled: false + cacheReadOnly: false clearCache: true cacheLocation: src/test/resources triplestore: