Commit 3d08ffaa authored by yanzg's avatar yanzg

默认日期格式的支持

parent d3d9e878
package com.yanzuoguang.util.cache;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
/**
* 服务工具类
*
* @author 颜佐光
*/
public abstract class MemoryCacheNullUtil<T, M> {
/**
* 缓存的公司和数据中心Id
* 缓存一个小时
*/
private MemoryCache<M> cache = new MemoryCache<>(60 * 60);
/**
* 缓存的公司和数据中心Id
* 缓存一分钟
*/
private MemoryCache<Boolean> cacheIsNull = new MemoryCache<>(60);
public MemoryCacheNullUtil() {
}
/**
* 缓存参数
*
* @param cacheSecond
* @param nullSecond
*/
public MemoryCacheNullUtil(int cacheSecond, int nullSecond) {
cache.setClearSecond(cacheSecond);
cacheIsNull.setClearSecond(nullSecond);
}
/**
* 获取数据中心Id
*
* @param key
* @return
*/
public M getCheck(T key) {
M databaseId = get(key);
if (databaseId == null) {
throw new CodeException("没有找到" + key + "的数据");
}
return databaseId;
}
/**
* 获取数据中心Id
*
* @param key
* @return
*/
public M get(T key) {
String keyString = getKey(key);
M value = cache.get(keyString);
if (value == null) {
if (StringHelper.toBoolean(cacheIsNull.get(keyString))) {
return null;
}
value = getValue(key);
setCache(keyString, value);
}
return value;
}
/**
* 获取关键字
*
* @param key
* @return
*/
protected abstract String getKey(T key);
/**
* 获取数据
*
* @param key
* @return
*/
protected abstract M getValue(T key);
/**
* 写入缓存
*
* @param key
* @param value
*/
protected void setCache(String key, M value) {
cacheIsNull.remove(key);
cache.remove(key);
if (value == null) {
cacheIsNull.put(key, true);
} else {
cache.put(key, value);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment