diff --git a/src/main/java/org/redkale/caching/CacheExpire.java b/src/main/java/org/redkale/caching/CacheExpire.java
new file mode 100644
index 000000000..fd548038a
--- /dev/null
+++ b/src/main/java/org/redkale/caching/CacheExpire.java
@@ -0,0 +1,44 @@
+/*
+ *
+ */
+package org.redkale.caching;
+
+import org.redkale.convert.ConvertColumn;
+import org.redkale.convert.ConvertDisabled;
+import org.redkale.convert.json.JsonConvert;
+
+/**
+ *
+ * 缓存过期对象
+ *
+ *
+ * 详情见: https://redkale.org
+ *
+ * @author zhangjx
+ * @param 泛型
+ *
+ * @since 2.8.0
+ */
+public class CacheExpire {
+
+ //为0表示不过期
+ @ConvertColumn(index = 1)
+ protected long time;
+
+ @ConvertDisabled
+ public boolean isExpired() {
+ return time > 0 && System.currentTimeMillis() > time;
+ }
+
+ public long getTime() {
+ return time;
+ }
+
+ public void setTime(long time) {
+ this.time = time;
+ }
+
+ public String toString() {
+ return JsonConvert.root().convertTo(this);
+ }
+}
diff --git a/src/main/java/org/redkale/caching/CacheManagerService.java b/src/main/java/org/redkale/caching/CacheManagerService.java
index 611537044..f896fc7ac 100644
--- a/src/main/java/org/redkale/caching/CacheManagerService.java
+++ b/src/main/java/org/redkale/caching/CacheManagerService.java
@@ -8,6 +8,7 @@ import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentSkipListSet;
import org.redkale.annotation.AutoLoad;
import org.redkale.annotation.Component;
import org.redkale.annotation.Nonnull;
@@ -39,6 +40,9 @@ public class CacheManagerService implements CacheManager, Service {
//本地缓存Source
protected final CacheMemorySource localSource = new CacheMemorySource("caching");
+ //缓存hash集合, 用于定时遍历删除过期数据
+ protected final ConcurrentSkipListSet hashNames = new ConcurrentSkipListSet<>();
+
//远程缓存Source
protected CacheSource remoteSource;
@@ -61,21 +65,26 @@ public class CacheManagerService implements CacheManager, Service {
this.localSource.destroy(conf);
}
+ public CacheManagerService addHash(String hash) {
+ this.hashNames.add(hash);
+ return this;
+ }
+
//-------------------------------------- 本地缓存 --------------------------------------
/**
* 本地获取缓存数据, 过期返回null
*
* @param 泛型
- * @param map 缓存hash
+ * @param hash 缓存hash
* @param key 缓存键
* @param type 数据类型
*
* @return 数据值
*/
@Override
- public T localGet(final String map, final String key, final Type type) {
+ public T localGet(final String hash, final String key, final Type type) {
Type t = loadCacheType(type);
- CacheValue val = localSource.hget(map, key, t);
+ CacheValue val = localSource.hget(hash, key, t);
return CacheValue.get(val);
}
@@ -83,30 +92,30 @@ public class CacheManagerService implements CacheManager, Service {
* 本地缓存数据
*
* @param 泛型
- * @param map 缓存hash
+ * @param hash 缓存hash
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @param expire 过期时长,为null表示永不过期
*/
@Override
- public void localSet(String map, String key, Type type, T value, Duration expire) {
+ public void localSet(String hash, String key, Type type, T value, Duration expire) {
Type t = loadCacheType(type, value);
CacheValue val = CacheValue.create(value, expire);
- localSource.hset(map, key, t, val);
+ localSource.hset(hash, key, t, val);
}
/**
* 本地删除缓存数据
*
- * @param map 缓存hash
- * @param key 缓存键
+ * @param hash 缓存hash
+ * @param key 缓存键
*
* @return 删除数量
*/
@Override
- public long localDel(String map, String key) {
- return localSource.hdel(map, key);
+ public long localDel(String hash, String key) {
+ return localSource.hdel(hash, key);
}
//-------------------------------------- 远程缓存 --------------------------------------
@@ -114,16 +123,16 @@ public class CacheManagerService implements CacheManager, Service {
* 远程获取缓存数据, 过期返回null
*
* @param 泛型
- * @param map 缓存hash
+ * @param hash 缓存hash
* @param key 缓存键
* @param type 数据类型
*
* @return 数据值
*/
@Override
- public T remoteGet(final String map, final String key, final Type type) {
+ public T remoteGet(final String hash, final String key, final Type type) {
Type t = loadCacheType(type);
- CacheValue val = remoteSource.hget(map, key, t);
+ CacheValue val = remoteSource.hget(hash, key, t);
return CacheValue.get(val);
}
@@ -131,16 +140,16 @@ public class CacheManagerService implements CacheManager, Service {
* 远程异步获取缓存数据, 过期返回null
*
* @param 泛型
- * @param map 缓存hash
+ * @param hash 缓存hash
* @param key 缓存键
* @param type 数据类型
*
* @return 数据值
*/
@Override
- public CompletableFuture remoteGetAsync(final String map, final String key, final Type type) {
+ public CompletableFuture remoteGetAsync(final String hash, final String key, final Type type) {
Type t = loadCacheType(type);
- CompletableFuture> future = remoteSource.hgetAsync(map, key, t);
+ CompletableFuture> future = remoteSource.hgetAsync(hash, key, t);
return future.thenApply(CacheValue::get);
}
@@ -148,56 +157,56 @@ public class CacheManagerService implements CacheManager, Service {
* 远程缓存数据
*
* @param 泛型
- * @param map 缓存hash
+ * @param hash 缓存hash
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @param expire 过期时长,为null表示永不过期
*/
- public void remoteSet(final String map, final String key, final Type type, final T value, Duration expire) {
+ public void remoteSet(final String hash, final String key, final Type type, final T value, Duration expire) {
Type t = loadCacheType(type, value);
CacheValue val = CacheValue.create(value, expire);
- remoteSource.hset(map, key, t, val);
+ remoteSource.hset(hash, key, t, val);
}
/**
* 远程异步缓存数据
*
* @param 泛型
- * @param map 缓存hash
+ * @param hash 缓存hash
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @param expire 过期时长,为null表示永不过期
*/
- public CompletableFuture remoteSetAsync(String map, String key, Type type, T value, Duration expire) {
+ public CompletableFuture remoteSetAsync(String hash, String key, Type type, T value, Duration expire) {
Type t = loadCacheType(type, value);
CacheValue val = CacheValue.create(value, expire);
- return remoteSource.hsetAsync(map, key, t, val);
+ return remoteSource.hsetAsync(hash, key, t, val);
}
/**
* 远程删除缓存数据
*
- * @param map 缓存hash
- * @param key 缓存键
+ * @param hash 缓存hash
+ * @param key 缓存键
*
* @return 删除数量
*/
- public long remoteDel(String map, String key) {
- return remoteSource.hdel(map, key);
+ public long remoteDel(String hash, String key) {
+ return remoteSource.hdel(hash, key);
}
/**
* 远程异步删除缓存数据
*
- * @param map 缓存hash
- * @param key 缓存键
+ * @param hash 缓存hash
+ * @param key 缓存键
*
* @return 删除数量
*/
- public CompletableFuture remoteDelAsync(String map, String key) {
- return remoteSource.hdelAsync(map, key);
+ public CompletableFuture remoteDelAsync(String hash, String key) {
+ return remoteSource.hdelAsync(hash, key);
}
//-------------------------------------- both缓存 --------------------------------------
@@ -205,38 +214,38 @@ public class CacheManagerService implements CacheManager, Service {
* 远程获取缓存数据, 过期返回null
*
* @param 泛型
- * @param map 缓存hash
+ * @param hash 缓存hash
* @param key 缓存键
* @param type 数据类型
*
* @return 数据值
*/
- public T bothGet(final String map, final String key, final Type type) {
+ public T bothGet(final String hash, final String key, final Type type) {
Type t = loadCacheType(type);
- CacheValue val = localSource.hget(map, key, t);
+ CacheValue val = localSource.hget(hash, key, t);
if (val != null && !val.isExpired()) {
return val.getValue();
}
- return CacheValue.get(remoteSource.hget(map, key, t));
+ return CacheValue.get(remoteSource.hget(hash, key, t));
}
/**
* 远程异步获取缓存数据, 过期返回null
*
* @param 泛型
- * @param map 缓存hash
+ * @param hash 缓存hash
* @param key 缓存键
* @param type 数据类型
*
* @return 数据值
*/
- public CompletableFuture bothGetAsync(final String map, final String key, final Type type) {
+ public CompletableFuture bothGetAsync(final String hash, final String key, final Type type) {
Type t = loadCacheType(type);
- CacheValue val = localSource.hget(map, key, t);
+ CacheValue val = localSource.hget(hash, key, t);
if (val != null && !val.isExpired()) {
return CompletableFuture.completedFuture(val.getValue());
}
- CompletableFuture> future = remoteSource.hgetAsync(map, key, t);
+ CompletableFuture> future = remoteSource.hgetAsync(hash, key, t);
return future.thenApply(CacheValue::get);
}
@@ -244,60 +253,60 @@ public class CacheManagerService implements CacheManager, Service {
* 远程缓存数据
*
* @param 泛型
- * @param map 缓存hash
+ * @param hash 缓存hash
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @param localExpire 本地过期时长,为null表示永不过期
* @param remoteExpire 远程过期时长,为null表示永不过期
*/
- public void bothSet(final String map, final String key, final Type type, final T value, Duration localExpire, Duration remoteExpire) {
+ public void bothSet(final String hash, final String key, final Type type, final T value, Duration localExpire, Duration remoteExpire) {
Type t = loadCacheType(type, value);
- localSource.hset(map, key, t, CacheValue.create(value, localExpire));
- remoteSource.hset(map, key, t, CacheValue.create(value, remoteExpire));
+ localSource.hset(hash, key, t, CacheValue.create(value, localExpire));
+ remoteSource.hset(hash, key, t, CacheValue.create(value, remoteExpire));
}
/**
* 远程异步缓存数据
*
* @param 泛型
- * @param map 缓存hash
+ * @param hash 缓存hash
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @param localExpire 本地过期时长,为null表示永不过期
* @param remoteExpire 远程过期时长,为null表示永不过期
*/
- public CompletableFuture bothSetAsync(String map, String key, Type type, T value, Duration localExpire, Duration remoteExpire) {
+ public CompletableFuture bothSetAsync(String hash, String key, Type type, T value, Duration localExpire, Duration remoteExpire) {
Type t = loadCacheType(type, value);
- localSource.hset(map, key, t, CacheValue.create(value, localExpire));
- return remoteSource.hsetAsync(map, key, t, CacheValue.create(value, remoteExpire));
+ localSource.hset(hash, key, t, CacheValue.create(value, localExpire));
+ return remoteSource.hsetAsync(hash, key, t, CacheValue.create(value, remoteExpire));
}
/**
* 远程删除缓存数据
*
- * @param map 缓存hash
- * @param key 缓存键
+ * @param hash 缓存hash
+ * @param key 缓存键
*
* @return 删除数量
*/
- public long bothDel(String map, String key) {
- localSource.hdel(map, key);
- return remoteSource.hdel(map, key);
+ public long bothDel(String hash, String key) {
+ localSource.hdel(hash, key);
+ return remoteSource.hdel(hash, key);
}
/**
* 远程异步删除缓存数据
*
- * @param map 缓存hash
- * @param key 缓存键
+ * @param hash 缓存hash
+ * @param key 缓存键
*
* @return 删除数量
*/
- public CompletableFuture bothDelAsync(String map, String key) {
- localSource.hdel(map, key);
- return remoteSource.hdelAsync(map, key);
+ public CompletableFuture bothDelAsync(String hash, String key) {
+ localSource.hdel(hash, key);
+ return remoteSource.hdelAsync(hash, key);
}
//-------------------------------------- 内部方法 --------------------------------------
diff --git a/src/main/java/org/redkale/caching/CacheValue.java b/src/main/java/org/redkale/caching/CacheValue.java
index 89118fd13..b3d1cfdf9 100644
--- a/src/main/java/org/redkale/caching/CacheValue.java
+++ b/src/main/java/org/redkale/caching/CacheValue.java
@@ -5,7 +5,6 @@ package org.redkale.caching;
import java.time.Duration;
import org.redkale.convert.ConvertColumn;
-import org.redkale.convert.ConvertDisabled;
import org.redkale.convert.json.JsonConvert;
/**
@@ -20,14 +19,10 @@ import org.redkale.convert.json.JsonConvert;
*
* @since 2.8.0
*/
-public class CacheValue {
+public class CacheValue extends CacheExpire {
- @ConvertColumn(index = 1)
- private T value;
-
- //为0表示不过期
@ConvertColumn(index = 2)
- private long time;
+ private T value;
public CacheValue() {
}
@@ -45,11 +40,6 @@ public class CacheValue {
return val != null && !val.isExpired() ? (T) val.getValue() : null;
}
- @ConvertDisabled
- public boolean isExpired() {
- return time > 0 && System.currentTimeMillis() > time;
- }
-
public T getValue() {
return value;
}
@@ -58,14 +48,6 @@ public class CacheValue {
this.value = value;
}
- public long getTime() {
- return time;
- }
-
- public void setTime(long time) {
- this.time = time;
- }
-
public String toString() {
return JsonConvert.root().convertTo(this);
}