org.apache.jetspeed.cache
Class FileCache

java.lang.Object
  extended byorg.apache.jetspeed.cache.FileCache
All Implemented Interfaces:
java.util.Comparator

public class FileCache
extends java.lang.Object
implements java.util.Comparator

FileCache keeps a cache of files up-to-date with a most simple eviction policy. The eviction policy will keep n items in the cache, and then start evicting the items ordered-by least used first. The cache runs a thread to check for both evictions and refreshes.

Version:
$Id: FileCache.java,v 1.5 2004/03/25 16:27:41 jford Exp $
Author:
David S. Taylor David Sean Taylor

Nested Class Summary
protected  class FileCache.FileCacheScanner
          inner class that runs as a thread to scan the cache for updates or evictions
 
Field Summary
protected  java.util.List listeners
           
protected  int maxSize
           
protected  long scanRate
           
 
Constructor Summary
FileCache()
          Default constructor.
FileCache(int initialCapacity, int loadFactor, long scanRate, int maxSize)
          Set all parameters on the cache
FileCache(long scanRate, int maxSize)
          Set scanRate and maxSize
 
Method Summary
 void addListener(FileCacheEventListener listener)
          Add a File Cache Event Listener
 int compare(java.lang.Object o1, java.lang.Object o2)
          Comparator function for sorting by last accessed during eviction
protected  void evict()
          Evicts entries based on last accessed time stamp
 FileCacheEntry get(java.lang.String key)
          Gets an entry from the cache given a key
 java.lang.Object getDocument(java.lang.String key)
          Gets an entry from the cache given a key
 java.util.Iterator getIterator()
          get an iterator over the cache values
 int getMaxSize()
          Get the maximum size of the cache
 long getScanRate()
          Get the refresh scan rate
 int getSize()
          get the size of the cache
 void put(java.io.File file, java.lang.Object document)
          Puts a file entry in the file cache
 void put(java.lang.String key, java.lang.Object document)
          Puts a file entry in the file cache
 java.lang.Object remove(java.lang.String key)
          Removes a file entry from the file cache
 void setMaxSize(int maxSize)
          Set the new maximum size of the cache
 void setScanRate(long scanRate)
          Set the new refresh scan rate on managed files.
 void startFileScanner()
          Start the file Scanner running at the current scan rate.
 void stopFileScanner()
          Stop the file Scanner
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Field Detail

scanRate

protected long scanRate

maxSize

protected int maxSize

listeners

protected java.util.List listeners
Constructor Detail

FileCache

public FileCache()
Default constructor. Use default values for scanReate and maxSize


FileCache

public FileCache(long scanRate,
                 int maxSize)
Set scanRate and maxSize

Parameters:
scanRate - how often in seconds to refresh and evict from the cache
maxSize - the maximum allowed size of the cache before eviction starts

FileCache

public FileCache(int initialCapacity,
                 int loadFactor,
                 long scanRate,
                 int maxSize)
Set all parameters on the cache

Parameters:
initialCapacity - the initial size of the cache as passed to HashMap
loadFactor - how full the hash table is allowed to get before increasing
scanRate - how often in seconds to refresh and evict from the cache
maxSize - the maximum allowed size of the cache before eviction starts
Method Detail

setScanRate

public void setScanRate(long scanRate)
Set the new refresh scan rate on managed files.

Parameters:
scanRate - the new scan rate in seconds

getScanRate

public long getScanRate()
Get the refresh scan rate

Returns:
the current refresh scan rate in seconds

setMaxSize

public void setMaxSize(int maxSize)
Set the new maximum size of the cache

Parameters:
maxSize - the maximum size of the cache

getMaxSize

public int getMaxSize()
Get the maximum size of the cache

Returns:
the current maximum size of the cache

get

public FileCacheEntry get(java.lang.String key)
Gets an entry from the cache given a key

Parameters:
key - the key to look up the entry by
Returns:
the entry

getDocument

public java.lang.Object getDocument(java.lang.String key)
Gets an entry from the cache given a key

Parameters:
key - the key to look up the entry by
Returns:
the entry

put

public void put(java.io.File file,
                java.lang.Object document)
         throws java.io.IOException
Puts a file entry in the file cache

Parameters:
file - The file to be put in the cache
document - the cached document
Throws:
java.io.IOException

put

public void put(java.lang.String key,
                java.lang.Object document)
         throws java.io.IOException
Puts a file entry in the file cache

Parameters:
document - the cached document
Throws:
java.io.IOException

remove

public java.lang.Object remove(java.lang.String key)
Removes a file entry from the file cache

Parameters:
key - the full path name of the file
Returns:
the entry removed

addListener

public void addListener(FileCacheEventListener listener)
Add a File Cache Event Listener

Parameters:
listener - the event listener

startFileScanner

public void startFileScanner()
Start the file Scanner running at the current scan rate.


stopFileScanner

public void stopFileScanner()
Stop the file Scanner


evict

protected void evict()
Evicts entries based on last accessed time stamp


compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
Comparator function for sorting by last accessed during eviction

Specified by:
compare in interface java.util.Comparator

getIterator

public java.util.Iterator getIterator()
get an iterator over the cache values

Returns:
iterator over the cache values

getSize

public int getSize()
get the size of the cache

Returns:
the size of the cache


Copyright © 2000-2005 Apache Software Foundation. All Rights Reserved.