From 691801f96e85371b9470bbf8492fa64b4ce4e973 Mon Sep 17 00:00:00 2001 From: Jonathan Jin Date: Fri, 12 Apr 2024 13:46:15 -0400 Subject: [PATCH] support :never for resource-refresh-overrides (#177) Contributes to #172. --- docs/how-tos/customization.md | 38 +++++++++++++++++++++++++++++++++++ docs/references/changelog.md | 2 ++ kele.el | 19 ++++++++++++------ tests/unit/test-kele.el | 16 ++++++++++++--- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/docs/how-tos/customization.md b/docs/how-tos/customization.md index ba52c0a..889d0c8 100644 --- a/docs/how-tos/customization.md +++ b/docs/how-tos/customization.md @@ -58,6 +58,44 @@ the `kele-get` result buffer's keybindings. Kele provides a handful of customization variables with which you can influence [cache behavior](../explanations/design.md#caches). +### Changing resource caching expiration time + +!!! note + + Currently only namespace names are cached. + +Kele caches certain resource names upon fetching. This speeds up subsequent +queries drastically. + +These cached values have a expiration time, after which the cached values are +erased. + +You can change the default refresh interval with +`kele-resource-default-refresh-interval`. For example, a value of `60` means +that all cached values are erased 60 seconds after creation. + +### Changing resource-specific caching expiration time + +For some resources, you might expect the set of names in the cluster to change +more or less frequently than others. Some you might, for all intents and +purposes, assume **never** change. + +You can set resource-specific cache expirations with +`kele-resource-refresh-overrides`. For example, the following will set cached +names for Pods to expire after 600 seconds: + +```emacs-lisp +(setq kele-resource-refresh-overrides '((pod . 600))) +``` + +You can **also** set the special value `:never`, in which case the cached values +are **never** automatically erased once they're written. For example, the +following will set cached names for Namespaces to never expire: + +```emacs-lisp +(setq kele-resource-refresh-overrides '((namespace . :never))) +``` + ### Change the discovery cache polling interval If you'd like Kele to poll the discovery cache more or less frequently than the default, set diff --git a/docs/references/changelog.md b/docs/references/changelog.md index f3262eb..694221f 100644 --- a/docs/references/changelog.md +++ b/docs/references/changelog.md @@ -17,6 +17,8 @@ versioning][semver]. **Kubernetes** section on the menu bar - Added ability to switch contexts from within the menu bar; available contexts are shown as a sub-menu +- Added ability to specify that cached names of a specific resource should never + expire ### Fixed diff --git a/kele.el b/kele.el index d805e9c..5c89021 100644 --- a/kele.el +++ b/kele.el @@ -82,9 +82,14 @@ If a resource is listed here, the corresponding value will be used for cache time-to-live for that resource. Otherwise, `kele-resource-default-refresh-interval' is used. +If the value is :never, then the resource will be cached once and +then never expired. + Keys are the singular form of the resource name, e.g. \"pod\" for pods." - :type '(alist :key-type symbol :value-type 'integer) + :type '(alist :key-type symbol :value-type (radio + (integer :tag "Expiration duration in seconds") + (const :tag "Never expire once cached" :never))) :group 'kele) (defcustom kele-discovery-refresh-interval @@ -821,11 +826,13 @@ The cache has a TTL as defined by Returns the passed-in list of namespaces." (add-to-list 'kele--namespaces-cache `(,(intern context) . ,namespace-names)) - (run-with-timer - (kele--get-cache-ttl-for-resource 'namespace) - nil - #'kele--clear-namespaces-for-context - context) + (let ((ttl (kele--get-cache-ttl-for-resource 'namespace))) + (when (and ttl (not (eq ttl :never))) + (run-with-timer + (kele--get-cache-ttl-for-resource 'namespace) + nil + #'kele--clear-namespaces-for-context + context))) namespace-names) (cl-defstruct (kele--resource-container diff --git a/tests/unit/test-kele.el b/tests/unit/test-kele.el index bdd1526..e52b228 100644 --- a/tests/unit/test-kele.el +++ b/tests/unit/test-kele.el @@ -176,7 +176,15 @@ (describe "kele--cache-namespaces" (before-each (spy-on 'run-with-timer) - (setq kele--namespaces-cache nil)) + (setq kele--namespaces-cache nil) + (setq kele-resource-refresh-overrides nil)) + + (describe "when resource's cache TTL is set to :never" + (before-each + (setq kele-resource-refresh-overrides '((namespace . :never)))) + (it "does not create a timer" + (kele--cache-namespaces "foobar" "n0") + (expect 'run-with-timer :not :to-have-been-called))) (it "adds namespaces correctly" (kele--cache-namespaces "foobar" "n0" "n1" "n2") @@ -191,14 +199,16 @@ (describe "resource caching" (before-each (setq kele-resource-default-refresh-interval 60) - (setq kele-resource-refresh-overrides '((foo . 999)))) + (setq kele-resource-refresh-overrides '((foo . 999) + (bar . :never)))) (describe "when a resource has a TTL override" (it "uses the override value" + (expect (kele--get-cache-ttl-for-resource 'bar) :to-equal :never) (expect (kele--get-cache-ttl-for-resource 'foo) :to-equal 999))) (describe "when a resource has no TTL override" (it "uses the default value" - (expect (kele--get-cache-ttl-for-resource 'bar) :to-equal 60)))) + (expect (kele--get-cache-ttl-for-resource 'qux) :to-equal 60)))) (describe "kele--cache-update (kele--discovery-cache)" (describe "the retval"