Skip to content

Commit

Permalink
perf[cache]: remove listener in eventbus
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Mar 26, 2024
1 parent 4b0018e commit 2ab3481
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions orm/src/main/java/com/zfoo/orm/cache/EntityCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,23 +69,29 @@ public void accept(Pair<PK, PNode<E>> pair, LazyCache.RemovalCause removalCause)
var entity = pnode.getEntity();
@SuppressWarnings("unchecked")
var entityClass = (Class<E>) 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
Expand Down

0 comments on commit 2ab3481

Please sign in to comment.