diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-07-30 13:46:10 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-07-31 12:04:26 +0200 |
commit | f0a1c302002d395f16bf12846817ec73df56e92d (patch) | |
tree | 0eebfcabb712777394a399d8dc3da65e6aa8dfeb /sonar-home | |
parent | c003fa9f7648ee31a963171683c29f6d6313c646 (diff) | |
download | sonarqube-f0a1c302002d395f16bf12846817ec73df56e92d.tar.gz sonarqube-f0a1c302002d395f16bf12846817ec73df56e92d.zip |
SONAR-6723 Use SQ version in cache keys
Diffstat (limited to 'sonar-home')
3 files changed, 36 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 058075f83ed..640d5c29401 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 @@ -31,6 +31,7 @@ import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; + import javax.annotation.CheckForNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -50,11 +51,13 @@ public class PersistentCache { // eviction strategy is to expire entries after modification once a time duration has elapsed private final long defaultDurationToExpireMs; private final Logger logger; + private final String version; - public PersistentCache(Path baseDir, long defaultDurationToExpireMs, Logger logger) { + public PersistentCache(Path baseDir, long defaultDurationToExpireMs, Logger logger, String version) { this.baseDir = baseDir; this.defaultDurationToExpireMs = defaultDurationToExpireMs; this.logger = logger; + this.version = version; reconfigure(); logger.debug("cache: " + baseDir + ", default expiration time (ms): " + defaultDurationToExpireMs); @@ -200,10 +203,14 @@ public class PersistentCache { lockChannel = null; } - private static String getKey(String uri) { + private String getKey(String uri) { try { + String key = uri; + if (version != null) { + key += version; + } MessageDigest digest = MessageDigest.getInstance(DIGEST_ALGO); - digest.update(uri.getBytes(StandardCharsets.UTF_8)); + digest.update(key.getBytes(StandardCharsets.UTF_8)); return byteArrayToHex(digest.digest()); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Couldn't create hash", e); 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 f0fa1596917..055d2615ed6 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,6 +30,7 @@ public class PersistentCacheBuilder { private Path cachePath; private final Logger logger; + private String version; public PersistentCacheBuilder(Logger logger) { this.logger = logger; @@ -40,7 +41,12 @@ public class PersistentCacheBuilder { setSonarHome(findHome()); } - return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, logger); + return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, logger, version); + } + + public PersistentCacheBuilder setVersion(String version) { + this.version = version; + return this; } public PersistentCacheBuilder setSonarHome(@Nullable Path p) { 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 62e105bf861..c327ff7c5eb 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 @@ -41,7 +41,7 @@ public class PersistentCacheTest { @Before public void setUp() { - cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class)); + cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null); } @Test @@ -79,7 +79,7 @@ public class PersistentCacheTest { @Test public void testReconfigure() throws Exception { - cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class)); + cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null); assertCacheHit(false); assertCacheHit(true); @@ -97,14 +97,29 @@ public class PersistentCacheTest { @Test public void testExpiration() throws Exception { // negative time to make sure it is expired on the second call - cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class)); + cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), null); assertCacheHit(false); assertCacheHit(false); } + + @Test + public void testDifferentServerVersions() throws Exception { + assertCacheHit(false); + assertCacheHit(true); + + PersistentCache cache2 = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), "5.2"); + assertCacheHit(cache2, false); + assertCacheHit(cache2, true); + + } private void assertCacheHit(boolean hit) throws Exception { + assertCacheHit(cache, hit); + } + + private void assertCacheHit(PersistentCache pCache, boolean hit) throws Exception { CacheFillerString c = new CacheFillerString(); - assertThat(cache.getString(URI, c)).isEqualTo(VALUE); + assertThat(pCache.getString(URI, c)).isEqualTo(VALUE); assertThat(c.wasCalled).isEqualTo(!hit); } |