CacheManagerUtils.java

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;
}

}

转载于:https://www.cnblogs.com/muliu/p/6145306.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值