Package org.apache.jackrabbit.core.data
Class LocalCache
- java.lang.Object
-
- org.apache.jackrabbit.core.data.LocalCache
-
public class LocalCache extends java.lang.Object
This class implements a LRU cache used byCachingDataStore
. If cache size exceeds limit, this cache goes in purge mode. In purge mode any operation to cache is no-op. After purge cache size would be less than cachePurgeResizeFactor * maximum size.
-
-
Constructor Summary
Constructors Constructor Description LocalCache(java.lang.String path, java.lang.String tmpPath, long maxSizeInBytes, double cachePurgeTrigFactor, double cachePurgeResizeFactor, AsyncUploadCache asyncUploadCache)
Build LRU cache of files located at 'path'.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close the cache.void
delete(java.lang.String fileName)
Delete file from cache.java.io.File
getFileIfStored(java.lang.String fileName)
java.lang.Long
getFileLength(java.lang.String fileName)
Returns length of file if exists in cache else returns null.java.io.InputStream
getIfStored(java.lang.String fileName)
Return the inputstream from from cache, or null if not in the cache.java.io.File
store(java.lang.String fileName, java.io.File src)
Store an item along with file in cache.AsyncUploadCacheResult
store(java.lang.String fileName, java.io.File src, boolean tryForAsyncUpload)
This method add file toLocalCache
and tries that file can be added toAsyncUploadCache
.java.io.InputStream
store(java.lang.String fileName, java.io.InputStream in)
Store an item in the cache and return the input stream.
-
-
-
Constructor Detail
-
LocalCache
public LocalCache(java.lang.String path, java.lang.String tmpPath, long maxSizeInBytes, double cachePurgeTrigFactor, double cachePurgeResizeFactor, AsyncUploadCache asyncUploadCache)
Build LRU cache of files located at 'path'. It uses lastModified property of file to build LRU cache. If cache size exceeds limit size, this cache goes in purge mode. In purge mode any operation to cache is no-op.- Parameters:
path
- file system pathtmpPath
- temporary directory used by cache.maxSizeInBytes
- maximum size of cache.cachePurgeTrigFactor
- factor which triggers cache to purge mode. That is if current size exceed (cachePurgeTrigFactor * maxSizeInBytes), the cache will go in auto-purge mode.cachePurgeResizeFactor
- after cache purge size of cache will be just less (cachePurgeResizeFactor * maxSizeInBytes).asyncUploadCache
-AsyncUploadCache
-
-
Method Detail
-
store
public java.io.InputStream store(java.lang.String fileName, java.io.InputStream in) throws java.io.IOException
Store an item in the cache and return the input stream. If cache is in purgeMode or file doesn't exists, inputstream from aTransientFileFactory.createTransientFile(String, String, File)
is returned. Otherwise inputStream from cached file is returned. This method doesn't close the incoming inputstream.- Parameters:
fileName
- the key of cache.in
-InputStream
- Returns:
- the (new) input stream.
- Throws:
java.io.IOException
-
store
public java.io.File store(java.lang.String fileName, java.io.File src)
Store an item along with file in cache. Cache size is increased byFile.length()
If file already exists in cache,File.setLastModified(long)
is updated with current time.- Parameters:
fileName
- the key of cache.src
- file to be added to cache.
-
store
public AsyncUploadCacheResult store(java.lang.String fileName, java.io.File src, boolean tryForAsyncUpload) throws java.io.IOException
This method add file toLocalCache
and tries that file can be added toAsyncUploadCache
. If file is added toAsyncUploadCache
successfully, it setsAsyncUploadCacheResult.setAsyncUpload(boolean)
to true.- Parameters:
fileName
- name of the file.src
- source file.tryForAsyncUpload
- If true it tries to add fileName toAsyncUploadCache
- Returns:
AsyncUploadCacheResult
. This method setsAsyncUploadCacheResult.setAsyncUpload(boolean)
to true, if fileName is added toAsyncUploadCache
successfully else it setsAsyncUploadCacheResult.setAsyncUpload(boolean)
to false.AsyncUploadCacheResult.getFile()
contains cached file, if it is added toLocalCache
or original file.- Throws:
java.io.IOException
-
getIfStored
public java.io.InputStream getIfStored(java.lang.String fileName) throws java.io.IOException
Return the inputstream from from cache, or null if not in the cache.- Parameters:
fileName
- name of file.- Returns:
- stream or null.
- Throws:
java.io.IOException
-
getFileIfStored
public java.io.File getFileIfStored(java.lang.String fileName) throws java.io.IOException
- Throws:
java.io.IOException
-
delete
public void delete(java.lang.String fileName)
Delete file from cache. Size of cache is reduced by file length. The method is no-op if file doesn't exist in cache.- Parameters:
fileName
- file name that need to be removed from cache.
-
getFileLength
public java.lang.Long getFileLength(java.lang.String fileName)
Returns length of file if exists in cache else returns null.- Parameters:
fileName
- name of the file.
-
close
public void close()
Close the cache. Cache maintain set of files which it was not able to delete successfully. This method will an attempt to delete all unsuccessful delete files.
-
-