DiskbackedCache implements CacheStrategy<CacheKey, CacheValue>, Closeable { @Override public void put(CacheKey key, CacheValue value) { init(); defaultCache.put(key, value); try (PreparedStatement statement = dbConnection.prepareStatement(SQL_PUT_CACHE_VALUE)) { statement.setString(1, key.getGroupName()); statement.setString(2, key.getType().toString()); statement.setString(3, value.getHash()); statement.setString(4, value.getRawContent()); statement.execute(); } catch (SQLException e) { throw new WroRuntimeException("Error putting a value into the cache", e); } } DiskbackedCache(int lruSize, String dbPathString);   DiskbackedCache(int lruSize); @Override void put(CacheKey key, CacheValue value); @Override CacheValue get(CacheKey key); @Override void clear(); @Override void destroy(); @Override void close(); static final String NAME; static final String DB_PROP_KEY; static final String SQL_PUT_CACHE_VALUE; static final String SQL_GET_QUERY; }
@Test public void testPut() throws Exception { CacheKey key = new CacheKey("groupName", ResourceType.CSS); CacheValue value = CacheValue.valueOf("rawContent", "hash"); final URL classFile = DiskbackedCacheTest.class.getResource(DiskbackedCache.class.getSimpleName() + ".class"); final Path path = Paths.get(classFile.toURI()).getParent().resolve("db"); try (DirectoryStream<Path> paths = Files.newDirectoryStream(path.getParent(), "cache*.db")) { for (Path path1 : paths) { Files.deleteIfExists(path1); } } try (DiskbackedCache cache = new DiskbackedCache(100, path.toString())) { cache.put(key, value); CacheValue loaded = cache.get(key); assertEquals(value.getHash(), loaded.getHash()); assertEquals(value.getRawContent(), loaded.getRawContent()); assertArrayEquals(value.getGzippedContent(), loaded.getGzippedContent()); } try (DiskbackedCache cache2 = new DiskbackedCache(100, path.toString())) { CacheValue loaded = cache2.get(key); assertEquals(value.getHash(), loaded.getHash()); assertEquals(value.getRawContent(), loaded.getRawContent()); assertArrayEquals(value.getGzippedContent(), loaded.getGzippedContent()); } }