src/platform/packages/shared/kbn-cache-cli/README.md
Centralised caching helpers for scripts and CLIs in the Kibana repo.
The goal is to make it easy for engineers to cache computationally or I/O expensive operations on disk, or in the future, possible remote.
import { fromCache, createLocalDirDiskCacheStore } from '@kbn/cache-cli';
import { createCache } from 'cache-manager';
const DOC_CACHE = createCache({
stores: [createLocalDirDiskCacheStore({ dir: 'my_docs', ttl: 60 * 60 /* 1h */ })],
});
const docs = await fromCache('docs', DOC_CACHE, async () => fetchDocs());
fromCache(key, cache, cb, validator?) semantics:
cache.get(key) (skipped when process.env.DISABLE_KBN_CACHE is truthy).validator(cached) – return false to force a refresh.cb() if the cache miss / invalid.cache.set(key, value) and returns it.@kbn/cache-cli wraps cache-manager so any Keyv compatible store works. The helpers below ship out-of-the-box:
| Helper | Backing store | Typical use-case |
|---|---|---|
createLocalDirDiskCacheStore({ dir, ttl? }) | cache-manager-fs-hash on <REPO_ROOT>/data/{dir} | Persist in ./data with an unknown ttl |
createTmpDirDiskCacheStore({ dir, ttl? }) | cache-manager-fs-hash on <OS_TMP_DIR>/{dir} | Persist in os tmp dir which might be cleared over restarts |
DISABLE_KBN_CACHE=true to force fresh data (useful in CI workflows).ttl when creating a store to let the backend expire entries automatically.cacheValidator callback to fromCache(); it receives the cached value and should return true when it is still valid.data/ if you need a hard reset.Choose whichever fits your script. They can be combined (e.g. a TTL plus a validator).