CacheManager

This commit is contained in:
redkale
2023-12-11 16:33:57 +08:00
parent c008dfdfd7
commit 8780365e17

View File

@@ -226,7 +226,11 @@ public class CacheManagerService implements CacheManager, Service {
if (val != null && !val.isExpired()) { if (val != null && !val.isExpired()) {
return val.getValue(); return val.getValue();
} }
if (remoteSource != null) {
return CacheValue.get(remoteSource.hget(hash, key, t)); return CacheValue.get(remoteSource.hget(hash, key, t));
} else {
return null;
}
} }
/** /**
@@ -245,8 +249,12 @@ public class CacheManagerService implements CacheManager, Service {
if (val != null && !val.isExpired()) { if (val != null && !val.isExpired()) {
return CompletableFuture.completedFuture(val.getValue()); return CompletableFuture.completedFuture(val.getValue());
} }
if (remoteSource != null) {
CompletableFuture<CacheValue<T>> future = remoteSource.hgetAsync(hash, key, t); CompletableFuture<CacheValue<T>> future = remoteSource.hgetAsync(hash, key, t);
return future.thenApply(CacheValue::get); return future.thenApply(CacheValue::get);
} else {
return CompletableFuture.completedFuture(null);
}
} }
/** /**
@@ -263,8 +271,10 @@ public class CacheManagerService implements CacheManager, Service {
public <T> void bothSet(final String hash, final String key, final Type type, final T value, Duration localExpire, Duration remoteExpire) { public <T> void bothSet(final String hash, final String key, final Type type, final T value, Duration localExpire, Duration remoteExpire) {
Type t = loadCacheType(type, value); Type t = loadCacheType(type, value);
localSource.hset(hash, key, t, CacheValue.create(value, localExpire)); localSource.hset(hash, key, t, CacheValue.create(value, localExpire));
if (remoteSource != null) {
remoteSource.hset(hash, key, t, CacheValue.create(value, remoteExpire)); remoteSource.hset(hash, key, t, CacheValue.create(value, remoteExpire));
} }
}
/** /**
* 远程异步缓存数据 * 远程异步缓存数据
@@ -280,7 +290,11 @@ public class CacheManagerService implements CacheManager, Service {
public <T> CompletableFuture<Void> bothSetAsync(String hash, String key, Type type, T value, Duration localExpire, Duration remoteExpire) { public <T> CompletableFuture<Void> bothSetAsync(String hash, String key, Type type, T value, Duration localExpire, Duration remoteExpire) {
Type t = loadCacheType(type, value); Type t = loadCacheType(type, value);
localSource.hset(hash, key, t, CacheValue.create(value, localExpire)); localSource.hset(hash, key, t, CacheValue.create(value, localExpire));
if (remoteSource != null) {
return remoteSource.hsetAsync(hash, key, t, CacheValue.create(value, remoteExpire)); return remoteSource.hsetAsync(hash, key, t, CacheValue.create(value, remoteExpire));
} else {
return CompletableFuture.completedFuture(null);
}
} }
/** /**
@@ -292,8 +306,12 @@ public class CacheManagerService implements CacheManager, Service {
* @return 删除数量 * @return 删除数量
*/ */
public long bothDel(String hash, String key) { public long bothDel(String hash, String key) {
localSource.hdel(hash, key); long v = localSource.hdel(hash, key);
if (remoteSource != null) {
return remoteSource.hdel(hash, key); return remoteSource.hdel(hash, key);
} else {
return v;
}
} }
/** /**
@@ -305,8 +323,12 @@ public class CacheManagerService implements CacheManager, Service {
* @return 删除数量 * @return 删除数量
*/ */
public CompletableFuture<Long> bothDelAsync(String hash, String key) { public CompletableFuture<Long> bothDelAsync(String hash, String key) {
localSource.hdel(hash, key); long v = localSource.hdel(hash, key);
if (remoteSource != null) {
return remoteSource.hdelAsync(hash, key); return remoteSource.hdelAsync(hash, key);
} else {
return CompletableFuture.completedFuture(v);
}
} }
//-------------------------------------- 内部方法 -------------------------------------- //-------------------------------------- 内部方法 --------------------------------------