org.glassfish.grizzly.http.server.filecache
Class FileCache

java.lang.Object
  extended by org.glassfish.grizzly.http.server.filecache.FileCache
All Implemented Interfaces:
JmxMonitoringAware<FileCacheProbe>, MonitoringAware<FileCacheProbe>

public class FileCache
extends java.lang.Object
implements JmxMonitoringAware<FileCacheProbe>

This class implements a file caching mechanism used to cache static resources.

Author:
Jeanfrancois Arcand, Scott Oaks

Nested Class Summary
static class FileCache.CacheType
           
 
Field Summary
protected  AbstractJmxMonitoringConfig<FileCacheProbe> monitoringConfig
          File cache probes
 
Constructor Summary
FileCache()
           
 
Method Summary
 void add(HttpRequestPacket request, java.io.File cacheFile)
          Add a resource to the cache.
protected  long addHeapSize(long size)
           
protected  long addMappedMemorySize(long size)
           
protected  boolean checkIfHeaders(FileCacheEntry entry, HttpRequestPacket request)
          Check if the conditions specified in the optional If headers are satisfied.
protected  boolean checkIfMatch(FileCacheEntry entry, HttpRequestPacket request)
          Check if the if-match condition is satisfied.
protected  boolean checkIfUnmodifiedSince(FileCacheEntry entry, HttpRequestPacket request)
          Check if the if-unmodified-since condition is satisfied.
protected  JmxObject createJmxManagementObject()
           
 HttpPacket get(HttpRequestPacket request)
          Send the cache.
 long getHeapCacheSize()
          Return the heap space used for cache
 long getMappedCacheSize()
          Return the size of Mapped memory used for caching
 int getMaxCacheEntries()
           
 long getMaxEntrySize()
           
 long getMaxLargeFileCacheSize()
           
 long getMaxSmallFileCacheSize()
           
 long getMinEntrySize()
           
 JmxMonitoringConfig<FileCacheProbe> getMonitoringConfig()
          
 int getSecondsMaxAge()
           
 void initialize(MemoryManager memoryManager, DelayedExecutor delayedExecutor)
           
 boolean isEnabled()
           
protected  HttpPacket makeResponse(FileCacheEntry entry, HttpRequestPacket request)
          Send the cached resource.
protected static void notifyProbesEntryAdded(FileCache fileCache, FileCacheEntry entry)
          Notify registered FileCacheProbes about the "entry added" event.
protected static void notifyProbesEntryHit(FileCache fileCache, FileCacheEntry entry)
          Notify registered FileCacheProbes about the "entry hit event.
protected static void notifyProbesEntryMissed(FileCache fileCache, HttpRequestPacket request)
          Notify registered FileCacheProbes about the "entry missed" event.
protected static void notifyProbesEntryRemoved(FileCache fileCache, FileCacheEntry entry)
          Notify registered FileCacheProbes about the "entry removed" event.
protected static void notifyProbesError(FileCache fileCache, java.lang.Throwable error)
          Notify registered FileCacheProbes about the error.
protected  void remove(FileCacheEntry entry)
           
 void setEnabled(boolean enabled)
          Enables/disables the FileCache.
 void setMaxCacheEntries(int maxCacheEntries)
          Sets the maximum number of files that may be cached.
 void setMaxEntrySize(long maxEntrySize)
          The maximum size, in bytes, a resource may be before it can no longer be considered cachable.
 void setMaxLargeFileCacheSize(long maxLargeFileCacheSize)
          Sets the maximum size, in bytes, of the memory mapped cache for large files.
 void setMaxSmallFileCacheSize(long maxSmallFileCacheSize)
          The maximum size, in bytes, of the heap cache for files below the water mark set by getMinEntrySize().
 void setMinEntrySize(long minEntrySize)
          The maximum size, in bytes, a file must be in order to be cached in the heap cache.
 void setSecondsMaxAge(int secondsMaxAge)
          Sets the maximum time, in seconds, a file may be cached.
protected  long subHeapSize(long size)
           
protected  long subMappedMemorySize(long size)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

monitoringConfig

protected final AbstractJmxMonitoringConfig<FileCacheProbe> monitoringConfig
File cache probes

Constructor Detail

FileCache

public FileCache()
Method Detail

initialize

public void initialize(MemoryManager memoryManager,
                       DelayedExecutor delayedExecutor)

add

public void add(HttpRequestPacket request,
                java.io.File cacheFile)
Add a resource to the cache. Currently, only static resources served by the DefaultServlet can be cached.


get

public HttpPacket get(HttpRequestPacket request)
Send the cache.


remove

protected void remove(FileCacheEntry entry)

createJmxManagementObject

protected JmxObject createJmxManagementObject()

makeResponse

protected HttpPacket makeResponse(FileCacheEntry entry,
                                  HttpRequestPacket request)
                           throws java.io.IOException
Send the cached resource.

Throws:
java.io.IOException

getSecondsMaxAge

public int getSecondsMaxAge()
Returns:
the maximum time, in seconds, a file may be cached.

setSecondsMaxAge

public void setSecondsMaxAge(int secondsMaxAge)
Sets the maximum time, in seconds, a file may be cached.

Parameters:
secondsMaxAge - max age of a cached file, in seconds.

getMaxCacheEntries

public int getMaxCacheEntries()
Returns:
the maximum number of files that may be cached.

setMaxCacheEntries

public void setMaxCacheEntries(int maxCacheEntries)
Sets the maximum number of files that may be cached.

Parameters:
maxCacheEntries - the maximum number of files that may be cached.

getMinEntrySize

public long getMinEntrySize()
Returns:
the minimum size, in bytes, a file must be in order to be cached in the heap cache.

setMinEntrySize

public void setMinEntrySize(long minEntrySize)
The maximum size, in bytes, a file must be in order to be cached in the heap cache.

Parameters:
minEntrySize - the maximum size, in bytes, a file must be in order to be cached in the heap cache.

getMaxEntrySize

public long getMaxEntrySize()
Returns:
the maximum size, in bytes, a resource may be before it can no longer be considered cachable.

setMaxEntrySize

public void setMaxEntrySize(long maxEntrySize)
The maximum size, in bytes, a resource may be before it can no longer be considered cachable.

Parameters:
maxEntrySize - the maximum size, in bytes, a resource may be before it can no longer be considered cachable.

getMaxLargeFileCacheSize

public long getMaxLargeFileCacheSize()
Returns:
the maximum size of the memory mapped cache for large files.

setMaxLargeFileCacheSize

public void setMaxLargeFileCacheSize(long maxLargeFileCacheSize)
Sets the maximum size, in bytes, of the memory mapped cache for large files.

Parameters:
maxLargeFileCacheSize - the maximum size, in bytes, of the memory mapped cache for large files.

getMaxSmallFileCacheSize

public long getMaxSmallFileCacheSize()
Returns:
the maximum size, in bytes, of the heap cache for files below the water mark set by getMinEntrySize().

setMaxSmallFileCacheSize

public void setMaxSmallFileCacheSize(long maxSmallFileCacheSize)
The maximum size, in bytes, of the heap cache for files below the water mark set by getMinEntrySize().

Parameters:
maxSmallFileCacheSize - the maximum size, in bytes, of the heap cache for files below the water mark set by getMinEntrySize().

isEnabled

public boolean isEnabled()
Returns:
true if the FileCache is enabled, otherwise false

setEnabled

public void setEnabled(boolean enabled)
Enables/disables the FileCache. By default, the FileCache is disabled.

Parameters:
enabled - true to enable the FileCache.

addHeapSize

protected final long addHeapSize(long size)

subHeapSize

protected final long subHeapSize(long size)

getHeapCacheSize

public long getHeapCacheSize()
Return the heap space used for cache

Returns:
heap size

addMappedMemorySize

protected final long addMappedMemorySize(long size)

subMappedMemorySize

protected final long subMappedMemorySize(long size)

getMappedCacheSize

public long getMappedCacheSize()
Return the size of Mapped memory used for caching

Returns:
Mapped memory size

checkIfHeaders

protected boolean checkIfHeaders(FileCacheEntry entry,
                                 HttpRequestPacket request)
                          throws java.io.IOException
Check if the conditions specified in the optional If headers are satisfied.

Returns:
boolean true if the resource meets all the specified conditions, and false if any of the conditions is not satisfied, in which case request processing is stopped
Throws:
java.io.IOException

checkIfUnmodifiedSince

protected boolean checkIfUnmodifiedSince(FileCacheEntry entry,
                                         HttpRequestPacket request)
                                  throws java.io.IOException
Check if the if-unmodified-since condition is satisfied.

Returns:
boolean true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
Throws:
java.io.IOException

checkIfMatch

protected boolean checkIfMatch(FileCacheEntry entry,
                               HttpRequestPacket request)
                        throws java.io.IOException
Check if the if-match condition is satisfied.

Parameters:
request - The servlet request we are processing
entry - the FileCacheEntry to validate
Returns:
true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
Throws:
java.io.IOException

getMonitoringConfig

public JmxMonitoringConfig<FileCacheProbe> getMonitoringConfig()

Specified by:
getMonitoringConfig in interface JmxMonitoringAware<FileCacheProbe>
Specified by:
getMonitoringConfig in interface MonitoringAware<FileCacheProbe>

notifyProbesEntryAdded

protected static void notifyProbesEntryAdded(FileCache fileCache,
                                             FileCacheEntry entry)
Notify registered FileCacheProbes about the "entry added" event.

Parameters:
fileCache - the FileCache event occurred on.
entry - entry been added

notifyProbesEntryRemoved

protected static void notifyProbesEntryRemoved(FileCache fileCache,
                                               FileCacheEntry entry)
Notify registered FileCacheProbes about the "entry removed" event.

Parameters:
fileCache - the FileCache event occurred on.
entry - entry been removed

notifyProbesEntryHit

protected static void notifyProbesEntryHit(FileCache fileCache,
                                           FileCacheEntry entry)
Notify registered FileCacheProbes about the "entry hit event.

Parameters:
fileCache - the FileCache event occurred on.
entry - entry been hit.

notifyProbesEntryMissed

protected static void notifyProbesEntryMissed(FileCache fileCache,
                                              HttpRequestPacket request)
Notify registered FileCacheProbes about the "entry missed" event.

Parameters:
fileCache - the FileCache event occurred on.
request - HTTP request.

notifyProbesError

protected static void notifyProbesError(FileCache fileCache,
                                        java.lang.Throwable error)
Notify registered FileCacheProbes about the error.

Parameters:
fileCache - the FileCache event occurred on.


Copyright © 2012 Oracle Corporation. All Rights Reserved.