ALF-9613: fix some cases of content being in in-memory cache but not on disk.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29957 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2011-08-22 14:55:18 +00:00
parent 7d8bd1abe3
commit a359d3cec9
4 changed files with 84 additions and 17 deletions

View File

@@ -49,7 +49,8 @@ public class CachingContentStore implements ContentStore
private ContentCache cache; private ContentCache cache;
private boolean cacheOnInbound; private boolean cacheOnInbound;
static { static
{
locks = new Object[numLocks]; locks = new Object[numLocks];
for (int i = 0; i < numLocks; i++) for (int i = 0; i < numLocks; i++)
{ {
@@ -152,19 +153,39 @@ public class CachingContentStore implements ContentStore
// when it should only be read once and cached versions should be returned after that. // when it should only be read once and cached versions should be returned after that.
synchronized(lock(contentUrl)) synchronized(lock(contentUrl))
{ {
if (!cache.contains(contentUrl)) return retryingCacheRead(contentUrl);
}
}
private ContentReader retryingCacheRead(String url)
{ {
ContentReader bsReader = backingStore.getReader(contentUrl); int triesLeft = 15;
if (!cache.put(contentUrl, bsReader))
while (triesLeft > 0)
{ {
// Content wasn't put into cache successfully. try
{
return cache.getReader(url);
}
catch (CacheMissException e)
{
// Cached content is missing either from memory or disk
// so try and populate it and retry reading it.
ContentReader bsReader = backingStore.getReader(url);
if (!cache.put(url, bsReader))
{
// Content was empty - probably hasn't been written yet.
return bsReader.getReader(); return bsReader.getReader();
} }
else
{
triesLeft--;
}
} }
} }
// TODO: what if, in the meantime this item has been deleted from the disk cache? // Give up and use the backing store directly
return cache.getReader(contentUrl); return backingStore.getReader(url);
} }

View File

@@ -19,6 +19,7 @@
package org.alfresco.repo.content.caching; package org.alfresco.repo.content.caching;
import java.io.File; import java.io.File;
import java.io.FilenameFilter;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
@@ -137,6 +138,30 @@ public class CachingContentStoreSpringTest extends AbstractWritableContentStoreT
} }
public void testStoreWillRecoverFromDeletedCacheFile()
{
final String content = "Content for " + getName() + " test.";
// Write some content to the backing store.
ContentWriter writer = backingStore.getWriter(ContentContext.NULL_CONTEXT);
writer.putContent(content);
final String contentUrl = writer.getContentUrl();
// Read content using the CachingContentStore - will cause content to be cached.
String retrievedContent = store.getReader(contentUrl).getContentString();
assertEquals(content, retrievedContent);
// Remove the cached disk file
File cacheFile = new File(cache.cacheFileLocation(contentUrl));
cacheFile.delete();
assertTrue("Cached content should have been deleted", !cacheFile.exists());
// Should still be able to ask for this content, even though the cache file was
// deleted and the record of the cache is still in the in-memory cache/lookup.
String contentAfterDelete = store.getReader(contentUrl).getContentString();
assertEquals(content, contentAfterDelete);
}
/* /*
* @see org.alfresco.repo.content.AbstractReadOnlyContentStoreTest#getStore() * @see org.alfresco.repo.content.AbstractReadOnlyContentStoreTest#getStore()

View File

@@ -75,7 +75,6 @@ public class CachingContentStoreTest
public void getReaderForItemInCache() public void getReaderForItemInCache()
{ {
ContentReader cachedContentReader = mock(ContentReader.class); ContentReader cachedContentReader = mock(ContentReader.class);
when(cache.contains("url")).thenReturn(true);
when(cache.getReader("url")).thenReturn(cachedContentReader); when(cache.getReader("url")).thenReturn(cachedContentReader);
ContentReader returnedReader = cachingStore.getReader("url"); ContentReader returnedReader = cachingStore.getReader("url");
@@ -86,16 +85,25 @@ public class CachingContentStoreTest
@Test @Test
public void getReadForItemMissingFromCache() public void getReaderForItemMissingFromCache()
{ {
ContentReader sourceContent = mock(ContentReader.class); ContentReader sourceContent = mock(ContentReader.class);
when(cache.contains("url")).thenReturn(false); when(cache.getReader("url")).thenThrow(new CacheMissException("url"));
when(backingStore.getReader("url")).thenReturn(sourceContent); when(backingStore.getReader("url")).thenReturn(sourceContent);
when(cache.put("url", sourceContent)).thenReturn(true);
cachingStore.getReader("url"); cachingStore.getReader("url");
}
verify(backingStore).getReader("url"); @Test
verify(cache).put("url", sourceContent); public void getReaderForItemMissingFromCacheButNoContentToCache()
{
ContentReader sourceContent = mock(ContentReader.class);
when(cache.getReader("url")).thenThrow(new CacheMissException("url"));
when(backingStore.getReader("url")).thenReturn(sourceContent);
when(cache.put("url", sourceContent)).thenReturn(false);
cachingStore.getReader("url");
} }

View File

@@ -63,7 +63,11 @@ public class ContentCacheImpl implements ContentCache
if (memoryStore.contains(contentUrl)) if (memoryStore.contains(contentUrl))
{ {
String path = memoryStore.get(contentUrl); String path = memoryStore.get(contentUrl);
return new FileContentReader(new File(path), contentUrl); File cacheFile = new File(path);
if (cacheFile.exists())
{
return new FileContentReader(cacheFile, contentUrl);
}
} }
throw new CacheMissException(contentUrl); throw new CacheMissException(contentUrl);
@@ -173,4 +177,13 @@ public class ContentCacheImpl implements ContentCache
{ {
this.memoryStore = memoryStore; this.memoryStore = memoryStore;
} }
// Not part of the ContentCache interface as this breaks encapsulation.
// Handy method for tests though, since it allows us to find out where
// the content was cached.
protected String cacheFileLocation(String url)
{
return memoryStore.get(url);
}
} }