From 1c9af0e56059ed5eef85a6bfc6f05f540f504f7a Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 13 Aug 2026 12:07:20 +0200 Subject: [PATCH] Remove caching from LimilabsImapEmailService Simplified the `LimilabsImapEmailService` by removing the dependency on `IMemoryCache` and eliminating all caching logic. The constructor no longer accepts an `IMemoryCache` parameter, and the static `CacheKeyPrefix` field has been removed. Replaced the caching mechanism with direct email fetching using `imap.GetMessageByUIDAsync`. Refactored the logic for processing attachments and visuals into `EmailAttachmentDto` objects, and streamlined the construction of `ReceivedEmailDto` to include metadata directly from the fetched email data. These changes reduce complexity, improve maintainability, and ensure the service always retrieves the latest email data from the IMAP server. --- .../Services/LimilabsImapEmailService.cs | 95 ++++++++----------- 1 file changed, 42 insertions(+), 53 deletions(-) diff --git a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsImapEmailService.cs b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsImapEmailService.cs index cd7a81b..1fc0e05 100644 --- a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsImapEmailService.cs +++ b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsImapEmailService.cs @@ -6,7 +6,6 @@ using DigitalData.MessagingService.Domain.Exceptions; using DigitalData.MessagingService.Infrastructure.Services.Extensions; using Limilabs.Client.IMAP; using Limilabs.Mail; -using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; using System.Text; @@ -16,10 +15,8 @@ namespace DigitalData.MessagingService.Infrastructure.Services; /// IMAP email service using Limilabs Mail.dll. /// Opens a fresh connection per call — stateless and thread-safe. /// -public class LimilabsImapEmailService(ILogger Logger, IMemoryCache Cache) : IImapEmailService +public class LimilabsImapEmailService(ILogger Logger) : IImapEmailService { - private static readonly string CacheKeyPrefix = Guid.NewGuid().ToString(); - static LimilabsImapEmailService() { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); @@ -67,58 +64,50 @@ public class LimilabsImapEmailService(ILogger Logger, try { #region Read email - var email = await Cache.GetOrCreateAsync( - CacheKeyPrefix + uid, - async entry => + var eml = await imap.GetMessageByUIDAsync(uid, cancel); + var mail = new MailBuilder().CreateFromEml(eml); + var flags = await imap.GetFlagsByUIDAsync(uid, cancel); + + var attachments = new List(); + + foreach (var att in mail.Attachments) + { + attachments.Add(new EmailAttachmentDto { - var eml = await imap.GetMessageByUIDAsync(uid, cancel); - var mail = new MailBuilder().CreateFromEml(eml); - var flags = await imap.GetFlagsByUIDAsync(uid, cancel); - - var attachments = new List(); - - foreach (var att in mail.Attachments) - { - attachments.Add(new EmailAttachmentDto - { - FileName = att.FileName ?? "attachment", - Content = att.Data, - ContentType = att.ContentType?.ToString() ?? "application/octet-stream", - IsInline = false, - ContentId = att.ContentId - }); - } - - foreach (var vis in mail.Visuals) - { - attachments.Add(new EmailAttachmentDto - { - FileName = vis.FileName ?? "inline", - Content = vis.Data, - ContentType = vis.ContentType?.ToString() ?? "application/octet-stream", - IsInline = true, - ContentId = vis.ContentId - }); - } - - return new ReceivedEmailDto - { - Uid = uid, - From = mail.From.FirstOrDefault()?.Address ?? string.Empty, - To = [.. mail.To.SelectMany(m => m.GetMailboxes()).Select(mb => mb.Address)], - Cc = [.. mail.Cc.SelectMany(m => m.GetMailboxes()).Select(mb => mb.Address)], - Subject = mail.Subject ?? string.Empty, - TextBody = mail.Text ?? string.Empty, - HtmlBody = mail.Html ?? string.Empty, - Date = mail.Date ?? DateTime.MinValue, - IsSeen = flags.Contains(Flag.Seen), - Attachments = attachments, - }; + FileName = att.FileName ?? "attachment", + Content = att.Data, + ContentType = att.ContentType?.ToString() ?? "application/octet-stream", + IsInline = false, + ContentId = att.ContentId }); - #endregion Read email + } - if (email is null) - continue; + foreach (var vis in mail.Visuals) + { + attachments.Add(new EmailAttachmentDto + { + FileName = vis.FileName ?? "inline", + Content = vis.Data, + ContentType = vis.ContentType?.ToString() ?? "application/octet-stream", + IsInline = true, + ContentId = vis.ContentId + }); + } + + var email = new ReceivedEmailDto + { + Uid = uid, + From = mail.From.FirstOrDefault()?.Address ?? string.Empty, + To = [.. mail.To.SelectMany(m => m.GetMailboxes()).Select(mb => mb.Address)], + Cc = [.. mail.Cc.SelectMany(m => m.GetMailboxes()).Select(mb => mb.Address)], + Subject = mail.Subject ?? string.Empty, + TextBody = mail.Text ?? string.Empty, + HtmlBody = mail.Html ?? string.Empty, + Date = mail.Date ?? DateTime.MinValue, + IsSeen = flags.Contains(Flag.Seen), + Attachments = attachments, + }; + #endregion Read email if (filter.UnseenOnly && email.IsSeen) continue;