Skip to content

Commit 19149a6

Browse files
authored
fix(config): cache read no mutation (#1256)
## What? Closes #1251 `GetCachedEmailBody` is a read operation but it was stamping `LastAccessedAt` on the matched entry and rewriting the body cache file on every hit, violating the read/write boundary the issue calls out. Drops the two flagged lines (`config/cache.go:505-506`) and adds a one-line note above the function that `SaveEmailBody` is the access-time owner. ## Why? The issue's analysis is correct: every email view path in `main.go` already chases `GetCachedEmailBody` with `SaveEmailBody`, which sets `LastAccessedAt = time.Now()` on the entry it persists. That keeps the LRU-style eviction in `evict` (sorted by `LastAccessedAt`) working unchanged, with one fewer disk write per email view. I traced both call sites to verify eviction recency is preserved on cache hits: - `main.go:707-744` (UpdatePreviewMsg): cache hit returns `PreviewBodyFetchedMsg{Err: nil}`. The handler at `main.go:746-779` runs `config.SaveEmailBody` whenever `msg.Err == nil`, which updates `LastAccessedAt` (`config/cache.go:552`). - `main.go:1247-1278` (ViewEmailMsg): cache hit returns `EmailBodyFetchedMsg{Err: nil}`. The handler at `main.go:1282-1328` runs `config.SaveEmailBody` whenever `msg.Err == nil`, same path. So an email the user repeatedly opens still bumps to the front of the eviction queue on every open; the work just happens in `SaveEmailBody` (which already exists) instead of duplicating it inside `GetCachedEmailBody`.
1 parent f6f120b commit 19149a6

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

config/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,15 @@ func saveEmailBodyCache(cache *EmailBodyCache) error {
495495
}
496496

497497
// GetCachedEmailBody returns the cached body for a specific email, or nil if not cached.
498+
// LastAccessedAt is updated by SaveEmailBody, not here -- a read should not
499+
// mutate cache state.
498500
func GetCachedEmailBody(folderName string, uid uint32, accountID string) *CachedEmailBody {
499501
cache, err := LoadEmailBodyCache(folderName)
500502
if err != nil {
501503
return nil
502504
}
503505
for i, b := range cache.Bodies {
504506
if b.UID == uid && b.AccountID == accountID {
505-
cache.Bodies[i].LastAccessedAt = time.Now()
506-
_ = saveEmailBodyCache(cache)
507507
return &cache.Bodies[i]
508508
}
509509
}

0 commit comments

Comments
 (0)