Skip to content

Conversation

@HelloSebastian
Copy link
Contributor

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 of ImapProtocol the 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:

$messages = $inbox->query()->whereSince(Carbon::create(2022, 1, 1))->get();

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:

$messages = $inbox->query()->all()->limit(100)->get();

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:

  1. The cache must be reset in additional locations.
  2. I have not tested the LegacyProtocol.
  3. The cache could become invalid during a fetch.
    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!

improve annotations of client and add uid cache to LegacyProtocol change change change cache asd
@Webklex Webklex merged commit 4dc4103 into Webklex:master Mar 7, 2022
@Webklex
Copy link
Owner

Webklex commented Mar 7, 2022

Hi @HelloSebastian ,
many thanks for your pull requests and time spent in the issue section. I highly appreciate it :)

Best regards,

@HelloSebastian HelloSebastian deleted the cache-uid branch March 15, 2022 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants