From 982e123e877c4273b14da94bfe56c9f44f0b7fec Mon Sep 17 00:00:00 2001 From: godotg Date: Wed, 26 Jun 2024 12:50:33 +0800 Subject: [PATCH] fix[orm]: cache cannot be persisted when closed application --- .../main/java/com/zfoo/orm/OrmContext.java | 2 +- .../java/com/zfoo/orm/cache/EntityCache.java | 33 +++++++++++-------- .../java/com/zfoo/orm/cache/IEntityCache.java | 3 ++ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/orm/src/main/java/com/zfoo/orm/OrmContext.java b/orm/src/main/java/com/zfoo/orm/OrmContext.java index 2e729d25..1ad3d0b4 100644 --- a/orm/src/main/java/com/zfoo/orm/OrmContext.java +++ b/orm/src/main/java/com/zfoo/orm/OrmContext.java @@ -107,7 +107,7 @@ public class OrmContext implements ApplicationListener, try { instance.ormManager .getAllEntityCaches() - .forEach(it -> it.persistAll()); + .forEach(it -> it.persistAllBlock()); instance.ormManager.mongoClient().close(); } catch (Exception e) { logger.error("Failed to close the MongoClient database connection", e); 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 2b51e176..a71cc3ed 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java @@ -240,26 +240,33 @@ public class EntityCache, E extends IEntity> imple doPersist(updateList, DEFAULT_BATCH_SIZE); } + @Override + public void persistAllBlock() { + var currentTime = TimeUtils.currentTimeMillis(); + var updateList = new ArrayList(); + cache.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); + } + } + }); + doPersist(updateList, DEFAULT_BATCH_SIZE); + } + + // 游戏中80%都是执行更新的操作,这样做会极大的提高更新速度 // 没有并发问题的entity指的是内部没有使用集合或者使用的集合全部支持并发操作 // 没有并发问题的entity还是在异步线程池Event慢慢更新,有并发问题的entity才放到原来的update线程去更新(第一次update会记录entity所在线程) @Override public void persistAll() { - var currentTime = TimeUtils.currentTimeMillis(); if (entityDef.isThreadSafe()) { - var updateList = new ArrayList(); - cache.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); - } - } - }); - EventBus.asyncExecute(entityDef.getClazz().hashCode(), () -> doPersist(updateList, DEFAULT_BATCH_SIZE)); + EventBus.asyncExecute(entityDef.getClazz().hashCode(), () -> persistAllBlock()); } else { + var currentTime = TimeUtils.currentTimeMillis(); // key为threadId var updateMap = new HashMap>(); cache.forEach(new BiConsumer>() { diff --git a/orm/src/main/java/com/zfoo/orm/cache/IEntityCache.java b/orm/src/main/java/com/zfoo/orm/cache/IEntityCache.java index 16077ac4..59dacac9 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/IEntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/IEntityCache.java @@ -14,6 +14,7 @@ package com.zfoo.orm.cache; import com.zfoo.orm.model.IEntity; +import java.util.List; import java.util.function.BiConsumer; /** @@ -76,6 +77,8 @@ public interface IEntityCache, E extends IEntity> /** * 持久化所有缓存数据 */ + void persistAllBlock(); + void persistAll(); void forEach(BiConsumer biConsumer);