-
-
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
Changes from 3 commits
3422887
263f7ae
72f210b
afc114c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
|
|
@@ -503,14 +505,34 @@ func GetCachedEmailBody(folderName string, uid uint32, accountID string) *Cached | |
| return nil | ||
| } | ||
|
|
||
| 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 = len(body.Body) | ||
|
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. this does not count |
||
|
|
||
| // Replace existing or append | ||
| found := false | ||
|
|
@@ -522,6 +544,11 @@ 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 ( |
||
|
|
||
| if calculateTotalCacheSize(cache)+body.SizeBytes > threshold { | ||
| evict(cache, body.SizeBytes, threshold) | ||
| } | ||
|
|
||
| cache.Bodies = append(cache.Bodies, body) | ||
|
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. If one new email is more than the |
||
| } | ||
|
|
||
|
|
||
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.