This commit is contained in:
redkale
2023-09-21 15:06:25 +08:00
parent eab480f0be
commit 915583d915
4 changed files with 15 additions and 13 deletions

View File

@@ -15,9 +15,9 @@
***************************************************************************** */ ***************************************************************************** */
package org.redkale.persistence; package org.redkale.persistence;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.*;
/** /**
* Specifies whether an entity should be cached if caching is enabled * Specifies whether an entity should be cached if caching is enabled
@@ -64,6 +64,6 @@ public @interface Cacheable {
* *
* @return boolean * @return boolean
*/ */
boolean continuousid() default false; boolean sequent() default false;
} }

View File

@@ -15,6 +15,7 @@ import java.util.function.*;
import java.util.logging.*; import java.util.logging.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.redkale.annotation.*; import org.redkale.annotation.*;
import org.redkale.annotation.AutoLoad; import org.redkale.annotation.AutoLoad;
import org.redkale.annotation.ResourceListener; import org.redkale.annotation.ResourceListener;
@@ -818,7 +819,8 @@ public final class CacheMemorySource extends AbstractCacheSource {
if (entry == null) { if (entry == null) {
return new ArrayList(); return new ArrayList();
} else { } else {
return new ArrayList(entry.mapValue.values().stream().map(v -> Utility.convertValue(type, v)).toList()); Stream<T> stream = entry.mapValue.values().stream().map(v -> Utility.convertValue(type, v));
return new ArrayList(stream.collect(Collectors.toList()));
} }
}); });
} }

View File

@@ -38,7 +38,7 @@ public final class EntityCache<T> {
// CopyOnWriteArrayList 插入慢、查询快; 10w数据插入需要3.2秒; ConcurrentLinkedQueue 插入快、查询慢10w数据查询需要 0.062秒, 查询慢40%; // CopyOnWriteArrayList 插入慢、查询快; 10w数据插入需要3.2秒; ConcurrentLinkedQueue 插入快、查询慢10w数据查询需要 0.062秒, 查询慢40%;
private Collection<T> list = new ConcurrentLinkedQueue(); private Collection<T> list = new ConcurrentLinkedQueue();
//continuousid=true此字段值才有效 //sequent=true此字段值才有效
private T[] array; private T[] array;
//Flipper.sort转换成Comparator的缓存 //Flipper.sort转换成Comparator的缓存
@@ -79,7 +79,7 @@ public final class EntityCache<T> {
final int interval; final int interval;
//&#064;Cacheable的主键字段是否同时满足: 1、类型为int2、主键值可为数组下标3、记录总数有限 //&#064;Cacheable的主键字段是否同时满足: 1、类型为int2、主键值可为数组下标3、记录总数有限
final boolean continuousid; final boolean sequent;
//&#064;Cacheable的定时器 //&#064;Cacheable的定时器
private ScheduledThreadPoolExecutor scheduler; private ScheduledThreadPoolExecutor scheduler;
@@ -87,13 +87,13 @@ public final class EntityCache<T> {
private CompletableFuture<List<T>> loadFuture; private CompletableFuture<List<T>> loadFuture;
public EntityCache(final EntityInfo<T> info, final Cacheable c) { public EntityCache(final EntityInfo<T> info, final Cacheable c) {
this(info, c != null ? c.interval() : 0, c != null && c.direct(), c != null && c.continuousid()); this(info, c != null ? c.interval() : 0, c != null && c.direct(), c != null && c.sequent());
} }
EntityCache(final EntityInfo<T> info, final int cacheInterval, final boolean cacheDirect, final boolean cacheContinuousid) { EntityCache(final EntityInfo<T> info, final int cacheInterval, final boolean cacheDirect, final boolean cacheContinuousid) {
this.info = info; this.info = info;
this.interval = cacheInterval < 0 ? 0 : cacheInterval; this.interval = cacheInterval < 0 ? 0 : cacheInterval;
this.continuousid = cacheContinuousid && info.getPrimary().type() == int.class; this.sequent = cacheContinuousid && info.getPrimary().type() == int.class;
this.type = info.getType(); this.type = info.getType();
this.arrayer = info.getArrayer(); this.arrayer = info.getArrayer();
this.creator = info.getCreator(); this.creator = info.getCreator();
@@ -213,7 +213,7 @@ public final class EntityCache<T> {
} }
private T[] transferArray(List<T> all) { private T[] transferArray(List<T> all) {
if (continuousid && all != null && !all.isEmpty()) { if (sequent && all != null && !all.isEmpty()) {
try { try {
int maxid = all.stream().mapToInt(v -> v == null ? 0 : (Integer) primary.get(v)).max().orElse(0); int maxid = all.stream().mapToInt(v -> v == null ? 0 : (Integer) primary.get(v)).max().orElse(0);
T[] result = arrayer.apply(maxid + 1); T[] result = arrayer.apply(maxid + 1);
@@ -286,7 +286,7 @@ public final class EntityCache<T> {
return result; return result;
} }
} }
if (continuousid && array != null) { if (sequent && array != null) {
T[] array0 = array; T[] array0 = array;
T[] result = arrayer.apply(pks.length); T[] result = arrayer.apply(pks.length);
if (needCopy) { if (needCopy) {
@@ -805,7 +805,7 @@ public final class EntityCache<T> {
T old = this.map.putIfAbsent(this.primary.get(rs), rs); T old = this.map.putIfAbsent(this.primary.get(rs), rs);
if (old == null) { if (old == null) {
this.list.add(rs); this.list.add(rs);
if (continuousid) { if (sequent) {
this.array = transferArray(new ArrayList<>(this.list)); this.array = transferArray(new ArrayList<>(this.list));
} }
return 1; return 1;
@@ -824,7 +824,7 @@ public final class EntityCache<T> {
return 0; return 0;
} }
this.list.remove(rs); this.list.remove(rs);
if (continuousid) { if (sequent) {
this.array[(Integer) primary.get(rs)] = null; this.array[(Integer) primary.get(rs)] = null;
} }
return 1; return 1;
@@ -853,7 +853,7 @@ public final class EntityCache<T> {
ids[++i] = this.primary.get(t); ids[++i] = this.primary.get(t);
this.map.remove(ids[i]); this.map.remove(ids[i]);
this.list.remove(t); this.list.remove(t);
if (continuousid) { if (sequent) {
this.array[(Integer) primary.get(t)] = null; this.array[(Integer) primary.get(t)] = null;
} }
} }

View File

@@ -690,7 +690,7 @@ public final class EntityInfo<T> {
this.cache = new EntityCache<>(this, this.cache = new EntityCache<>(this,
c1 == null ? (c2 == null ? 0 : c2.interval()) : c1.interval(), c1 == null ? (c2 == null ? 0 : c2.interval()) : c1.interval(),
c1 == null ? (c2 == null ? false : c2.direct()) : c1.direct(), c1 == null ? (c2 == null ? false : c2.direct()) : c1.direct(),
c1 == null ? false : c1.continuousid()); c1 == null ? false : c1.sequent());
} else { } else {
this.cache = null; this.cache = null;
} }