格式化

This commit is contained in:
redkale
2024-09-11 10:12:24 +08:00
parent eced5e522e
commit 61586beb45
4 changed files with 21 additions and 15 deletions

View File

@@ -94,7 +94,7 @@ public interface CachedManager extends Resourcable {
* 处理指定缓存key的{@link org.redkale.cached.spi.CachedAction}<br> * 处理指定缓存key的{@link org.redkale.cached.spi.CachedAction}<br>
* 可用于动态调整缓存时长 * 可用于动态调整缓存时长
* *
* @param templetKey 缓存key * @param templetKey 模板key
* @param consumer 处理函数 * @param consumer 处理函数
*/ */
public void acceptCachedAction(String templetKey, Consumer<CachedAction> consumer); public void acceptCachedAction(String templetKey, Consumer<CachedAction> consumer);

View File

@@ -85,7 +85,7 @@ public class CachedAction {
this.paramNames = paramNames; this.paramNames = paramNames;
this.methodName = method.getName(); this.methodName = method.getName();
this.fieldName = Objects.requireNonNull(fieldName); this.fieldName = Objects.requireNonNull(fieldName);
this.templetKey = cached.getKey(); this.templetKey = cached.getTempletKey();
Type returnType = method.getGenericReturnType(); Type returnType = method.getGenericReturnType();
this.async = CompletableFuture.class.isAssignableFrom(TypeToken.typeToClass(returnType)); this.async = CompletableFuture.class.isAssignableFrom(TypeToken.typeToClass(returnType));
this.resultType = this.async ? ((ParameterizedType) returnType).getActualTypeArguments()[0] : returnType; this.resultType = this.async ? ((ParameterizedType) returnType).getActualTypeArguments()[0] : returnType;
@@ -93,7 +93,7 @@ public class CachedAction {
String init(ResourceFactory resourceFactory, Object service) { String init(ResourceFactory resourceFactory, Object service) {
this.manager = resourceFactory.load(cached.getManager(), CachedManager.class); this.manager = resourceFactory.load(cached.getManager(), CachedManager.class);
final String key = environment.getPropertyValue(cached.getKey()); final String key = environment.getPropertyValue(cached.getTempletKey());
if (key.startsWith("@")) { // 动态加载缓存key生成器 if (key.startsWith("@")) { // 动态加载缓存key生成器
String generatorName = key.substring(1); String generatorName = key.substring(1);
this.keyGenerator = resourceFactory.findChild(generatorName, CachedKeyGenerator.class); this.keyGenerator = resourceFactory.findChild(generatorName, CachedKeyGenerator.class);

View File

@@ -20,7 +20,7 @@ public class CachedEntry {
private String manager; private String manager;
private String key; private String templetKey;
private String localExpire; private String localExpire;
@@ -34,7 +34,7 @@ public class CachedEntry {
public CachedEntry(DynForCached cached) { public CachedEntry(DynForCached cached) {
this.manager = cached.manager(); this.manager = cached.manager();
this.key = cached.key(); this.templetKey = cached.key();
this.localExpire = cached.localExpire(); this.localExpire = cached.localExpire();
this.remoteExpire = cached.remoteExpire(); this.remoteExpire = cached.remoteExpire();
this.timeUnit = cached.timeUnit(); this.timeUnit = cached.timeUnit();
@@ -43,7 +43,7 @@ public class CachedEntry {
public CachedEntry(Cached cached) { public CachedEntry(Cached cached) {
this.manager = cached.manager(); this.manager = cached.manager();
this.key = cached.key(); this.templetKey = cached.key();
this.localExpire = cached.localExpire(); this.localExpire = cached.localExpire();
this.remoteExpire = cached.remoteExpire(); this.remoteExpire = cached.remoteExpire();
this.timeUnit = cached.timeUnit(); this.timeUnit = cached.timeUnit();
@@ -54,8 +54,8 @@ public class CachedEntry {
return manager; return manager;
} }
public String getKey() { public String getTempletKey() {
return key; return templetKey;
} }
public String getLocalExpire() { public String getLocalExpire() {

View File

@@ -159,7 +159,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
keys.clear(); keys.clear();
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
container.forEach((k, x) -> { container.forEach((k, x) -> {
if (x.expireMills > 0 && (now > (x.lastAccessed + x.expireMills))) { if (x.isExpired(now)) {
keys.add(x.key); keys.add(x.key);
} }
}); });
@@ -171,7 +171,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
} }
long now2 = System.currentTimeMillis(); long now2 = System.currentTimeMillis();
rateLimitContainer.forEach((k, x) -> { rateLimitContainer.forEach((k, x) -> {
if (x.expireMills > 0 && (now2 > (x.lastAccessed + x.expireMills))) { if (x.isExpired(now2)) {
keys.add(x.key); keys.add(x.key);
} }
}); });
@@ -2604,10 +2604,13 @@ public final class CacheMemorySource extends AbstractCacheSource {
return JsonFactory.root().getConvert().convertTo(this); return JsonFactory.root().getConvert().convertTo(this);
} }
public boolean isExpired(long now) {
return expireMills > 0 && (initTime + expireMills) < now;
}
@ConvertColumn(ignore = true) @ConvertColumn(ignore = true)
public boolean isExpired() { public boolean isExpired() {
long now = System.currentTimeMillis(); return isExpired(System.currentTimeMillis());
return expireMills > 0 && (initTime + expireMills) < now;
} }
public void lock() { public void lock() {
@@ -2700,15 +2703,18 @@ public final class CacheMemorySource extends AbstractCacheSource {
return JsonFactory.root().getConvert().convertTo(this); return JsonFactory.root().getConvert().convertTo(this);
} }
@ConvertColumn(ignore = true) public boolean isExpired(long now) {
public boolean isExpired() {
long now = System.currentTimeMillis();
if (endTime > 0) { if (endTime > 0) {
return now >= endTime; return now >= endTime;
} }
return expireMills > 0 && (initTime + expireMills) < now; return expireMills > 0 && (initTime + expireMills) < now;
} }
@ConvertColumn(ignore = true)
public boolean isExpired() {
return isExpired(System.currentTimeMillis());
}
// value类型只能是byte[]/String/AtomicLong // value类型只能是byte[]/String/AtomicLong
public static <T> T serialToObj(@Nonnull Convert convert, @Nonnull Type type, Serializable value) { public static <T> T serialToObj(@Nonnull Convert convert, @Nonnull Type type, Serializable value) {
if (value == null) { if (value == null) {