Пример #1
0
 @Override
 public void remove(String contentUrl) {
   // Remove from the in-memory cache, but not from disk. Let the clean-up process do this
   // asynchronously.
   String path = getCacheFilePath(contentUrl);
   memoryStore.remove(Key.forUrl(contentUrl));
   memoryStore.remove(Key.forCacheFile(path));
 }
Пример #2
0
  @Test
  public void getContentUrl() {
    final File cacheFile = new File("/some/path");
    final String expectedUrl = "store://some/url";
    Mockito.when(lookupTable.get(Key.forCacheFile(cacheFile))).thenReturn(expectedUrl);

    String url = contentCache.getContentUrl(cacheFile);

    assertEquals("Content URLs should match", expectedUrl, url);
  }
Пример #3
0
  @Test
  public void remove() {
    final String url = "store://some/url.bin";
    final String path = "/some/path";
    Mockito.when(lookupTable.get(Key.forUrl(url))).thenReturn(path);

    contentCache.remove(url);

    Mockito.verify(lookupTable).remove(Key.forUrl(url));
    Mockito.verify(lookupTable).remove(Key.forCacheFile(path));
  }
Пример #4
0
  @Test
  public void getWriter() {
    final String url = "store://some/url.bin";

    FileContentWriter writer = (FileContentWriter) contentCache.getWriter(url);
    writer.putContent("Some test content for " + getClass().getName());

    assertEquals(url, writer.getContentUrl());
    // Check cached item is recorded properly in ehcache
    Mockito.verify(lookupTable).put(Key.forUrl(url), writer.getFile().getAbsolutePath());
    Mockito.verify(lookupTable).put(Key.forCacheFile(writer.getFile().getAbsolutePath()), url);
  }
Пример #5
0
  @Test(expected = CacheMissException.class)
  public void getReaderForItemInCacheButMissingContentFile() {
    final String url = "store://content/url.bin";
    Mockito.when(lookupTable.contains(Key.forUrl(url))).thenReturn(true);
    final String path = "/no/content/file/at/this/path.bin";
    Mockito.when(lookupTable.get(Key.forUrl(url))).thenReturn(path);

    try {
      contentCache.getReader(url);
    } finally {
      // Important the get(path) was called, so that the timeToIdle is reset
      // for the 'reverse lookup' as well as the URL to path mapping.
      Mockito.verify(lookupTable).get(Key.forCacheFile(path));
    }
  }
Пример #6
0
  @Test
  public void putForNonEmptyFile() {
    ContentReader contentReader = Mockito.mock(ContentReader.class);
    Mockito.when(contentReader.getSize()).thenReturn(999000L);
    final String url = "store://some/url.bin";
    boolean putResult = contentCache.put(url, contentReader);

    assertTrue("Non-empty files should be cached", putResult);
    ArgumentCaptor<File> cacheFileArg = ArgumentCaptor.forClass(File.class);
    Mockito.verify(contentReader).getContent(cacheFileArg.capture());
    // Check cached item is recorded properly in ehcache
    Mockito.verify(lookupTable).put(Key.forUrl(url), cacheFileArg.getValue().getAbsolutePath());
    Mockito.verify(lookupTable)
        .put(Key.forCacheFile(cacheFileArg.getValue().getAbsolutePath()), url);
  }
Пример #7
0
  @Test
  public void canGetReaderForItemInCacheHavingLiveFile() {
    final String url = "store://content/url.bin";
    Mockito.when(lookupTable.contains(Key.forUrl(url))).thenReturn(true);
    final String path = tempfile().getAbsolutePath();
    Mockito.when(lookupTable.get(Key.forUrl(url))).thenReturn(path);

    FileContentReader reader = (FileContentReader) contentCache.getReader(url);

    assertEquals("Reader should have correct URL", url, reader.getContentUrl());
    assertEquals(
        "Reader should be for correct cached content file",
        path,
        reader.getFile().getAbsolutePath());
    // Important the get(path) was called, so that the timeToIdle is reset
    // for the 'reverse lookup' as well as the URL to path mapping.
    Mockito.verify(lookupTable).get(Key.forCacheFile(path));
  }
Пример #8
0
  @Override
  public ContentReader getReader(String contentUrl) {
    Key url = Key.forUrl(contentUrl);
    if (memoryStore.contains(url)) {
      String path = memoryStore.get(url);

      // Getting the path for a URL from the memoryStore will reset the timeToIdle for
      // that URL. It is important to perform a reverse lookup as well to ensure that the
      // cache file path to URL mapping is also kept in the cache.
      memoryStore.get(Key.forCacheFile(path));

      File cacheFile = new File(path);
      if (cacheFile.exists()) {
        return new FileContentReader(cacheFile, contentUrl);
      }
    }

    throw new CacheMissException(contentUrl);
  }
Пример #9
0
 /**
  * Get a content URL from the cache - keyed by File.
  *
  * @param file
  * @return
  */
 public String getContentUrl(File file) {
   return memoryStore.get(Key.forCacheFile(file));
 }
Пример #10
0
 private void recordCacheEntries(String contentUrl, File cacheFile) {
   memoryStore.put(Key.forUrl(contentUrl), cacheFile.getAbsolutePath());
   memoryStore.put(Key.forCacheFile(cacheFile), contentUrl);
 }