Class MinifiedScriptCache

java.lang.Object
org.gatein.portal.controller.resource.MinifiedScriptCache

public class MinifiedScriptCache extends Object
Minified scripts kept on disk, so that a restart or a reload minifies only the scripts whose source changed, and again those the compiler rejected. A file's name is the SHA-256 of the script's source, of Closure's release version and of the compiler options, so its content is the minification of that source, whichever thread or node wrote it. A build that reads a file makes it recent: deleteUnused(), once the startup build is done, deletes the files no build read for RETENTION.
  • Field Details

    • CACHE_DIR_PROPERTY

      public static final String CACHE_DIR_PROPERTY
      Where minified scripts are kept across restarts. Its files are served as the platform's scripts: only the server's account may write in it.
      See Also:
    • RETENTION

      public static final Duration RETENTION
      Files no build read for that long are deleted after the startup build.
  • Constructor Details

    • MinifiedScriptCache

      public MinifiedScriptCache(String configuredDirectory, String compilerOptions)
      Parameters:
      configuredDirectory - the directory to use; when blank, or when it cannot be created or written, a temporary directory deleted when the JVM exits
      compilerOptions - the compiler options, as part of each file's key
  • Method Details

    • configuredDirectory

      public static String configuredDirectory()
      Returns:
      the directory CACHE_DIR_PROPERTY names; when it is not set, ${gatein.data.dir}/js-cache; an empty value, or no data directory, leaves it empty
    • get

      public String get(String source, UnaryOperator<String> minifier)
      Parameters:
      source - a script's source
      minifier - minifies a source, or returns null when it cannot
      Returns:
      the minified script, read from the directory when a previous minification of the same source left it there, else the minifier's result, written to the directory when not null. An empty file for a non-blank source is a miss: a write a host crash cut short leaves one
    • getDirectory

      public Path getDirectory()
    • isTemporary

      public boolean isTemporary()
    • deleteUnused

      public void deleteUnused()
      Deletes the cache's files that no build read for RETENTION. Run once the startup build has read the scripts it serves: their files are then recent, however long ago the previous build ran.