-
-
Notifications
You must be signed in to change notification settings - Fork 61
feat(config): add LRU eviction #1227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+71
−12
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3422887
feat(config): add LRU eviction to email body cache
mavonx 263f7ae
feat(config): make email body cache threshold configurable
mavonx 72f210b
fix: restore correct cache threshold units
mavonx afc114c
fix(config): fix LRU eviction correctness
mavonx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -422,11 +422,13 @@ type CachedAttachment struct { | |
|
|
||
| // CachedEmailBody stores the body and attachment metadata for a single email. | ||
| type CachedEmailBody struct { | ||
| UID uint32 `json:"uid"` | ||
| AccountID string `json:"account_id"` | ||
| Body string `json:"body"` | ||
| Attachments []CachedAttachment `json:"attachments,omitempty"` | ||
| CachedAt time.Time `json:"cached_at"` | ||
| UID uint32 `json:"uid"` | ||
| AccountID string `json:"account_id"` | ||
| Body string `json:"body"` | ||
| Attachments []CachedAttachment `json:"attachments,omitempty"` | ||
| CachedAt time.Time `json:"cached_at"` | ||
| LastAccessedAt time.Time `json:"last_accessed_at"` | ||
| SizeBytes int `json:"size_bytes"` | ||
| } | ||
|
|
||
| // EmailBodyCache stores cached email bodies for a folder. | ||
|
|
@@ -495,22 +497,57 @@ func GetCachedEmailBody(folderName string, uid uint32, accountID string) *Cached | |
| if err != nil { | ||
| return nil | ||
| } | ||
| for _, b := range cache.Bodies { | ||
| for i, b := range cache.Bodies { | ||
| if b.UID == uid && b.AccountID == accountID { | ||
| return &b | ||
| cache.Bodies[i].LastAccessedAt = time.Now() | ||
| _ = saveEmailBodyCache(cache) | ||
| return &cache.Bodies[i] | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func calculateEmailBodySize(body *CachedEmailBody) int { | ||
| size := len(body.Body) | ||
| for _, att := range body.Attachments { | ||
| size += len(att.Filename) | ||
| size += len(att.PartID) | ||
| size += len(att.Encoding) | ||
| size += len(att.MIMEType) | ||
| size += len(att.ContentID) | ||
| size += len(att.CalendarData) | ||
| } | ||
| return size | ||
| } | ||
|
|
||
| func calculateTotalCacheSize(cache *EmailBodyCache) int { | ||
| total := 0 | ||
| for _, b := range cache.Bodies { | ||
| total += b.SizeBytes | ||
| } | ||
| return total | ||
| } | ||
|
|
||
| func evict(cache *EmailBodyCache, newSize int, threshold int) { | ||
| sort.Slice(cache.Bodies, func(i, j int) bool { | ||
| return cache.Bodies[i].LastAccessedAt.Before(cache.Bodies[j].LastAccessedAt) | ||
| }) | ||
|
|
||
| for len(cache.Bodies) > 0 && calculateTotalCacheSize(cache)+newSize > threshold { | ||
| cache.Bodies = cache.Bodies[1:] | ||
| } | ||
| } | ||
|
|
||
| // SaveEmailBody saves or updates a cached email body for a folder. | ||
| func SaveEmailBody(folderName string, body CachedEmailBody) error { | ||
| func SaveEmailBody(folderName string, body CachedEmailBody, threshold int) error { | ||
| cache, err := LoadEmailBodyCache(folderName) | ||
| if err != nil { | ||
| cache = &EmailBodyCache{FolderName: folderName} | ||
| } | ||
|
|
||
| body.CachedAt = time.Now() | ||
| body.LastAccessedAt = time.Now() | ||
| body.SizeBytes = calculateEmailBodySize(&body) | ||
|
|
||
| // Replace existing or append | ||
| found := false | ||
|
|
@@ -522,7 +559,13 @@ func SaveEmailBody(folderName string, body CachedEmailBody) error { | |
| } | ||
| } | ||
| if !found { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The least of my concerns, but also worth noting, that, if the email is changed ( |
||
| cache.Bodies = append(cache.Bodies, body) | ||
| if body.SizeBytes <= threshold { | ||
| if calculateTotalCacheSize(cache)+body.SizeBytes > threshold { | ||
| evict(cache, body.SizeBytes, threshold) | ||
| } | ||
|
|
||
| cache.Bodies = append(cache.Bodies, body) | ||
| } | ||
| } | ||
|
|
||
| return saveEmailBodyCache(cache) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I see, this is never updated. Should be updated on
GetCachedEmailBody.