org.apache.jackrabbit.core.data
Class LocalCache

java.lang.Object
  extended by org.apache.jackrabbit.core.data.LocalCache

public class LocalCache
extends Object

This class implements a LRU cache used by CachingDataStore. 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
LocalCache(String path, String tmpPath, long size, double cachePurgeTrigFactor, double cachePurgeResizeFactor, AsyncUploadCache asyncUploadCache)
          Build LRU cache of files located at 'path'.
 
Method Summary
 void close()
          Close the cache.
 void delete(String fileName)
          Delete file from cache.
 File getFileIfStored(String fileName)
           
 Long getFileLength(String fileName)
          Returns length of file if exists in cache else returns null.
 InputStream getIfStored(String fileName)
          Return the inputstream from from cache, or null if not in the cache.
 File store(String fileName, File src)
          Store an item along with file in cache.
 AsyncUploadCacheResult store(String fileName, File src, boolean tryForAsyncUpload)
          This method add file to LocalCache and tries that file can be added to AsyncUploadCache.
 InputStream store(String fileName, InputStream in)
          Store an item in the cache and return the input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LocalCache

public LocalCache(String path,
                  String tmpPath,
                  long size,
                  double cachePurgeTrigFactor,
                  double cachePurgeResizeFactor,
                  AsyncUploadCache asyncUploadCache)
           throws IOException,
                  ClassNotFoundException
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 path
tmpPath - temporary directory used by cache.
maxSize - maximum size of cache.
cachePurgeTrigFactor - factor which triggers cache to purge mode. That is if current size exceed (cachePurgeTrigFactor * maxSize), the cache will go in auto-purge mode.
cachePurgeResizeFactor - after cache purge size of cache will be just less (cachePurgeResizeFactor * maxSize).
asyncUploadCache - AsyncUploadCache
Throws:
RepositoryException
IOException
ClassNotFoundException
Method Detail

store

public InputStream store(String fileName,
                         InputStream in)
                  throws IOException
Store an item in the cache and return the input stream. If cache is in purgeMode or file doesn't exists, inputstream from a TransientFileFactory.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:
IOException

store

public File store(String fileName,
                  File src)
Store an item along with file in cache. Cache size is increased by File.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.
Throws:
IOException

store

public AsyncUploadCacheResult store(String fileName,
                                    File src,
                                    boolean tryForAsyncUpload)
                             throws IOException
This method add file to LocalCache and tries that file can be added to AsyncUploadCache. If file is added to AsyncUploadCache successfully, it sets AsyncUploadResult#setAsyncUpload(boolean) to true.

Parameters:
fileName - name of the file.
src - source file.
tryForAsyncUpload - If true it tries to add fileName to AsyncUploadCache
Returns:
AsyncUploadCacheResult. This method sets AsyncUploadResult#setAsyncUpload(boolean) to true, if fileName is added to AsyncUploadCache successfully else it sets AsyncUploadCacheResult.setAsyncUpload(boolean) to false. AsyncUploadCacheResult.getFile() contains cached file, if it is added to LocalCache or original file.
Throws:
IOException

getIfStored

public InputStream getIfStored(String fileName)
                        throws IOException
Return the inputstream from from cache, or null if not in the cache.

Parameters:
fileName - name of file.
Returns:
stream or null.
Throws:
IOException

getFileIfStored

public File getFileIfStored(String fileName)
                     throws IOException
Throws:
IOException

delete

public void delete(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 Long getFileLength(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.



Copyright © 2004-2014 The Apache Software Foundation. All Rights Reserved.