cache优化

This commit is contained in:
redkale
2024-06-07 22:42:59 +08:00
parent 5e5577e685
commit 0ff3d3133b
4 changed files with 44 additions and 27 deletions

View File

@@ -56,7 +56,8 @@
<!-- <!--
全局Serivce的缓存设置没配置该节点将自动创建一个。 全局Serivce的缓存设置没配置该节点将自动创建一个。
enabled 是否开启缓存功能。默认: true enabled 是否开启缓存功能。默认: true
source: 远程CacheSource的资源名 remote: 远程CacheSource的资源名
broadcastable: 存在远程CacheSource时修改数据是否进行广播到其他集群服务中。默认: true
--> -->
<cache enabled="true" source="xxx"/> <cache enabled="true" remote="xxx" broadcastable="true"/>
``` ```

View File

@@ -27,7 +27,7 @@ public @interface Cached {
/** /**
* 缓存的key支持参数动态组合比如"key_#{id}" <br> * 缓存的key支持参数动态组合比如"key_#{id}" <br>
* '@'开头的key值视为CacheKeyGenerator对象名称 <br> * <b>'@'开头的key值视为CacheKeyGenerator对象名称</b> <br>
* *
* @see org.redkale.cache.spi.CacheKeyGenerator#name() * @see org.redkale.cache.spi.CacheKeyGenerator#name()
* *

View File

@@ -195,7 +195,15 @@ public class CacheManagerService implements CacheManager, Service {
boolean nullable, boolean nullable,
Duration expire, Duration expire,
ThrowSupplier<T> supplier) { ThrowSupplier<T> supplier) {
return getSet(this::localGetCache, this::localSetCache, hash, key, type, nullable, expire, supplier); return getSet(
(id, ex, ct) -> localSource.get(id, ct),
this::localSetCache,
hash,
key,
type,
nullable,
expire,
supplier);
} }
/** /**
@@ -219,7 +227,14 @@ public class CacheManagerService implements CacheManager, Service {
Duration expire, Duration expire,
ThrowSupplier<CompletableFuture<T>> supplier) { ThrowSupplier<CompletableFuture<T>> supplier) {
return getSetAsync( return getSetAsync(
this::localGetCacheAsync, this::localSetCacheAsync, hash, key, type, nullable, expire, supplier); (id, ex, ct) -> localSource.getAsync(id, ct),
this::localSetCacheAsync,
hash,
key,
type,
nullable,
expire,
supplier);
} }
/** /**
@@ -302,7 +317,15 @@ public class CacheManagerService implements CacheManager, Service {
boolean nullable, boolean nullable,
Duration expire, Duration expire,
ThrowSupplier<T> supplier) { ThrowSupplier<T> supplier) {
return getSet(this::remoteGetCache, this::remoteSetCache, hash, key, type, nullable, expire, supplier); return getSet(
(id, ex, ct) -> remoteSource.get(id, ct),
this::remoteSetCache,
hash,
key,
type,
nullable,
expire,
supplier);
} }
/** /**
@@ -326,7 +349,14 @@ public class CacheManagerService implements CacheManager, Service {
Duration expire, Duration expire,
ThrowSupplier<CompletableFuture<T>> supplier) { ThrowSupplier<CompletableFuture<T>> supplier) {
return getSetAsync( return getSetAsync(
this::remoteGetCacheAsync, this::remoteSetCacheAsync, hash, key, type, nullable, expire, supplier); (id, ex, ct) -> remoteSource.getAsync(id, ct),
this::remoteSetCacheAsync,
hash,
key,
type,
nullable,
expire,
supplier);
} }
/** /**
@@ -808,6 +838,7 @@ public class CacheManagerService implements CacheManager, Service {
* @param <T> 泛型 * @param <T> 泛型
* @param hash 缓存hash * @param hash 缓存hash
* @param key 缓存键 * @param key 缓存键
* @param expire 过期时长Duration.ZERO为永不过期
* @param type 数据类型 * @param type 数据类型
* @return 数据值 * @return 数据值
*/ */
@@ -833,27 +864,12 @@ public class CacheManagerService implements CacheManager, Service {
} }
} }
protected <T> CacheValue<T> localGetCache(String id, Duration expire, Type cacheType) {
return localSource.get(id, cacheType);
}
protected <T> CompletableFuture<CacheValue<T>> localGetCacheAsync(String id, Duration expire, Type cacheType) {
return localSource.getAsync(id, cacheType);
}
protected <T> CacheValue<T> remoteGetCache(String id, Duration expire, Type cacheType) {
return remoteSource.get(id, cacheType);
}
protected <T> CompletableFuture<CacheValue<T>> remoteGetCacheAsync(String id, Duration expire, Type cacheType) {
return remoteSource.getAsync(id, cacheType);
}
/** /**
* 远程异步获取缓存数据, 过期返回null * 远程异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param id 缓存键 * @param id 缓存键
* @param expire 过期时长Duration.ZERO为永不过期
* @param cacheType 数据类型 * @param cacheType 数据类型
* @return 数据值 * @return 数据值
*/ */

View File

@@ -3,11 +3,10 @@
*/ */
package org.redkale.lock; package org.redkale.lock;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.METHOD;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.redkale.service.LoadMode; import org.redkale.service.LoadMode;
@@ -15,7 +14,8 @@ import org.redkale.service.LoadMode;
* //TODO 待实现 * //TODO 待实现
* *
* <p>标记在Service的锁接口, 方法有以下限制: <br> * <p>标记在Service的锁接口, 方法有以下限制: <br>
* 2、方法必须是protected/public 3、方法不能是final/static * 1、方法必须是protected/public <br>
* 2、方法不能是final/static <br>
* *
* <p>详情见: https://redkale.org * <p>详情见: https://redkale.org
* *