smart-components
Hooks

useLRU

Minimal in-memory LRU cache hook. Stable identity across renders.

import { useLRU } from '@extedcoud/smart-components';

const cache = useLRU<string, string>(16);
cache.set('key', 'value');
cache.get('key'); // 'value' — bumps to most-recent

Used internally by useGhostCompletion, useSuggestionList, useRewrite, and useSmartState to memoize completions keyed by (prompt, context, ...). Stable identity across renders so it can be passed to effect deps without restarting fetches.

Methods

MethodTypeDescription
get(key: K) => V | undefinedReturns cached value and bumps to most-recent.
set(key: K, value: V) => voidInserts; evicts oldest if over capacity.
has(key: K) => booleanWithout bumping recency.
delete(key: K) => voidRemove a single key.
clear() => voidEmpty the cache.

Tips

  • Capacity defaults to 16 — small but big enough for the typical "user typing in a textbox" workload.
  • The cache lives in a useState(() => new LRU()) cell, so it survives across renders within a component but resets on unmount.

On this page