aboutsummaryrefslogtreecommitdiff
path: root/wttrin.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-06-28 03:53:24 -0400
committerCraig Jennings <c@cjennings.net>2026-06-28 03:53:24 -0400
commitec38418d6d3eb4342363af1714f0073a74d52fbc (patch)
tree1327072f95de36f0577e114fd259c8d064426619 /wttrin.el
parent944a52f0e33087dafba383edb3d97ec19a06d361 (diff)
downloademacs-wttrin-ec38418d6d3eb4342363af1714f0073a74d52fbc.tar.gz
emacs-wttrin-ec38418d6d3eb4342363af1714f0073a74d52fbc.zip
fix: always evict at least one cache entry when over max
wttrin--cleanup-cache-if-needed removed floor(count * 0.20) entries, which is 0 for a small cache (e.g. two entries at max one), so the cache could sit over its advertised maximum indefinitely. It now removes at least one whenever it is over.
Diffstat (limited to 'wttrin.el')
-rw-r--r--wttrin.el7
1 files changed, 4 insertions, 3 deletions
diff --git a/wttrin.el b/wttrin.el
index cdef10a..4bc1b3a 100644
--- a/wttrin.el
+++ b/wttrin.el
@@ -1283,12 +1283,13 @@ Returns a list where each element is a cons cell (key . timestamp)."
(defun wttrin--cleanup-cache-if-needed ()
"Remove oldest entries if cache exceeds max size.
Removes oldest entries based on `wttrin--cache-cleanup-percentage'
-when cache count exceeds `wttrin-cache-max-entries'.
+when cache count exceeds `wttrin-cache-max-entries', and always at least one
+so the cache can't sit over its maximum when the percentage floors to zero.
This creates headroom to avoid frequent cleanups."
(when (> (hash-table-count wttrin--cache) wttrin-cache-max-entries)
(let* ((entries-by-age (wttrin--get-cache-entries-by-age))
- (num-to-remove (floor (* (length entries-by-age)
- wttrin--cache-cleanup-percentage))))
+ (num-to-remove (max 1 (floor (* (length entries-by-age)
+ wttrin--cache-cleanup-percentage)))))
(dotimes (i num-to-remove)
(remhash (car (nth i entries-by-age)) wttrin--cache)))))