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
  • Field Details

    • monitoringConfig

      protected final org.glassfish.grizzly.monitoring.DefaultMonitoringConfig<FileCacheProbe> monitoringConfig
      File cache probes
  • Constructor Details

    • FileCache

      public FileCache()
  • Method Details

    • 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 the add(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 a File resource to the cache. If a client comes with not the latest version of this resource - the FileCache will return it the latest resource version.
    • 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)
      Returns FileCacheEntry. If FileCacheEntry has been found - this method also sets correspondent HttpResponsePacket status 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 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.
    • 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 true if File resources may be be sent using FileChannel.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:
      true if resources will be sent using FileChannel.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 sending File resources via FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel). If disabled, the more traditional byte[] copy will be used to send content.
      Parameters:
      fileSendEnabled - true to enable FileChannel.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:
      getMonitoringConfig in interface org.glassfish.grizzly.monitoring.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, org.glassfish.grizzly.http.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, Throwable error)
      Notify registered FileCacheProbes about the error.
      Parameters:
      fileCache - the FileCache event occurred on.
    • convertToLong

      protected static long convertToLong(String dateHeader)