CachingStellarProcessor extends StellarProcessor { public static Cache<Key, Object> createCache(Map<String, Object> config) { if(config == null) { LOG.debug("Cannot create cache; missing cache configuration"); return null; } Long maxSize = getParam(config, MAX_CACHE_SIZE_PARAM, null, Long.class); if(maxSize == null || maxSize <= 0) { LOG.error("Cannot create cache; missing or invalid configuration; {} = {}", MAX_CACHE_SIZE_PARAM, maxSize); return null; } Integer maxTimeRetain = getParam(config, MAX_TIME_RETAIN_PARAM, null, Integer.class); if(maxTimeRetain == null || maxTimeRetain <= 0) { LOG.error("Cannot create cache; missing or invalid configuration; {} = {}", MAX_TIME_RETAIN_PARAM, maxTimeRetain); return null; } Caffeine<Object, Object> cache = Caffeine .newBuilder() .maximumSize(maxSize) .expireAfterWrite(maxTimeRetain, TimeUnit.MINUTES); Boolean recordStats = getParam(config, RECORD_STATS, false, Boolean.class); if(recordStats) { cache.recordStats(); } return cache.build(); }  @Override @SuppressWarnings("unchecked") Object parse(
      String expression,
      VariableResolver variableResolver,
      FunctionResolver functionResolver,
      Context context); static Cache<Key, Object> createCache(Map<String, Object> config); static String MAX_CACHE_SIZE_PARAM; static String MAX_TIME_RETAIN_PARAM; static String RECORD_STATS; }
@Test public void testInvalidMaxCacheSize() { Map<String, Object> cacheConfig = ImmutableMap.of( CachingStellarProcessor.MAX_CACHE_SIZE_PARAM, -1, CachingStellarProcessor.MAX_TIME_RETAIN_PARAM, 10 ); cache = CachingStellarProcessor.createCache(cacheConfig); assertNull(cache); }
@Test public void testMissingMaxCacheSize() { Map<String, Object> cacheConfig = ImmutableMap.of( CachingStellarProcessor.MAX_TIME_RETAIN_PARAM, 10 ); cache = CachingStellarProcessor.createCache(cacheConfig); assertNull(cache); }
@Test public void testInvalidMaxTimeRetain() { Map<String, Object> cacheConfig = ImmutableMap.of( CachingStellarProcessor.MAX_CACHE_SIZE_PARAM, 10, CachingStellarProcessor.MAX_TIME_RETAIN_PARAM, -2 ); cache = CachingStellarProcessor.createCache(cacheConfig); assertNull(cache); }
@Test public void testMissingMaxTimeRetain() { Map<String, Object> cacheConfig = ImmutableMap.of( CachingStellarProcessor.MAX_CACHE_SIZE_PARAM, 10 ); cache = CachingStellarProcessor.createCache(cacheConfig); assertNull(cache); }