Function: projection--cache-get-with-predicate
projection--cache-get-with-predicate is a natively compiled function
defined in projection-core-cache.el.
Signature
(projection--cache-get-with-predicate PROJECT KEY PREDICATE BODY)
Documentation
Get a value from the projection cache and reset it if stale.
This is a helper function to both retrieve and set a cache entry for KEY in the current PROJECT. If a cache entry for KEY exists in the current project and its newer than the PREDICATE value then it will be returned. Otherwise BODY will be called to determine a new value and it will be saved alongside PREDICATE for a later retrieval.
PREDICATE should be a comparable value that will change when the value in the cache is stale. For example PREDICATE could be the modification time of a file used by BODY. If the file is unchanged the cache is up to date. If the FILE is modified PREDICATE will be updated and the CACHE is stale. PREDICATE can be set to nil to never cache the result and always call BODY. It can instead be set to t to always cache. PREDICATE can also be a function that is supplied a plist containing a :value and :stamp field and should return one of the aforementioned PREDICATE values.
PROJECT is an optional argument. When set to nil calling this function is essentially the same as just calling BODY directly.
Source Code
;; Defined in /nix/store/j6y921qbdmdqxjnd43iibapbig3p4hai-emacs-packages-deps/share/emacs/site-lisp/elpa/projection-20260614.1822/projection-core-cache.el
(defun projection--cache-get-with-predicate (project key predicate body)
"Get a value from the `projection' cache and reset it if stale.
This is a helper function to both retrieve and set a cache entry for KEY in
the current PROJECT. If a cache entry for KEY exists in the current project
and its newer than the PREDICATE value then it will be returned. Otherwise
BODY will be called to determine a new value and it will be saved alongside
PREDICATE for a later retrieval.
PREDICATE should be a comparable value that will change when the value
in the cache is stale. For example PREDICATE could be the modification
time of a file used by BODY. If the file is unchanged the cache is up to
date. If the FILE is modified PREDICATE will be updated and the CACHE is
stale. PREDICATE can be set to nil to never cache the result and always
call BODY. It can instead be set to t to always cache. PREDICATE can also
be a function that is supplied a plist containing a :value and :stamp
field and should return one of the aforementioned PREDICATE values.
PROJECT is an optional argument. When set to nil calling this function is
essentially the same as just calling BODY directly."
(let ((cached-entry (when project
(projection--cache-get project key))))
(when cached-entry
(cl-assert (projection--cache-get-with-predicate-cached-p cached-entry)
'show-args "cache-get-with-predicate called on cache entry with \
value not set by cache-get-with-predicate"))
(when (functionp predicate)
(setq predicate
(if cached-entry
(funcall predicate
(list :value (projection--cache-get-with-predicate-value cached-entry)
:stamp (projection--cache-get-with-predicate-stamp cached-entry)))
(projection--cache-now))))
(if (and cached-entry
(if (and (numberp predicate)
(numberp (projection--cache-get-with-predicate-stamp cached-entry)))
(<= predicate (projection--cache-get-with-predicate-stamp cached-entry))
predicate))
(projection--cache-get-with-predicate-value cached-entry)
(let ((resolved-value (funcall body)))
(when (and project predicate resolved-value)
(projection--cache-put
project key (projection--cache-get-with-predicate-create predicate resolved-value)))
resolved-value))))