mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
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:
@@ -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,22 +153,42 @@ 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)
|
||||||
|
{
|
||||||
|
int triesLeft = 15;
|
||||||
|
|
||||||
|
while (triesLeft > 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
ContentReader bsReader = backingStore.getReader(contentUrl);
|
return cache.getReader(url);
|
||||||
if (!cache.put(contentUrl, bsReader))
|
}
|
||||||
|
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 wasn't put into cache successfully.
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @see org.alfresco.repo.content.ContentStore#getWriter(org.alfresco.repo.content.ContentContext)
|
* @see org.alfresco.repo.content.ContentStore#getWriter(org.alfresco.repo.content.ContentContext)
|
||||||
*/
|
*/
|
||||||
|
@@ -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()
|
||||||
|
@@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
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);
|
||||||
|
|
||||||
verify(backingStore).getReader("url");
|
cachingStore.getReader("url");
|
||||||
verify(cache).put("url", sourceContent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user