diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-06-18 15:23:54 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-06-23 15:15:25 +0200 |
commit | d2b146706202047d71eb4e7a6a6a57eb307d25d8 (patch) | |
tree | 7c75002d03eca9dc2184a0a8e2e7d18fde51622d /sonar-home | |
parent | 792d7eb799efc25d92adcf059acc4c7c9f67d2c7 (diff) | |
download | sonarqube-d2b146706202047d71eb4e7a6a6a57eb307d25d8.tar.gz sonarqube-d2b146706202047d71eb4e7a6a6a57eb307d25d8.zip |
SONAR-6649 Move initialization of persistit caches to global context
Diffstat (limited to 'sonar-home')
3 files changed, 32 insertions, 8 deletions
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java index a5d88ffcbe9..3ceeaa47f3d 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java @@ -52,15 +52,19 @@ public class PersistentCache { // eviction strategy is to expire entries after modification once a time duration has elapsed private final long defaultDurationToExpireMs; private final Log log; - private final boolean forceUpdate; + private boolean forceUpdate; public PersistentCache(Path baseDir, long defaultDurationToExpireMs, Log log, boolean forceUpdate) { this.baseDir = baseDir; this.defaultDurationToExpireMs = defaultDurationToExpireMs; this.log = log; - this.forceUpdate = forceUpdate; + reconfigure(forceUpdate); log.info("cache: " + baseDir + ", default expiration time (ms): " + defaultDurationToExpireMs); + } + + public void reconfigure(boolean forceUpdate) { + this.forceUpdate = forceUpdate; if (forceUpdate) { log.debug("cache: forcing update"); diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java index c8fcf06d4d0..c58dc53bbd6 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java @@ -30,17 +30,19 @@ import java.nio.file.Paths; import java.util.concurrent.TimeUnit; public class PersistentCacheBuilder { + private static final long DEFAULT_EXPIRE_DURATION = TimeUnit.MILLISECONDS.convert(1L, TimeUnit.DAYS); + private static final String DIR_NAME = "ws_cache"; + private boolean forceUpdate = false; private Path cachePath = null; private Log log = new StandardLog(); - private String name = "ws_cache"; public PersistentCache build() { if (cachePath == null) { setSonarHome(findHome()); } - return new PersistentCache(cachePath, TimeUnit.MILLISECONDS.convert(1L, TimeUnit.DAYS), log, forceUpdate); + return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, log, forceUpdate); } public PersistentCacheBuilder setLog(Log log) { @@ -50,7 +52,7 @@ public class PersistentCacheBuilder { public PersistentCacheBuilder setSonarHome(@Nullable Path p) { if (p != null) { - this.cachePath = p.resolve(name); + this.cachePath = p.resolve(DIR_NAME); } return this; } diff --git a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java index 5f1e3424642..8f8596e0705 100644 --- a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java +++ b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java @@ -19,12 +19,13 @@ */ package org.sonar.home.cache; +import org.apache.commons.io.FileUtils; + import org.sonar.home.log.Slf4jLog; import org.junit.Rule; import org.junit.rules.TemporaryFolder; import java.io.File; -import java.nio.file.Path; import java.util.concurrent.Callable; import static org.mockito.Mockito.when; @@ -96,8 +97,25 @@ public class PersistentCacheTest { } @Test + public void testReconfigure() throws Exception { + cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, log, true); + assertCacheHit(false); + assertCacheHit(false); + + File root = tmp.getRoot(); + FileUtils.deleteDirectory(root); + + // should re-create cache directory and start using the cache + cache.reconfigure(false); + assertThat(root).exists(); + + assertCacheHit(false); + assertCacheHit(true); + } + + @Test public void testExpiration() throws Exception { - //negative time to make sure it is expired on the second call + // negative time to make sure it is expired on the second call cache = new PersistentCache(tmp.getRoot().toPath(), -100, log, false); assertCacheHit(false); assertCacheHit(false); @@ -118,7 +136,7 @@ public class PersistentCacheTest { return VALUE; } } - + /** * WSCache should be transparent regarding exceptions: if an exception is thrown by the value loader, it should pass through * the cache to the original caller using the cache. |