From 2ab34818e59a62099c9057cf6d495869e4e0f0dc Mon Sep 17 00:00:00 2001 From: godotg Date: Tue, 26 Mar 2024 14:12:35 +0800 Subject: [PATCH] perf[cache]: remove listener in eventbus --- .../java/com/zfoo/orm/cache/EntityCache.java | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) 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 c88e908af..14abbe299 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java @@ -16,6 +16,7 @@ import com.mongodb.client.model.BulkWriteOptions; import com.mongodb.client.model.Filters; import com.mongodb.client.model.ReplaceOneModel; +import com.zfoo.event.manager.EventBus; import com.zfoo.orm.OrmContext; import com.zfoo.orm.cache.persister.IOrmPersister; import com.zfoo.orm.cache.persister.PNode; @@ -68,23 +69,29 @@ public void accept(Pair> pair, LazyCache.RemovalCause removalCause) var entity = pnode.getEntity(); @SuppressWarnings("unchecked") var entityClass = (Class) entityDef.getClazz(); - var collection = OrmContext.getOrmManager().getCollection(entityClass); - - var version = entity.gvs(); - entity.svs(version + 1); - - var filter = entity.gvs() > 0 - ? Filters.and(Filters.eq("_id", entity.id()), Filters.eq("vs", version)) - : Filters.eq("_id", entity.id()); - var result = collection.replaceOne(filter, entity); - if (result.getModifiedCount() <= 0) { - // 移除缓存时,更新数据库中的实体文档异常 - logger.error("onRemoval(): update entity to db failed when remove [{}] [pk:{}] by [removalCause:{}]", entityClass.getSimpleName(), entity.id(), removalCause); - } + EventBus.execute(entityClass.hashCode(), new Runnable() { + @Override + public void run() { + var collection = OrmContext.getOrmManager().getCollection(entityClass); + + var version = entity.gvs(); + entity.svs(version + 1); + + var filter = entity.gvs() > 0 + ? Filters.and(Filters.eq("_id", entity.id()), Filters.eq("vs", version)) + : Filters.eq("_id", entity.id()); + var result = collection.replaceOne(filter, entity); + if (result.getModifiedCount() <= 0) { + // 移除缓存时,更新数据库中的实体文档异常 + logger.error("onRemoval(): update entity to db failed when remove [{}] [pk:{}] by [removalCause:{}]", entityClass.getSimpleName(), entity.id(), removalCause); + } + } + }); } }; + var expireCheckIntervalMillis = Math.max(3 * TimeUtils.MILLIS_PER_SECOND, entityDef.getExpireMillisecond() / 10); this.entityDef = entityDef; - this.cache = new LazyCache<>(entityDef.getCacheSize(), entityDef.getExpireMillisecond(), 1 * TimeUtils.MILLIS_PER_SECOND, removeCallback); + this.cache = new LazyCache<>(entityDef.getCacheSize(), entityDef.getExpireMillisecond(), expireCheckIntervalMillis, removeCallback); if (CollectionUtils.isNotEmpty(entityDef.getIndexDefMap())) { // indexMap