CachedActionFunc

This commit is contained in:
redkale
2024-06-13 15:23:12 +08:00
parent 892e09cb53
commit 4411a32522
11 changed files with 127 additions and 15 deletions

View File

@@ -51,6 +51,28 @@
}
```
## 动态调整缓存时长
  使用```@Resource```注入多个```CachedManager```
```java
@Resource
private CachedManager cachedManager;
@Cached(key = "#{code}_#{map.id}", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS)
public String getName(String code, Map<String, Long> map) {
return code + "-" + map;
}
public void updateExpire() {
Duration expire = Duration.ofMillis(600);
cachedManager.acceptCachedAction("#{code}_#{map.id}", action -> {
//将缓存时长改成600毫秒并开启本地缓存
action.setLocalExpire(expire);
action.setRemoteExpire(expire);
});
}
```
## 缓存配置
```xml
<!--

View File

@@ -5,7 +5,10 @@ package org.redkale.cached;
import java.lang.reflect.Type;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import org.redkale.cached.spi.CachedAction;
import org.redkale.inject.Resourcable;
import org.redkale.util.ThrowSupplier;
@@ -58,6 +61,22 @@ public interface CachedManager extends Resourcable {
}
}
/**
* 获取{@link org.redkale.cached.spi.CachedAction}集合
*
* @return CachedAction集合
*/
public List<CachedAction> getCachedActions();
/**
* 处理指定缓存key的{@link org.redkale.cached.spi.CachedAction}<br>
* 可用于动态调整缓存时长
*
* @param templetKey 缓存key
* @param consumer 处理函数
*/
public void acceptCachedAction(String templetKey, Consumer<CachedAction> consumer);
// -------------------------------------- 本地缓存 --------------------------------------
/**
* 本地获取缓存数据, 过期返回null

View File

@@ -63,7 +63,7 @@ public class CachedAction {
private final String[] paramNames;
// 模板key
String templetKey;
final String templetKey;
// 缓存key生成器
private CachedKeyGenerator keyGenerator;
@@ -91,10 +91,9 @@ public class CachedAction {
this.resultType = this.async ? ((ParameterizedType) returnType).getActualTypeArguments()[0] : returnType;
}
void init(ResourceFactory resourceFactory, Object service) {
String init(ResourceFactory resourceFactory, Object service) {
this.manager = resourceFactory.load(cached.getManager(), CachedManager.class);
String key = environment.getPropertyValue(cached.getKey());
this.templetKey = key;
final String key = environment.getPropertyValue(cached.getKey());
if (key.startsWith("@")) { // 动态加载缓存key生成器
String generatorName = key.substring(1);
this.keyGenerator = resourceFactory.findChild(generatorName, CachedKeyGenerator.class);
@@ -104,6 +103,8 @@ public class CachedAction {
}
this.localExpire = createDuration(cached.getLocalExpire());
this.remoteExpire = createDuration(cached.getRemoteExpire());
((CachedActionFunc) this.manager).addAction(this);
return key;
}
@ClassDepends
@@ -160,6 +161,26 @@ public class CachedAction {
return method;
}
public String getTempletKey() {
return templetKey;
}
public Duration getLocalExpire() {
return localExpire;
}
public void setLocalExpire(Duration localExpire) {
this.localExpire = localExpire;
}
public Duration getRemoteExpire() {
return remoteExpire;
}
public void setRemoteExpire(Duration remoteExpire) {
this.remoteExpire = remoteExpire;
}
@Override
public String toString() {
return "{"

View File

@@ -0,0 +1,17 @@
/*
*/
package org.redkale.cached.spi;
/**
* 增加 {@link org.redkale.cached.spi.CachedAction} {@link org.redkale.cached.CachedManager}的实现类也必须实现本接口
*
* <p>详情见: https://redkale.org
*
* @author zhangjx
* @since 2.8.0
*/
public interface CachedActionFunc {
void addAction(CachedAction action);
}

View File

@@ -237,9 +237,9 @@ public class CachedAsmMethodBoost extends AsmMethodBoost {
actionMap.forEach((field, action) -> {
try {
resourceFactory.inject(action);
action.init(resourceFactory, service);
if (action.templetKey.indexOf('@') < 0
&& action.templetKey.indexOf('{') < 0
final String tkey = action.init(resourceFactory, service);
if (tkey.indexOf('@') < 0
&& tkey.indexOf('{') < 0
&& action.getMethod().getParameterCount() > 0) {
// 一般有参数的方法Cached.key应该是动态的
logger.log(

View File

@@ -5,9 +5,13 @@ package org.redkale.cached.spi;
import java.lang.reflect.Type;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -40,7 +44,7 @@ import org.redkale.util.TypeToken;
@Component
@AutoLoad(false)
@ResourceType(CachedManager.class)
public class CachedManagerService implements CachedManager, Service {
public class CachedManagerService implements CachedManager, CachedActionFunc, Service {
public static final String CACHED_CHANNEL_TOPIC_PREFIX = "cached-update-channel";
@@ -75,6 +79,8 @@ public class CachedManagerService implements CachedManager, Service {
// 缓存无效时使用的异步锁
private final ConcurrentHashMap<String, CachedAsyncLock> asyncLockMap = new ConcurrentHashMap<>();
protected final List<CachedAction> actions = new CopyOnWriteArrayList<>();
@Resource(required = false)
protected Application application;
@@ -173,10 +179,37 @@ public class CachedManagerService implements CachedManager, Service {
}
}
@Override
public void addAction(CachedAction action) {
actions.add(action);
}
/**
* 获取{@link org.redkale.cached.spi.CachedAction}集合
*
* @return CachedAction集合
*/
@Override
public List<CachedAction> getCachedActions() {
return new ArrayList<>(actions);
}
/**
* 处理指定缓存key的{@link org.redkale.cached.spi.CachedAction}
*
* @param templetKey 缓存key
* @param consumer 处理函数
*/
@Override
public void acceptCachedAction(String templetKey, Consumer<CachedAction> consumer) {
actions.stream()
.filter(v -> Objects.equals(v.getTempletKey(), templetKey))
.forEach(consumer);
}
@Override
public String toString() {
return getClass().getSimpleName() + "_" + Objects.hash(this) + "{name='" + name + "', schema='" + schema
+ "'}";
return getClass().getSimpleName() + "_" + Objects.hash(this) + "{name='" + name + "', schema='" + schema + "'}";
}
// -------------------------------------- 本地缓存 --------------------------------------

View File

@@ -17,7 +17,7 @@ import java.lang.annotation.Target;
* <p>详情见: https://redkale.org
*
* @since 2.8.0
* @org.redkale.net.http.RestMapping
* @see org.redkale.net.http.RestMapping
* @see org.redkale.net.http.RestService
* @author zhangjx
*/

View File

@@ -17,7 +17,7 @@ import java.lang.annotation.Target;
* <p>详情见: https://redkale.org
*
* @since 2.8.0
* @org.redkale.net.http.RestMapping
* @see org.redkale.net.http.RestMapping
* @see org.redkale.net.http.RestService
* @author zhangjx
*/

View File

@@ -17,7 +17,7 @@ import java.lang.annotation.Target;
* <p>详情见: https://redkale.org
*
* @since 2.8.0
* @org.redkale.net.http.RestMapping
* @see org.redkale.net.http.RestMapping
* @see org.redkale.net.http.RestService
* @author zhangjx
*/

View File

@@ -17,7 +17,7 @@ import java.lang.annotation.Target;
* <p>详情见: https://redkale.org
*
* @since 2.8.0
* @org.redkale.net.http.RestMapping
* @see org.redkale.net.http.RestMapping
* @see org.redkale.net.http.RestService
* @author zhangjx
*/

View File

@@ -17,7 +17,7 @@ import java.lang.annotation.Target;
* <p>详情见: https://redkale.org
*
* @since 2.8.0
* @org.redkale.net.http.RestMapping
* @see org.redkale.net.http.RestMapping
* @see org.redkale.net.http.RestService
* @author zhangjx
*/