ALF-9613: numerous changes including use of ReentrantReadWriteLock for locking and introduction of a custom cleanup job that does not delete files that are in use by the cache.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30066 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2011-08-25 15:20:08 +00:00
parent bfacbeb0b3
commit 98947fda5c
19 changed files with 1029 additions and 119 deletions

View File

@@ -19,22 +19,23 @@
package org.alfresco.repo.content.caching;
import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.alfresco.repo.cache.SimpleCache;
import org.alfresco.repo.content.ContentStore;
import org.alfresco.repo.content.filestore.FileContentReader;
import org.alfresco.repo.content.filestore.FileContentWriter;
import org.alfresco.service.cmr.repository.ContentIOException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentStreamListener;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.util.TempFileProvider;
import org.alfresco.util.GUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* The one and only implementation of the ContentCache class.
* <p>
* Binary content data itself is stored on disk in temporary files managed
* by Alfresco (see {@link org.alfresco.util.TempFileProvider}).
* The one and only implementation of the ContentCache class. Binary content data itself
* is stored on disk in the location specified by {@link cacheRoot}.
* <p>
* The in-memory lookup table is provided by Ehcache.
*
@@ -42,27 +43,70 @@ import org.alfresco.util.TempFileProvider;
*/
public class ContentCacheImpl implements ContentCache
{
private static final String CACHE_DIR = "caching_cs";
private static final String TMP_FILE_EXTENSION = ".tmp";
private final File cacheRoot = TempFileProvider.getLongLifeTempDir(CACHE_DIR);
private SimpleCache<String, String> memoryStore;
private static final Log log = LogFactory.getLog(ContentCacheImpl.class);
private static final String CACHE_FILE_EXT = ".bin";
private File cacheRoot;
private SimpleCache<Key, String> memoryStore;
@Override
public boolean contains(String contentUrl)
{
return memoryStore.contains(contentUrl);
return memoryStore.contains(Key.forUrl(contentUrl));
}
/**
* Allows caller to perform lookup using a {@link Key}.
*
* @param key
* @return true if the cache contains, false otherwise.
*/
public boolean contains(Key key)
{
return memoryStore.contains(key);
}
/**
* Put an item in the lookup table.
*
* @param key
* @param value
*/
public void putIntoLookup(Key key, String value)
{
memoryStore.put(key, value);
}
/**
* Get the path of a cache file for the given content URL - will return null if there is no entry
* in the cache for the specified URL.
*
* @param contentUrl
* @return cache file path
*/
public String getCacheFilePath(String contentUrl)
{
return memoryStore.get(Key.forUrl(contentUrl));
}
/**
* Get a content URL from the cache - keyed by File.
*
* @param file
* @return
*/
public String getContentUrl(File file)
{
return memoryStore.get(Key.forCacheFile(file));
}
@Override
public ContentReader getReader(String contentUrl)
{
if (memoryStore.contains(contentUrl))
Key url = Key.forUrl(contentUrl);
if (memoryStore.contains(url))
{
String path = memoryStore.get(contentUrl);
String path = memoryStore.get(url);
File cacheFile = new File(path);
if (cacheFile.exists())
{
@@ -72,26 +116,29 @@ public class ContentCacheImpl implements ContentCache
throw new CacheMissException(contentUrl);
}
@Override
public boolean put(String contentUrl, ContentReader source)
{
File cacheFile = createCacheFile(contentUrl);
File cacheFile = createCacheFile();
// Copy the content from the source into a cache file
if (source.getSize() > 0L)
{
source.getContent(cacheFile);
// Add a record of the cached file to the in-memory cache.
memoryStore.put(contentUrl, cacheFile.getAbsolutePath());
recordCacheEntries(contentUrl, cacheFile);
return true;
}
return false;
}
private void recordCacheEntries(String contentUrl, File cacheFile)
{
memoryStore.put(Key.forUrl(contentUrl), cacheFile.getAbsolutePath());
memoryStore.put(Key.forCacheFile(cacheFile), contentUrl);
}
/**
* Create a File object and makes any intermediate directories in the path.
@@ -99,39 +146,28 @@ public class ContentCacheImpl implements ContentCache
* @param contentUrl
* @return File
*/
private File createCacheFile(String contentUrl)
private File createCacheFile()
{
File path = new File(cacheRoot, pathFromUrl(contentUrl));
File parentDir = path.getParentFile();
File file = new File(cacheRoot, createNewCacheFilePath());
File parentDir = file.getParentFile();
parentDir.mkdirs();
File cacheFile = TempFileProvider.createTempFile(path.getName(), TMP_FILE_EXTENSION, parentDir);
return cacheFile;
return file;
}
/*
* @see org.alfresco.repo.content.caching.ContentCache#remove(java.lang.String)
*/
@Override
public void remove(String contentUrl)
{
// Remove from the in-memory cache, but not from disk. Let the clean-up process do this asynchronously.
memoryStore.remove(contentUrl);
String path = getCacheFilePath(contentUrl);
memoryStore.remove(Key.forUrl(contentUrl));
memoryStore.remove(Key.forCacheFile(path));
}
/*
* @see org.alfresco.repo.content.caching.ContentCache#getWriter(org.alfresco.repo.content.ContentContext)
*/
@Override
public ContentWriter getWriter(final String url)
{
// Get a writer to a cache file.
final File cacheFile = createCacheFile(url);
final File cacheFile = createCacheFile();
ContentWriter writer = new FileContentWriter(cacheFile, url, null);
// Attach a listener to populate the in-memory store when done writing.
@@ -140,50 +176,112 @@ public class ContentCacheImpl implements ContentCache
@Override
public void contentStreamClosed() throws ContentIOException
{
memoryStore.put(url, cacheFile.getAbsolutePath());
recordCacheEntries(url, cacheFile);
}
});
return writer;
}
/**
* Converts a content URL to a relative path name where the protocol will
* be the name of a subdirectory. For example:
* Creates a relative path for a new cache file. The path is based
* upon the current date/time: year/month/day/hour/minute/guid.bin
* <p>
* store://2011/8/5/15/4/386595e0-3b52-4d5c-a32d-df9d0b9fd56e.bin
* <p>
* will become:
* <p>
* store/2011/8/5/15/4/386595e0-3b52-4d5c-a32d-df9d0b9fd56e.bin
*
* @param contentUrl
* @return String representation of relative path to file.
* e.g. 2011/12/3/13/55/27d56416-bf9f-4d89-8f9e-e0a52de0a59e.bin
* @return The relative path for the new cache file.
*/
private String pathFromUrl(String contentUrl)
public static String createNewCacheFilePath()
{
return contentUrl.replaceFirst(ContentStore.PROTOCOL_DELIMITER, "/");
Calendar calendar = new GregorianCalendar();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // 0-based
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
// create the URL
StringBuilder sb = new StringBuilder(20);
sb.append(year).append('/')
.append(month).append('/')
.append(day).append('/')
.append(hour).append('/')
.append(minute).append('/')
.append(GUID.generate()).append(CACHE_FILE_EXT);
return sb.toString();
}
/**
* Configure ContentCache with a memory store - an EhCacheAdapter.
*
* @param memoryStore the memoryStore to set
*/
public void setMemoryStore(SimpleCache<String, String> memoryStore)
public void setMemoryStore(SimpleCache<Key, String> memoryStore)
{
this.memoryStore = memoryStore;
}
/**
* Specify the directory where cache files will be written.
*
* @param cacheRoot
*/
public void setCacheRoot(File cacheRoot)
{
this.cacheRoot = cacheRoot;
}
/**
* Returns the directory where cache files will be written (cacheRoot).
*
* @return cacheRoot
*/
public File getCacheRoot()
{
return this.cacheRoot;
}
// 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);
return memoryStore.get(Key.forUrl(url));
}
/**
* @param cachedContentCleaner
*/
public void processFiles(FileHandler handler)
{
handleDir(cacheRoot, handler);
}
/**
* Recurse into a directory handling cache files (*.bin) with the supplied
* {@link FileHandler}.
*
* @param dir
* @param handler
*/
private void handleDir(File dir, FileHandler handler)
{
if (dir.isDirectory())
{
File[] files = dir.listFiles();
for (File file : files)
{
if (file.isDirectory())
{
handleDir(file, handler);
}
else
{
if (file.getName().endsWith(CACHE_FILE_EXT)) handler.handle(file);
}
}
}
else
{
throw new IllegalArgumentException("handleDir() called with non-directory: " + dir.getAbsolutePath());
}
}
}