From e0eda4f8be42ca6ca2e9e938aa090a935ae19e7c Mon Sep 17 00:00:00 2001 From: lqb <1991tss@gmail.com> Date: Mon, 1 Apr 2024 06:08:57 +0800 Subject: [PATCH] #3370 LimitOffsetPagedList add getTotalCount cache (#3373) * #3370 LimitOffsetPagedList add getTotalCount cache * Move totalRowCount check to inside the lock * No change, format only on LimitOffsetPagedList --------- Co-authored-by: Rob Bygrave --- .../server/query/LimitOffsetPagedList.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/query/LimitOffsetPagedList.java b/ebean-core/src/main/java/io/ebeaninternal/server/query/LimitOffsetPagedList.java index fd461427d..19f2dffcc 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/query/LimitOffsetPagedList.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/query/LimitOffsetPagedList.java @@ -3,8 +3,9 @@ package io.ebeaninternal.server.query; import io.ebean.PagedList; import io.ebeaninternal.api.SpiEbeanServer; import io.ebeaninternal.api.SpiQuery; - import jakarta.persistence.PersistenceException; + +import java.util.Collections; import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.locks.ReentrantLock; @@ -20,7 +21,7 @@ public final class LimitOffsetPagedList implements PagedList { private final int firstRow; private final int maxRows; - private int foregroundTotalRowCount = -1; + private int totalRowCount = -1; private Future futureRowCount; private List list; @@ -57,7 +58,12 @@ public final class LimitOffsetPagedList implements PagedList { lock.lock(); try { if (list == null) { - list = server.findList(query); + if (totalRowCount == 0) { + // already count and no rows + list = Collections.emptyList(); + } else { + list = server.findList(query); + } } return list; } finally { @@ -87,20 +93,21 @@ public final class LimitOffsetPagedList implements PagedList { public int getTotalCount() { lock.lock(); try { + if (totalRowCount > -1) { + return totalRowCount; + } if (futureRowCount != null) { try { // background query already initiated so get it with a wait - return futureRowCount.get(); + totalRowCount = futureRowCount.get(); + return totalRowCount; } catch (Exception e) { throw new PersistenceException(e); } } - // already fetched? - if (foregroundTotalRowCount > -1) return foregroundTotalRowCount; - // just using foreground thread - foregroundTotalRowCount = server.findCount(query); - return foregroundTotalRowCount; + totalRowCount = server.findCount(query); + return totalRowCount; } finally { lock.unlock(); }