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 a71cc3ed..9653480c 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java @@ -351,46 +351,42 @@ public class EntityCache, E extends IEntity> imple var entityClass = (Class) entityDef.getClazz(); var ids = updateList.stream().map(it -> it.id()).toList(); - try { - var dbList = OrmContext.getQuery(entityClass).in("_id", ids).queryAll(); - var dbMap = dbList.stream().collect(Collectors.toMap(key -> key.id(), value -> value)); - for (var entity : updateList) { - var id = entity.id(); - var dbEntity = dbMap.get(id); + var dbList = OrmContext.getQuery(entityClass).in("_id", ids).queryAll(); + var dbMap = dbList.stream().collect(Collectors.toMap(key -> key.id(), value -> value)); + for (var entity : updateList) { + var id = entity.id(); + var dbEntity = dbMap.get(id); - if (dbEntity == null) { - cache.remove(entity.id()); - logger.warn("[database:{}] not found entity [id:{}]", entityClass.getSimpleName(), id); - continue; - } + if (dbEntity == null) { + cache.remove(entity.id()); + logger.warn("[database:{}] not found entity [id:{}]", entityClass.getSimpleName(), id); + continue; + } - // 如果没有版本号,则直接更新数据库 - if (entity.gvs() <= 0) { - OrmContext.getAccessor().update(entity); - continue; - } + // 如果没有版本号,则直接更新数据库 + if (entity.gvs() <= 0) { + OrmContext.getAccessor().update(entity); + continue; + } - // 如果版本号相同,说明已经更新到 - if (dbEntity.gvs() == entity.gvs()) { - continue; - } + // 如果版本号相同,说明已经更新到 + if (dbEntity.gvs() == entity.gvs()) { + continue; + } - // 如果数据库版本号较小,说明缓存的数据是最新的,直接写入数据库 - if (dbEntity.gvs() < entity.gvs()) { - OrmContext.getAccessor().update(entity); - continue; - } + // 如果数据库版本号较小,说明缓存的数据是最新的,直接写入数据库 + if (dbEntity.gvs() < entity.gvs()) { + OrmContext.getAccessor().update(entity); + continue; + } - // 如果数据库版本号较大,说明缓存的数据不是最新的,直接清除缓存,下次重新加载 - if (dbEntity.gvs() > entity.gvs()) { - cache.remove(id); - load(id); - logger.warn("[database:{}] document of entity [id:{}] version [{}] is greater than cache [vs:{}]", entityClass.getSimpleName(), id, dbEntity.gvs(), entity.gvs()); - continue; - } + // 如果数据库版本号较大,说明缓存的数据不是最新的,直接清除缓存,下次重新加载 + if (dbEntity.gvs() > entity.gvs()) { + cache.remove(id); + load(id); + logger.warn("[database:{}] document of entity [id:{}] version [{}] is greater than cache [vs:{}]", entityClass.getSimpleName(), id, dbEntity.gvs(), entity.gvs()); + continue; } - } catch (Throwable t) { - logger.error("persistAllAndCompare(): [{}] unknown error", entityClass.getSimpleName(), t); } } 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 59dacac9..d16e016e 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/IEntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/IEntityCache.java @@ -77,9 +77,10 @@ public interface IEntityCache, E extends IEntity> /** * 持久化所有缓存数据 */ + void persistAll(); + void persistAllBlock(); - void persistAll(); void forEach(BiConsumer biConsumer);