Skip to content

Commit

Permalink
caching for number of total messages. issue #2724
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dimension committed Dec 8, 2024
1 parent 5b76ae9 commit 89aec7d
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ public class FolderContainer {

private long cachedUnreadUpdated = -1;
private int cachedUnread = -1;
private int cachedTotal = -1;

private static final Logger log = Logger.getLogger(FolderContainer.class.getName());
private static final HashMap<String, String> folderNameMapping = new HashMap<>();
Expand All @@ -712,6 +713,7 @@ public FolderContainer(Folder f) {
public void resetCaches() {
this.cachedToStringUpdated = -1;
this.cachedUnreadUpdated = -1;
this.cachedTotal=-1;
this.cachedMessages.clear();
}

Expand Down Expand Up @@ -743,6 +745,7 @@ public int getUnreadMessageCount() {
this.cachedUnreadUpdated = System.currentTimeMillis();
try {
this.cachedUnread = this.folder.getUnreadMessageCount();
this.cachedTotal = this.folder.getMessageCount();
} catch (StoreClosedException stex) {
log.warn("Unable to determine number of unread messages - folder is closed");
} catch (MessagingException ex) {
Expand All @@ -752,6 +755,24 @@ public int getUnreadMessageCount() {
}
return cachedUnread;
}

public int getMessageCount() {

if (this.cachedTotal == -1 || ((System.currentTimeMillis() - cachedUnreadUpdated) > this.getRetentionTime())) {
if (this.folder != null) {
this.cachedUnreadUpdated = System.currentTimeMillis();
try {
this.cachedUnread = this.folder.getUnreadMessageCount();
this.cachedTotal = this.folder.getMessageCount();
} catch (StoreClosedException stex) {
log.warn("Unable to determine number of total messages - folder is closed");
} catch (MessagingException ex) {
log.error("Unable to determine number of total messages", ex);
}
}
}
return cachedTotal;
}

/*
* Called e.g. by the email tree cell renderer, therefore it is performance critical
Expand All @@ -774,8 +795,8 @@ public String toString() {
}
}

//int msgCount = this.folder.getMessageCount();
int msgCount = this.getUnreadMessageCount();
int msgCount = this.folder.getMessageCount();
//int msgCount = this.getUnreadMessageCount();
cachedToStringUpdated = System.currentTimeMillis();
if (msgCount < 0) {
cachedToString = name;
Expand Down

0 comments on commit 89aec7d

Please sign in to comment.