Class FileCache
- java.lang.Object
-
- org.glassfish.grizzly.http.server.filecache.FileCache
-
- All Implemented Interfaces:
org.glassfish.grizzly.monitoring.MonitoringAware<FileCacheProbe>
public class FileCache extends Object implements org.glassfish.grizzly.monitoring.MonitoringAware<FileCacheProbe>
This class implements a file caching mechanism used to cache static resources.- Author:
- Jeanfrancois Arcand, Scott Oaks
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFileCache.CacheResultstatic classFileCache.CacheType
-
Field Summary
Fields Modifier and Type Field Description protected org.glassfish.grizzly.monitoring.DefaultMonitoringConfig<FileCacheProbe>monitoringConfigFile cache probes
-
Constructor Summary
Constructors Constructor Description FileCache()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description FileCache.CacheResultadd(org.glassfish.grizzly.http.HttpRequestPacket request, long lastModified)Add a resource to the cache.FileCache.CacheResultadd(org.glassfish.grizzly.http.HttpRequestPacket request, File cacheFile)Add aFileresource to the cache.protected FileCache.CacheResultadd(org.glassfish.grizzly.http.HttpRequestPacket request, File cacheFile, long lastModified)Add a resource to the cache.protected longaddHeapSize(long size)protected longaddMappedMemorySize(long size)protected voidcompressFile(FileCacheEntry entry)Creates a temporary compressed representation of the given cache entry.protected static longconvertToLong(String dateHeader)protected ObjectcreateJmxManagementObject()FileCacheEntryget(org.glassfish.grizzly.http.HttpRequestPacket request)ReturnsFileCacheEntry.FilegetCompressedFilesFolder()Returns the folder to be used to store temporary compressed files.org.glassfish.grizzly.http.CompressionConfiggetCompressionConfig()Returns the FileCache compression configuration settings.longgetHeapCacheSize()Return the heap space used for cachelonggetMappedCacheSize()Return the size of Mapped memory used for cachingintgetMaxCacheEntries()longgetMaxEntrySize()longgetMaxLargeFileCacheSize()longgetMaxSmallFileCacheSize()longgetMinEntrySize()org.glassfish.grizzly.monitoring.MonitoringConfig<FileCacheProbe>getMonitoringConfig()intgetSecondsMaxAge()voidinitialize(org.glassfish.grizzly.utils.DelayedExecutor delayedExecutor)booleanisEnabled()booleanisFileSendEnabled()Returnstrueif File resources may be be sent usingFileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel).protected static voidnotifyProbesEntryAdded(FileCache fileCache, FileCacheEntry entry)Notify registeredFileCacheProbes about the "entry added" event.protected static voidnotifyProbesEntryHit(FileCache fileCache, FileCacheEntry entry)Notify registeredFileCacheProbes about the "entry hit event.protected static voidnotifyProbesEntryMissed(FileCache fileCache, org.glassfish.grizzly.http.HttpRequestPacket request)Notify registeredFileCacheProbes about the "entry missed" event.protected static voidnotifyProbesEntryRemoved(FileCache fileCache, FileCacheEntry entry)Notify registeredFileCacheProbes about the "entry removed" event.protected static voidnotifyProbesError(FileCache fileCache, Throwable error)Notify registeredFileCacheProbes about the error.protected voidremove(FileCacheEntry entry)voidsetCompressedFilesFolder(File compressedFilesFolder)Sets the folder to be used to store temporary compressed files.voidsetEnabled(boolean enabled)Enables/disables theFileCache.voidsetFileSendEnabled(boolean fileSendEnabled)Configure whether or send-file support will enabled which allows sendingFileresources viaFileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel).voidsetMaxCacheEntries(int maxCacheEntries)Sets the maximum number of files that may be cached.voidsetMaxEntrySize(long maxEntrySize)The maximum size, in bytes, a resource may be before it can no longer be considered cacheable.voidsetMaxLargeFileCacheSize(long maxLargeFileCacheSize)Sets the maximum size, in bytes, of the memory mapped cache for large files.voidsetMaxSmallFileCacheSize(long maxSmallFileCacheSize)The maximum size, in bytes, of the heap cache for files below the water mark set bygetMinEntrySize().voidsetMinEntrySize(long minEntrySize)The maximum size, in bytes, a file must be in order to be cached in the heap cache.voidsetSecondsMaxAge(int secondsMaxAge)Sets the maximum time, in seconds, a file may be cached.protected longsubHeapSize(long size)protected longsubMappedMemorySize(long size)
-
-
-
Field Detail
-
monitoringConfig
protected final org.glassfish.grizzly.monitoring.DefaultMonitoringConfig<FileCacheProbe> monitoringConfig
File cache probes
-
-
Method Detail
-
initialize
public void initialize(org.glassfish.grizzly.utils.DelayedExecutor delayedExecutor)
-
add
public FileCache.CacheResult add(org.glassfish.grizzly.http.HttpRequestPacket request, long lastModified)
Add a resource to the cache. Unlike theadd(org.glassfish.grizzly.http.HttpRequestPacket, java.io.File)this method adds a resource to a cache but is not able to send the resource content to a client if client doesn't have the latest version of this resource.
-
add
public FileCache.CacheResult add(org.glassfish.grizzly.http.HttpRequestPacket request, File cacheFile)
-
add
protected FileCache.CacheResult add(org.glassfish.grizzly.http.HttpRequestPacket request, File cacheFile, long lastModified)
Add a resource to the cache.
-
get
public FileCacheEntry get(org.glassfish.grizzly.http.HttpRequestPacket request)
ReturnsFileCacheEntry. IfFileCacheEntryhas been found - this method also sets correspondentHttpResponsePacketstatus code and reason phrase.
-
remove
protected void remove(FileCacheEntry entry)
-
createJmxManagementObject
protected Object createJmxManagementObject()
-
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 cacheable.
-
setMaxEntrySize
public void setMaxEntrySize(long maxEntrySize)
The maximum size, in bytes, a resource may be before it can no longer be considered cacheable.- Parameters:
maxEntrySize- the maximum size, in bytes, a resource may be before it can no longer be considered cacheable.
-
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 bygetMinEntrySize().- Parameters:
maxSmallFileCacheSize- the maximum size, in bytes, of the heap cache for files below the water mark set bygetMinEntrySize().
-
isEnabled
public boolean isEnabled()
- Returns:
trueif theFileCacheis enabled, otherwisefalse
-
setEnabled
public void setEnabled(boolean enabled)
- Parameters:
enabled-trueto enable theFileCache.
-
getCompressionConfig
public org.glassfish.grizzly.http.CompressionConfig getCompressionConfig()
Returns the FileCache compression configuration settings.
-
getCompressedFilesFolder
public File getCompressedFilesFolder()
Returns the folder to be used to store temporary compressed files.
-
setCompressedFilesFolder
public void setCompressedFilesFolder(File compressedFilesFolder)
Sets the folder to be used to store temporary compressed files.
-
isFileSendEnabled
public boolean isFileSendEnabled()
Returns
trueif File resources may be be sent usingFileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel).By default, this property will be true, except in the following cases:
- JVM OS is HP-UX
- JVM OS is Linux, and the Oracle JVM in use is 1.6.0_17 or older
Finally, if the connection between endpoints is secure, send file functionality will be disabled regardless of configuration.
- Returns:
trueif resources will be sent usingFileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel).- Since:
- 2.3.5
-
setFileSendEnabled
public void setFileSendEnabled(boolean fileSendEnabled)
Configure whether or send-file support will enabled which allows sendingFileresources viaFileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel). If disabled, the more traditional byte[] copy will be used to send content.- Parameters:
fileSendEnabled-trueto enableFileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel)support.- Since:
- 2.3.5
-
compressFile
protected void compressFile(FileCacheEntry entry)
Creates a temporary compressed representation of the given cache entry.
-
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
-
getMonitoringConfig
public org.glassfish.grizzly.monitoring.MonitoringConfig<FileCacheProbe> getMonitoringConfig()
- Specified by:
getMonitoringConfigin interfaceorg.glassfish.grizzly.monitoring.MonitoringAware<FileCacheProbe>
-
notifyProbesEntryAdded
protected static void notifyProbesEntryAdded(FileCache fileCache, FileCacheEntry entry)
Notify registeredFileCacheProbes 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 registeredFileCacheProbes 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 registeredFileCacheProbes about the "entry hit event.- Parameters:
fileCache- the FileCache event occurred on.entry- entry been hit.
-
notifyProbesEntryMissed
protected static void notifyProbesEntryMissed(FileCache fileCache, org.glassfish.grizzly.http.HttpRequestPacket request)
Notify registeredFileCacheProbes about the "entry missed" event.- Parameters:
fileCache- the FileCache event occurred on.request- HTTP request.
-
notifyProbesError
protected static void notifyProbesError(FileCache fileCache, Throwable error)
Notify registeredFileCacheProbes about the error.- Parameters:
fileCache- the FileCache event occurred on.
-
convertToLong
protected static long convertToLong(String dateHeader)
-
-