Skip to content

Commit

Permalink
chore[cache]: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Aug 26, 2024
1 parent 353914d commit da8e2f6
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions orm/src/main/java/com/zfoo/orm/cache/EntityCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,20 @@ private PNode<PK, E> fetchCachePnode(E entity, boolean safe) {
@Override
public E get(PK pk) {
PNode<PK, E> cachePnode = caches.get(pk);
if (cachePnode == null) {
return null;
}
return cachePnode.getEntity();
return cachePnode == null ? null : cachePnode.getEntity();
}

@Override
public void update(E entity) {
var cachePnode = fetchCachePnode(entity, true);
// 加100以防止,立刻加载并且立刻修改数据的情况发生时,服务器取到的时间戳相同
cachePnode.setModifiedTime(TimeUtils.now() + 100);
// 加128以防止,立刻加载并且立刻修改数据的情况发生时,服务器取到的时间戳相同
cachePnode.setModifiedTime(TimeUtils.now() + 128);
}

@Override
public void updateUnsafe(E entity) {
var cachePnode = fetchCachePnode(entity, false);
cachePnode.setModifiedTime(TimeUtils.now() + 100);
cachePnode.setModifiedTime(TimeUtils.now() + 128);
}

@Override
Expand Down Expand Up @@ -266,7 +263,7 @@ public void accept(PK pk, PNode<PK, E> pnode) {
EventBus.asyncExecute(clazz.hashCode(), () -> doPersist(updateList));
} else {
// 使用scheduler均匀的分配入库的时间点,减少数据库的并发写入压力
SchedulerBus.schedule(() -> executor.execute(() -> doPersist(updateList)), count++ * 100L, TimeUnit.MILLISECONDS);
SchedulerBus.schedule(() -> executor.execute(() -> doPersist(updateList)), count++ * 128L, TimeUnit.MILLISECONDS);
}
}
}
Expand Down

0 comments on commit da8e2f6

Please sign in to comment.