一、google.guava
用到的包
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>20.0</version><scope>compile</scope></dependency>
写法
单位
代码
@Component
public class LocalCacheUtil {/*** 未使用N分钟过期*/private final Cache<String, Object> CACHE_EXPIRE = CacheBuilder.newBuilder().maximumSize(1000).expireAfterAccess(DConstant.EXPRESS_DURATION, DConstant.EXPRESS_TIMIMG_TIME_UNIT).build();/*** 获取不缓存对象* @param key 键* @return 对象*/public Object getExpireCache(String key){return CACHE_EXPIRE.getIfPresent(key);}/*** 存入对象* @param key 键* @param obj 对象*/public void putExpireCache(String key,Object obj){CACHE_EXPIRE.put(key,obj);}/*** 固定N分钟过期*/private final Cache<String, Object> CACHE_TIMING = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(DConstant.TIMIMG_DURATION, DConstant.TIMIMG_TIME_UNIT).build();/*** 获取不缓存对象* @param key 键* @return 对象*/public Object getTimingCache(String key){return CACHE_TIMING.getIfPresent(key);}/*** 存入对象* @param key 键* @param obj 对象*/public void putTimingCache(String key,Object obj){CACHE_TIMING.put(key,obj);}}