GuavaCachedTemplateLoader implements TemplateLoader { public static GuavaCachedTemplateLoader cacheWithExpiration( final TemplateLoader delegate, final long duration, final TimeUnit unit) { Cache<String, TemplateSource> cache = CacheBuilder.newBuilder() .expireAfterAccess(duration, unit).build(); return new GuavaCachedTemplateLoader(delegate, cache); } GuavaCachedTemplateLoader(final TemplateLoader delegate,
      final Cache<String, TemplateSource> cache); static GuavaCachedTemplateLoader cacheWithExpiration(
      final TemplateLoader delegate, final long duration, final TimeUnit unit); TemplateSource sourceAt(final String location); String resolve(final String location); String getPrefix(); String getSuffix(); void setPrefix(final String prefix); void setSuffix(final String suffix); @Override void setCharset(final Charset charset); @Override Charset getCharset();  }
@Test public void testCacheWithExpiration() throws Exception { TemplateLoader loader = new FileTemplateLoader(new File( "src/test/resources")); TemplateLoader cachedLoader = GuavaCachedTemplateLoader .cacheWithExpiration(loader, 200, TimeUnit.MILLISECONDS); assertNotNull(loader.sourceAt("template")); assertNotNull(cachedLoader.sourceAt("template")); final int TOTAL = 1000; Stopwatch sw = Stopwatch.createStarted(); for (int i = 0; i < TOTAL; i++) { loader.sourceAt("template"); } sw.stop(); out.println("Loader took: " + sw); sw.reset().start(); for (int i = 0; i < TOTAL; i++) { cachedLoader.sourceAt("template"); } out.println("Cached took: " + sw); }