在spring中利用Guava实现本地Cache的具体配置 第二篇<二>
来源:互联网 发布:php 图片上传的原理 编辑:程序博客网 时间:2024/06/07 18:56
通过直接实现org.springframework.cache.Cache 接口来管理和实现缓存
springframework 为我们提供了一套公用的接口模版标准 本文将通过Guava工程 利用单例实现Cache 接口来完成本地缓存的配置
具体代码如下:
public class GuavaCacheUtil implements Cache { private static Logger log = LoggerFactory.getLogger(GuavaCacheUtil.class); private static final Object NULL_HOLDER = new NullHolder(); private static final String DEFAULT_NAME = "system_cache"; private final String name; private final com.google.common.cache.Cache<Object, Object> cache; private final boolean allowNullValues; private static GuavaCacheUtil guavaCacheUtil = new GuavaCacheUtil(); public static GuavaCacheUtil builder() { return guavaCacheUtil; } private GuavaCacheUtil() { this(DEFAULT_NAME, CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(1, TimeUnit.HOURS).build()); } private GuavaCacheUtil(String name, com.google.common.cache.Cache<Object, Object> cache) { this(name, cache, true); } private GuavaCacheUtil(String name, com.google.common.cache.Cache<Object, Object> cache, boolean allowNullValues) { Assert.notNull(name, "Name must not be null"); Assert.notNull(cache, "Cache must not be null"); this.name = name; this.cache = cache; this.allowNullValues = allowNullValues; } @Override public String getName() { return this.name; } @Override public Object getNativeCache() { return this.cache; } @Override public ValueWrapper get(Object key) { key = getKey(key.toString()); Object value = this.cache.getIfPresent(key); log.info("getKey = {}, getObject = {}", key, JsonUtil.objectToJson(value)); return toWrapper(value); } @Override public <T> T get(Object key, Class<T> aClass) { key = getKey(key.toString()); T value = fromStoreValue(this.cache.getIfPresent(key), aClass); log.info("getKey = {}, getObject = {}", key, JsonUtil.objectToJson(value)); return value; } @Override public <T> T get(Object o, Callable<T> callable) { return null; } @Override public void put(Object key, Object value) { key = getKey(key.toString()); this.cache.put(key, toStoreValue(value)); log.info("getKey = {}, getObject = {}", key, JsonUtil.objectToJson(value)); } @Override public ValueWrapper putIfAbsent(Object key, Object value) { PutIfAbsentCallable callable = new PutIfAbsentCallable(value); try { Object result = this.cache.get(key, callable); return (callable.called ? null : toWrapper(result)); } catch (ExecutionException e) { throw new IllegalStateException(e); } } @Override public void evict(Object key) { this.cache.invalidate(key); log.info("deleteKey = {}", key); } @Override public void clear() { this.cache.invalidateAll(); log.info("clearAll"); } private ValueWrapper toWrapper(Object value) { return (value != null ? new SimpleValueWrapper(fromStoreValue(value, null)) : null); } private static class NullHolder implements Serializable { } private Object toStoreValue(Object userValue) { if (this.allowNullValues && userValue == null) { return NULL_HOLDER; } return userValue; } private <T> T fromStoreValue(Object storeValue, Class<T> aClass) { if (this.allowNullValues && storeValue == NULL_HOLDER) { return null; } if (storeValue != null && aClass != null && !aClass.isInstance(storeValue)) { throw new IllegalStateException("Cached value is not of required type [" + aClass.getName() + "]: " + storeValue); } return (T) storeValue; } private class PutIfAbsentCallable implements Callable<Object> { private final Object value; private boolean called; public PutIfAbsentCallable(Object value) { this.value = value; } @Override public Object call() throws Exception { this.called = true; return toStoreValue(this.value); } } private String getKey(String key) { return name + ":" + key; }}
测试代码 以及测试结果:
public static String testCachePut(){ Cache cache = GuavaCacheUtil.builder(); cache.put("user_id:1","20"); String user_id = cache.get("user_id:1", String.class); System.out.println(user_id); return user_id; } public static String testCacheGet(){ Cache cache = GuavaCacheUtil.builder(); cache.put("user_id:2","30"); String user_id = cache.get("user_id:1", String.class); System.out.println(user_id); System.out.println(cache.get("user_id:2",String.class)); return user_id; } public static void main(String[] args) { testGuavaCachePut(); testGuavaCacheGet(); }
测试结果 :
0 0
- 在spring中利用Guava实现本地Cache的具体配置 第二篇<二>
- 在spring中利用Guava实现本地Cache的具体配置 第一篇<一>
- Guava cache实现本地缓存
- Guava Cache实现本地缓存
- 本地缓存实现之Guava Cache
- spring cache 中guava CacheBuilder的个性实例化
- Guava中Cache的使用
- 使用spring cacheManager配置Guava Cache和Redis Cache
- 使用spring cacheManager配置Guava Cache和Redis Cache
- spring guava cache
- Guava Cache 实现详解
- 利用Spring扫包实现发现具体的注解类
- Guava Cache的Ticker
- Guava 笔记二: cache简介
- guava实现本地缓存
- 在Eclipse中利用Gradle配置Spring
- 利用spring+ibatiS技术,在spring中配置多个数据源,并实现动态切换。
- 利用spring+ibatiS技术,在spring中配置多个数据源,并实现动态切换。
- redis 初探
- 欢迎使用CSDN-markdown编辑器
- js点击隐藏和显示div
- 矩阵连乘 备忘录
- 1.3 硬件开发板简介
- 在spring中利用Guava实现本地Cache的具体配置 第二篇<二>
- Cookie和Session的比较
- .Net 代码安全保护产品DNGuard HVM使用
- 使用AJAX完成用户名是否存在异步校验:
- 关于HTTP返回参数代表什么
- 如何让百度收录你的网站
- java压缩/解压缩zip格式文件
- 整数因子分解法
- hibernate CascadeType属性说明