Fork me on GitHub

Caching data

The compiler can cache:

  • nothing
  • compiled code
  • source code
  • source and compiled code

The compiler of the relevant caching strategy can be created by (all located in the package biz.gabrys.lesscss.extended.compiler):

Builders API simplifies creating compilers. After creating a new builder you can instantly call create() method (it fill not configured object with default implementations).

// creates compiler without cache
ExtendedCompiler compiler = new NonCachingExtendedCompilerBuilder().create();

// creates cache
FullCache cache = new FullCacheBuilder().withDirectory(new File("/less-cache")).create();
// creates compilers with cache
compiler = new CachingCompiledCodeExtendedCompilerBuilder(cache).create();
compiler = new CachingSourceCodeExtendedCompilerBuilder(cache).create();
compiler = new CachingSourceAndCompiledCodeExtendedCompilerBuilder(cache).create();

Caching compiled code

To reduce the complexity of the mechanism that determines whether the compiler should compile source or return code from the cache, it does not take into account changes in the compiler options (parameters).

// creates compiler & source file
FullCache cache = new FullCacheBuilder().withDirectory(new File("/less-cache")).create();
ExtendedCompiler compiler = new CachingCompiledCodeExtendedCompilerBuilder(cache).create();
LessSource source = new LocalSource(new File("/less/file.less"), "UTF-8");

// compiles file with default options
String cssCode = compiler.compile(source);

// sets custom option: minify CSS code
CompilerOptions options = new CompilerOptionsBuilder().setMinified(true).create();
// returns compiled code from cache
String cssCode2 = compiler.compile(source, options);
cssCode.equals(cssCode2); // true

You can force the source compilation by deleting cache entry:

// creates compiler & source file
FullCache cache = new FullCacheBuilder().withDirectory(new File("/less-cache")).create();
ExtendedCompiler compiler = new CachingCompiledCodeExtendedCompilerBuilder(cache).create();
LessSource source = new LocalSource(new File("/less/file.less"), "UTF-8");

// compiles file with default options
String cssCode = compiler.compile(source);

cache.deleteAll(); // or cache.delete(source);

// sets custom option: minify CSS code
CompilerOptions options = new CompilerOptionsBuilder().setMinified(true).create();
// compiles file + minification
String cssCode2 = compiler.compile(source, options);
cssCode.equals(cssCode2); // false