Skip to content

Commit 3790a69

Browse files
committed
reduced memory footprint by not loading all mails
1 parent feadba9 commit 3790a69

2 files changed

Lines changed: 23 additions & 19 deletions

File tree

Services/MailRepository.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@ namespace Lyralabs.TempMailServer
1212
{
1313
public class MailRepository(IDbContextFactory<DatabaseContext> databaseContextFactory)
1414
{
15-
public async Task<List<MailModel>> GetMails(int mailBoxId)
15+
public async Task<List<MailModel>> GetMails(int mailBoxId, int? limit = null)
1616
{
1717
using var context = databaseContextFactory.CreateDbContext();
1818

19-
var mails = await context.Mails
19+
IQueryable<MailModel> query = context.Mails
2020
.Where(x => x.MailboxId == mailBoxId)
21-
.ToListAsync();
21+
.OrderByDescending(x => x.ReceivedDate);
22+
23+
if (limit != null)
24+
{
25+
query = query.Take(limit.Value);
26+
}
27+
28+
var mails = await query.ToListAsync();
2229

2330
return mails;
2431
}
@@ -69,9 +76,12 @@ public async Task<MailboxModel> GetMailbox(string address, bool loadMails)
6976

7077
var mailbox = await query.SingleAsync(x => x.Address == address);
7178

72-
mailbox.Mails = mailbox.Mails
73-
.OrderByDescending(x => x.ReceivedDate)
74-
.ToList();
79+
if (loadMails == true)
80+
{
81+
mailbox.Mails = mailbox.Mails
82+
.OrderByDescending(x => x.ReceivedDate)
83+
.ToList();
84+
}
7585

7686
return mailbox;
7787
}

Services/MailboxService.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,21 @@ public async Task<List<MailPreviewDto>> GetDecryptedMailsAsync(string address, s
4141
throw new ArgumentException($"'{nameof(address)}' cannot be null or whitespace.", nameof(address));
4242
}
4343

44-
var mailbox = await this.mailRepository.GetMailbox(address, loadMails: true);
44+
var mailbox = await this.mailRepository.GetMailbox(address, loadMails: false);
4545

4646
if (mailbox is null)
4747
{
4848
return [];
49-
}
50-
51-
var query = mailbox.Mails
52-
.OrderByDescending(x => x.ReceivedDate)
49+
}
50+
51+
var mails = await this.mailRepository.GetMails(mailbox.Id, limit: 200);
52+
return mails
5353
.Select(x =>
5454
{
5555
var mail = this.emailCryptoService.Decrypt(x, privateKey);
5656
return this.ConvertToPreview(mail);
57-
});
58-
59-
if (limit is not null)
60-
{
61-
query = query.Take(limit.Value);
62-
}
63-
64-
return query.ToList();
57+
})
58+
.ToList();
6559
}
6660

6761
public MailPreviewDto ConvertToPreview(MailModel mail)

0 commit comments

Comments
 (0)