/** * Clears the cache. */ public void clearCache() { if (cacheFile != null) { cacheFile.reset(); } }
/** * Load cached values from file. * @throws IOException when there is a problems with file read */ public void load() throws IOException { // get the current config so if the file isn't found // the first time the hash will be added to output file configHash = getHashCodeBasedOnObjectContent(config); final File file = new File(fileName); if (file.exists()) { try (InputStream inStream = Files.newInputStream(file.toPath())) { details.load(inStream); final String cachedConfigHash = details.getProperty(CONFIG_HASH_KEY); if (!configHash.equals(cachedConfigHash)) { // Detected configuration change - clear cache reset(); } } } else { // put the hash in the file if the file is going to be created reset(); } }
/** * Puts external resources in cache. * If at least one external resource changed, clears the cache. * @param locations locations of external resources. */ public void putExternalResources(Set<String> locations) { final Set<ExternalResource> resources = loadExternalResources(locations); if (areExternalResourcesChanged(resources)) { reset(); fillCacheWithExternalResources(resources); } }
@Test public void testConfigHashOnReset() throws IOException { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); cache.load(); final String hash = cache.get(PropertyCacheFile.CONFIG_HASH_KEY); assertNotNull("Config hash key should not be null", hash); cache.reset(); assertEquals("Invalid config hash key", hash, cache.get(PropertyCacheFile.CONFIG_HASH_KEY)); }
/** * Clears the cache. */ public void clearCache() { if (cacheFile != null) { cacheFile.reset(); } }
/** * Load cached values from file. * @throws IOException when there is a problems with file read */ public void load() throws IOException { // get the current config so if the file isn't found // the first time the hash will be added to output file configHash = getHashCodeBasedOnObjectContent(config); final File file = new File(fileName); if (file.exists()) { try (InputStream inStream = Files.newInputStream(file.toPath())) { details.load(inStream); final String cachedConfigHash = details.getProperty(CONFIG_HASH_KEY); if (!configHash.equals(cachedConfigHash)) { // Detected configuration change - clear cache reset(); } } } else { // put the hash in the file if the file is going to be created reset(); } }
/** * Puts external resources in cache. * If at least one external resource changed, clears the cache. * @param locations locations of external resources. */ public void putExternalResources(Set<String> locations) { final Set<ExternalResource> resources = loadExternalResources(locations); if (areExternalResourcesChanged(resources)) { reset(); fillCacheWithExternalResources(resources); } }