Skip to content

Commit

Permalink
support :never for resource-refresh-overrides (#177)
Browse files Browse the repository at this point in the history
Contributes to #172.
  • Loading branch information
jinnovation authored Apr 12, 2024
1 parent 691608e commit 691801f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
38 changes: 38 additions & 0 deletions docs/how-tos/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/references/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 13 additions & 6 deletions kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions tests/unit/test-kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"
Expand Down

0 comments on commit 691801f

Please sign in to comment.