CacheManagerUtils.java

时间:2022-05-21 08:23:53

package com.vcredit.framework.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
* <pre>
* Cache缓存工具
* 1. 微信调用Token、JsApiTicket要缓存7200秒,生成频率有限制不缓存会影响业务
* 2. 短信验证码缓存,有效时间5分钟
*
* </pre>
*/
public class CacheManagerUtils {
private static CacheManagerUtils cacheUtils = null;
private static final CacheManager manager = CacheManager.create();
private static final long default_time = 1800;

public static enum CACHE_TYPE {
RANDOM, WEIXIN, DEFAULT_CACHE
}

/**
* Construct
*/
private CacheManagerUtils() {
/**
* 随机码缓存
*/
if (!manager.cacheExists(CACHE_TYPE.RANDOM.name())) {
Cache cache = getNewIfNotPresent(CACHE_TYPE.RANDOM, 300);
manager.addCacheIfAbsent(cache);
}

/**
* 微信缓存 7200秒, 设置7000秒过期
*/
if (!manager.cacheExists(CACHE_TYPE.WEIXIN.name())) {
Cache cache = getNewIfNotPresent(CACHE_TYPE.WEIXIN, 7000);
manager.addCacheIfAbsent(cache);
}

/**
* 系统缓存 24h
*/
if (!manager.cacheExists(CACHE_TYPE.DEFAULT_CACHE.name())) {
Cache cache = getNewIfNotPresent(CACHE_TYPE.DEFAULT_CACHE, 3600 * 24);
manager.addCacheIfAbsent(cache);
}
}

/**
* 添加缓存值
*
* @param cacheName
* @param key
* @param value
*/
public static void addValue(CACHE_TYPE cacheName, Object key, Object value) {
getInstance();
Cache cache = getCache(cacheName);
if (cache == null) {
manager.addCache(getNewIfNotPresent(cacheName, default_time));
}
cache = getCache(cacheName);
cache.put(new Element(key, value));
}

/**
* 添加缓存值
*
* @param cacheName
* @param key
* @param value
*/
public static void addValue(CACHE_TYPE cacheName, Object key, Object value, int expires) {
getInstance();
Cache cache = getCache(cacheName);
if (cache == null) {
manager.addCache(getNewIfNotPresent(cacheName, default_time));
}
cache = getCache(cacheName);
cache.put(new Element(key, value, expires, expires));
}

/**
* 获取缓存值
*
* @param cacheName
* @param key
* @return Object
*/
public static Object getValue(CACHE_TYPE cacheName, Object key) {
getInstance();
Cache cache = getCache(cacheName);
if (cache == null || cache.get(key) == null || cache.get(key).isExpired()) {
return null;
}
return cache.get(key).getObjectValue();
}

/**
* 添加缓存值
*
* @param cacheName
* @param key
* @param value
*/
public static void addValue(Object key, Object value) {
addValue(CACHE_TYPE.DEFAULT_CACHE, key, value);
}

/**
* 获取缓存值
*
* @param cacheName
* @param key
* @return Object
*/
public static Object getValue(Object key) {
return getValue(CACHE_TYPE.DEFAULT_CACHE, key);
}

/**
* 如果缓存不存在创建缓存
*
* @param cacheName
* @param time
* @return Cache
*/
private static Cache getNewIfNotPresent(CACHE_TYPE cacheName, long time) {
String name = cacheName.name();
if (manager.cacheExists(name)) {
return manager.getCache(name);
}
return new Cache(name, 1000, false, false, time, time);
}

/**
* 获取缓存, 如果不存在返回null
*
* @param cacheName
* @return Cache
*/
private static Cache getCache(CACHE_TYPE cacheName) {
String name = cacheName.name();
return manager.getCache(name);
}

private static synchronized CacheManagerUtils getInstance() {
if (cacheUtils == null) {
cacheUtils = new CacheManagerUtils();
}
return cacheUtils;
}

}