diff --git a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java index 97a58c3d..a6177a2f 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java @@ -74,7 +74,12 @@ public class EntityCache, E extends IEntity> imple if (removalCause == LazyCache.RemovalCause.EXPLICIT) { return; } - EventBus.asyncExecute(clazz.hashCode(), () -> doPersist(removes.stream().map(it -> it.v.getEntity()).toList())); + var updateList = removes.stream() + .map(it -> it.v) + .filter(it -> !noNeedUpdate(it)) + .map(it -> it.getEntity()) + .toList(); + EventBus.asyncExecute(clazz.hashCode(), () -> doPersist(updateList)); } }; var expireCheckIntervalMillis = Math.max(3 * TimeUtils.MILLIS_PER_SECOND, entityDef.getExpireMillisecond() / 10); @@ -220,16 +225,17 @@ public class EntityCache, E extends IEntity> imple @Override public void persist(PK pk) { var pnode = caches.get(pk); - if (pnode == null) { - return; - } - if (pnode.getModifiedTime() == pnode.getWriteToDbTime()) { + if (noNeedUpdate(pnode)) { return; } pnode.resetTime(TimeUtils.currentTimeMillis()); doPersist(List.of(pnode.getEntity())); } + private boolean noNeedUpdate(PNode pnode) { + return pnode == null || pnode.getEntity() == null || pnode.getModifiedTime() == pnode.getWriteToDbTime(); + } + // 游戏中80%都是执行更新的操作,这样做会极大的提高更新速度 // 没有并发问题的entity指的是内部没有使用集合或者使用的集合全部支持并发操作 @@ -246,12 +252,12 @@ public class EntityCache, E extends IEntity> imple caches.forEach(new BiConsumer>() { @Override public void accept(PK pk, PNode pnode) { - var entity = pnode.getEntity(); - if (pnode.getModifiedTime() != pnode.getWriteToDbTime()) { - pnode.resetTime(currentTime); - var updateList = updateMap.computeIfAbsent(pnode.getThreadId(), it -> new ArrayList<>(initSize)); - updateList.add(entity); + if (noNeedUpdate(pnode)) { + return; } + pnode.resetTime(currentTime); + var updateList = updateMap.computeIfAbsent(pnode.getThreadId(), it -> new ArrayList<>(initSize)); + updateList.add(pnode.getEntity()); } }); var count = 0; @@ -276,11 +282,11 @@ public class EntityCache, E extends IEntity> imple caches.forEach(new BiConsumer>() { @Override public void accept(PK pk, PNode pnode) { - var entity = pnode.getEntity(); - if (pnode.getModifiedTime() != pnode.getWriteToDbTime()) { - pnode.resetTime(currentTime); - updateList.add(entity); + if (noNeedUpdate(pnode)) { + return; } + pnode.resetTime(currentTime); + updateList.add(pnode.getEntity()); } }); doPersist(updateList);