add uid cache for UID of a mailbox (folder) to improve performance #204
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Hi @Webklex,
this PR adds a cache for UID of a mailbox (folder).
Description
I noticed that when initializing each message, the
ProtocolInterface::getUid($id = null)method is called. In this, the matching UID is to be found based on a message number. In the implementation ofImapProtocolthe complete mailbox with all UIDs for each message is retrieved, iterated and searched for the matching message number and the stored UID. This causes not only a large additional effort in requests but also in the runtime of an execution.For this problem I developed a UID cache, which writes the UIDs into an array at the first call of the
ProtocolInterface::getUid($id = null). If in a future query the mailbox (folder) has not changed, the cache is used. On logout or when the mailbox (folder) is changed, the cache is cleared.You can disable the cache in the configuration (uid_cache). By default it is enabled.
Tests
I have tried the cache on the following examples:
It found 133 messages.
If the UID cache is deactivated the call needs 17290ms.
If the UID cache is enabled the call nedes 2211ms.
If I have not miscalculated, that is a time saving of 87.21%.
A second test with 100 messages:
It found 311 in the mailbox but only 100 messages were initialized.
If the UID cache is deactivated the call needs 15201ms.
If the UID cache is enabled the call needs 3621ms.
That is a time saving of 76.18 %.
Potential problems:
In the implementation of ImapProtocol, the cache is refreshed if no UID is found. Only after that an exception is thrown.
I hope the PR is helpful and I haven't missed anything. I am happy to receive feedback!