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.
This commit is contained in:
@@ -6,7 +6,6 @@ using DigitalData.MessagingService.Domain.Exceptions;
|
|||||||
using DigitalData.MessagingService.Infrastructure.Services.Extensions;
|
using DigitalData.MessagingService.Infrastructure.Services.Extensions;
|
||||||
using Limilabs.Client.IMAP;
|
using Limilabs.Client.IMAP;
|
||||||
using Limilabs.Mail;
|
using Limilabs.Mail;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@@ -16,10 +15,8 @@ namespace DigitalData.MessagingService.Infrastructure.Services;
|
|||||||
/// IMAP email service using Limilabs Mail.dll.
|
/// IMAP email service using Limilabs Mail.dll.
|
||||||
/// Opens a fresh connection per call — stateless and thread-safe.
|
/// Opens a fresh connection per call — stateless and thread-safe.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class LimilabsImapEmailService(ILogger<LimilabsImapEmailService> Logger, IMemoryCache Cache) : IImapEmailService
|
public class LimilabsImapEmailService(ILogger<LimilabsImapEmailService> Logger) : IImapEmailService
|
||||||
{
|
{
|
||||||
private static readonly string CacheKeyPrefix = Guid.NewGuid().ToString();
|
|
||||||
|
|
||||||
static LimilabsImapEmailService()
|
static LimilabsImapEmailService()
|
||||||
{
|
{
|
||||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||||
@@ -67,10 +64,6 @@ public class LimilabsImapEmailService(ILogger<LimilabsImapEmailService> Logger,
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
#region Read email
|
#region Read email
|
||||||
var email = await Cache.GetOrCreateAsync(
|
|
||||||
CacheKeyPrefix + uid,
|
|
||||||
async entry =>
|
|
||||||
{
|
|
||||||
var eml = await imap.GetMessageByUIDAsync(uid, cancel);
|
var eml = await imap.GetMessageByUIDAsync(uid, cancel);
|
||||||
var mail = new MailBuilder().CreateFromEml(eml);
|
var mail = new MailBuilder().CreateFromEml(eml);
|
||||||
var flags = await imap.GetFlagsByUIDAsync(uid, cancel);
|
var flags = await imap.GetFlagsByUIDAsync(uid, cancel);
|
||||||
@@ -101,7 +94,7 @@ public class LimilabsImapEmailService(ILogger<LimilabsImapEmailService> Logger,
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ReceivedEmailDto
|
var email = new ReceivedEmailDto
|
||||||
{
|
{
|
||||||
Uid = uid,
|
Uid = uid,
|
||||||
From = mail.From.FirstOrDefault()?.Address ?? string.Empty,
|
From = mail.From.FirstOrDefault()?.Address ?? string.Empty,
|
||||||
@@ -114,12 +107,8 @@ public class LimilabsImapEmailService(ILogger<LimilabsImapEmailService> Logger,
|
|||||||
IsSeen = flags.Contains(Flag.Seen),
|
IsSeen = flags.Contains(Flag.Seen),
|
||||||
Attachments = attachments,
|
Attachments = attachments,
|
||||||
};
|
};
|
||||||
});
|
|
||||||
#endregion Read email
|
#endregion Read email
|
||||||
|
|
||||||
if (email is null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (filter.UnseenOnly && email.IsSeen)
|
if (filter.UnseenOnly && email.IsSeen)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user