1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| @Component public class LocalCacheHelper {
@Value("${localCache.expireTime:60}") private Integer expireTime;
@Value("${localCache.maxSize:2000}") private Integer maxSize;
private static Cache<String, String> cache;
@PostConstruct private void init() {
cache = CacheBuilder.newBuilder() .maximumSize(maxSize) .expireAfterWrite(expireTime, TimeUnit.MINUTES) .recordStats() .build(); }
public static <T>String get(String key, Supplier<T> supplier,UmsLogger logger) { try { logger.build("key", key); return cache.get(key, () -> { logger.info("do not use cache"); return JSON.toJSONString(supplier.get()); }); } catch (ExecutionException e) { logger.error(e.getMessage()); } return null; }
public static void put(String key, Object value) { String v = JSON.toJSONString(value); cache.put(key, v); } }
|