优化内部缓存实现

This commit is contained in:
Charles7c 2023-08-05 19:20:34 +08:00
parent b6280b1006
commit 8f33bf9cac

View File

@ -1,12 +1,9 @@
package org.dromara.sms4j.api.smsProxy; package org.dromara.sms4j.api.smsProxy;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.universal.SmsRestrictedUtil; import org.dromara.sms4j.api.universal.SmsRestrictedUtil;
import org.dromara.sms4j.comm.exception.SmsBlendException; import org.dromara.sms4j.comm.exception.SmsBlendException;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -25,62 +22,47 @@ import java.util.concurrent.ConcurrentHashMap;
public class TimeExpiredPoolCache implements SmsRestrictedUtil { public class TimeExpiredPoolCache implements SmsRestrictedUtil {
private TimeExpiredPoolCache poolCache = TimeExpiredPoolCache.getInstance(); private TimeExpiredPoolCache poolCache = TimeExpiredPoolCache.getInstance();
/** /**
* 持久化文件格式 * 过期时间默认 24 小时
*/ */
private static final String FILE_TYPE = "persistence.data"; private static final long DEFAULT_CACHED_MILLIS = 24 * 60 * 60 * 1000L;
private static final long defaultCachedMillis = 24 * 60 * 60 * 1000L;//过期时间默认24小时 /**
private static final long timerMillis = 30 * 1000L;//定时清理默认1分钟 * 定时清理默认 1 分钟
*/
private static final long TIMER_MILLIS = 30 * 1000L;
/** /**
* 对象池 * 对象池
*/ */
private static ConcurrentHashMap<String, DataWrapper<?>> dataPool = null; private static ConcurrentHashMap<String, DataWrapper<?>> DATA_POOL = null;
/** /**
* 对象单例 * 对象单例
*/ */
private static TimeExpiredPoolCache instance = null; private static TimeExpiredPoolCache INSTANCE = null;
/** /**
* 定时器定时清理过期缓存 * 定时器定时清理过期缓存
*/ */
private static final Timer timer = new Timer(); private static final Timer TIMER = new Timer();
private TimeExpiredPoolCache() { private TimeExpiredPoolCache() {
} }
private static synchronized void syncInit() { private static synchronized void syncInit() {
if (instance == null) { if (INSTANCE == null) {
instance = new TimeExpiredPoolCache(); INSTANCE = new TimeExpiredPoolCache();
dataPool = new ConcurrentHashMap<>(); DATA_POOL = new ConcurrentHashMap<>();
initTimer(); initTimer();
} }
} }
public static TimeExpiredPoolCache getInstance() { public static TimeExpiredPoolCache getInstance() {
if (instance == null) { if (INSTANCE == null) {
syncInit(); syncInit();
} }
return instance; return INSTANCE;
} }
/**
* 读取持久化文件
*/
// private static boolean persistenceInit() {
// String path = FileTool.getPath() + FILE_TYPE;
// try {
// DataWrapper d = JSONUtil.toBean(FileTool.readFile(path), DataWrapper.class);
// if (dataPool != null) {
// return true;
// }
// } catch (IOException e) {
// log.error(e.getMessage());
// }
// return false;
// }
private static void initTimer() { private static void initTimer() {
timer.scheduleAtFixedRate(new TimerTask() { TIMER.scheduleAtFixedRate(new TimerTask() {
@Override @Override
public void run() { public void run() {
try { try {
@ -90,29 +72,21 @@ public class TimeExpiredPoolCache implements SmsRestrictedUtil {
throw new SmsBlendException(e.getMessage()); throw new SmsBlendException(e.getMessage());
} }
} }
}, timerMillis, timerMillis); }, TIMER_MILLIS, TIMER_MILLIS);
} }
/** 写入持久化文件*/
// private static void persistence() {
// String path = FileTool.getPath() + FILE_TYPE;
// FileTool.createFile(path);
// FileTool.writeFile(new File(path), JSONUtil.toJsonStr(dataPool), false);
// }
/** /**
* 清除过期的缓存 * 清除过期的缓存
*/ */
private static void clearExpiredCaches() { private static void clearExpiredCaches() {
List<String> expiredKeyList = new LinkedList<>(); List<String> expiredKeyList = new LinkedList<>();
for (Entry<String, DataWrapper<?>> entry : DATA_POOL.entrySet()) {
for (Entry<String, DataWrapper<?>> entry : dataPool.entrySet()) {
if (entry.getValue().isExpired()) { if (entry.getValue().isExpired()) {
expiredKeyList.add(entry.getKey()); expiredKeyList.add(entry.getKey());
} }
} }
for (String key : expiredKeyList) { for (String key : expiredKeyList) {
dataPool.remove(key); DATA_POOL.remove(key);
} }
} }
@ -126,7 +100,7 @@ public class TimeExpiredPoolCache implements SmsRestrictedUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T put(String key, T data, long cachedMillis, DataRenewer<T> dataRenewer) throws Exception { public <T> T put(String key, T data, long cachedMillis, DataRenewer<T> dataRenewer) throws Exception {
DataWrapper<T> dataWrapper = (DataWrapper<T>) dataPool.get(key); DataWrapper<T> dataWrapper = (DataWrapper<T>) DATA_POOL.get(key);
if (data == null && dataRenewer != null) { if (data == null && dataRenewer != null) {
data = dataRenewer.renewData(); data = dataRenewer.renewData();
} }
@ -139,7 +113,7 @@ public class TimeExpiredPoolCache implements SmsRestrictedUtil {
dataWrapper.update(data, cachedMillis); dataWrapper.update(data, cachedMillis);
} else { } else {
dataWrapper = new DataWrapper<>(data, cachedMillis); dataWrapper = new DataWrapper<>(data, cachedMillis);
dataPool.put(key, dataWrapper); DATA_POOL.put(key, dataWrapper);
} }
return data; return data;
} }
@ -149,13 +123,13 @@ public class TimeExpiredPoolCache implements SmsRestrictedUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T put(String key, T data, long cachedMillis) throws Exception { public <T> T put(String key, T data, long cachedMillis) throws Exception {
DataWrapper<T> dataWrapper = (DataWrapper<T>) dataPool.get(key); DataWrapper<T> dataWrapper = (DataWrapper<T>) DATA_POOL.get(key);
if (dataWrapper != null) { if (dataWrapper != null) {
//更新 //更新
dataWrapper.update(data, cachedMillis); dataWrapper.update(data, cachedMillis);
} else { } else {
dataWrapper = new DataWrapper<T>(data, cachedMillis); dataWrapper = new DataWrapper<T>(data, cachedMillis);
dataPool.put(key, dataWrapper); DATA_POOL.put(key, dataWrapper);
} }
return data; return data;
} }
@ -165,7 +139,7 @@ public class TimeExpiredPoolCache implements SmsRestrictedUtil {
*/ */
@Deprecated @Deprecated
public <T> T put(String key, T data, DataRenewer<T> dataRenewer) throws Exception { public <T> T put(String key, T data, DataRenewer<T> dataRenewer) throws Exception {
return put(key, data, defaultCachedMillis, dataRenewer); return put(key, data, DEFAULT_CACHED_MILLIS, dataRenewer);
} }
/** /**
@ -173,7 +147,7 @@ public class TimeExpiredPoolCache implements SmsRestrictedUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T get(String key, long cachedMillis, DataRenewer<T> dataRenewer) throws Exception { public <T> T get(String key, long cachedMillis, DataRenewer<T> dataRenewer) throws Exception {
DataWrapper<T> dataWrapper = (DataWrapper<T>) dataPool.get(key); DataWrapper<T> dataWrapper = (DataWrapper<T>) DATA_POOL.get(key);
if (dataWrapper != null && !dataWrapper.isExpired()) { if (dataWrapper != null && !dataWrapper.isExpired()) {
return dataWrapper.data; return dataWrapper.data;
} }
@ -182,31 +156,29 @@ public class TimeExpiredPoolCache implements SmsRestrictedUtil {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T get(String key) { public <T> T get(String key) {
DataWrapper<T> dataWrapper = (DataWrapper<T>) dataPool.get(key); DataWrapper<T> dataWrapper = (DataWrapper<T>) DATA_POOL.get(key);
if (dataWrapper != null && !dataWrapper.isExpired()) { if (dataWrapper != null && !dataWrapper.isExpired()) {
return dataWrapper.data; return dataWrapper.data;
} }
return null; return null;
} }
/** @Override
* 清除缓存 public void clean() throws RuntimeException {
*/ DATA_POOL.clear();
public void clear() {
dataPool.clear();
} }
/** /**
* 删除指定key的value * 删除指定key的value
*/ */
public void remove(String key) { public void remove(String key) {
dataPool.remove(key); DATA_POOL.remove(key);
} }
@Override @Override
public boolean setOrTime(String key, Object value, Long time) { public boolean setOrTime(String key, Object value, Long time) {
try { try {
poolCache.put(key,value,time); poolCache.put(key, value, time);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -216,7 +188,7 @@ public class TimeExpiredPoolCache implements SmsRestrictedUtil {
@Override @Override
public boolean set(String key, Object value) { public boolean set(String key, Object value) {
try { try {
poolCache.put(key,value,defaultCachedMillis); poolCache.put(key, value, DEFAULT_CACHED_MILLIS);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -228,11 +200,6 @@ public class TimeExpiredPoolCache implements SmsRestrictedUtil {
return null; return null;
} }
@Override
public void clean() throws RuntimeException {
//TODO
}
/** /**
* 数据构造 * 数据构造
*/ */
@ -281,5 +248,4 @@ public class TimeExpiredPoolCache implements SmsRestrictedUtil {
return true; return true;
} }
} }
} }