Google Guava-基于泛型的使用方式

来源:互联网 发布:钢结构强度计算软件 编辑:程序博客网 时间:2024/06/09 19:34

源码:

private static Cache<String, String> cacheFormCallable = null;public static <K, V> Cache<K, V> callableCached() throws Exception {Cache<K, V> cache = CacheBuilder.newBuilder().maximumSize(10000).expireAfterWrite(10, TimeUnit.MINUTES).build();return cache;}private String getCallableCache(final String userName) {try {// Callable只有在缓存值不存在时,才会调用return cacheFormCallable.get(userName, new Callable<String>() {@Overridepublic String call() throws Exception {System.out.println(userName + " from db");return "hello " + userName + "!";}});} catch (ExecutionException e) {e.printStackTrace();return null;}}


0 0