The type of elements in the array / 数组元素的类型
The function to memoize. 要记忆化的函数
Optionaloptions: MemoizeOptions<T>
Options for memoization. 记忆化配置
OptionalkeyResolver?: (...args: Parameters<T>) => stringCustom key resolver. By default, a fast key is generated for a single
number/string/boolean argument, and other arguments are serialized via JSON.
自定义 key 生成器。默认情况下,单个 number/string/boolean 参数生成快速 key,其余参数使用 JSON 序列化
OptionalmaxSize?: numberMaximum number of cached entries. When the limit is exceeded, the least recently used entry is evicted (LRU). Cache hits refresh the recency order. 缓存条目最大数量。超出上限时淘汰最久未使用的条目(LRU);命中会刷新使用顺序
Optionalttl?: numberTime-to-live in milliseconds. If set, cache expires after this duration. 缓存有效期(毫秒)。设置后缓存将在此时间后过期
A memoized version of the function. 记忆化后的函数
When neither maxSize nor ttl is set, the cache grows without bound.
For long-running applications, it is strongly recommended to set at least one of these
options to prevent memory leaks.
当 maxSize 和 ttl 都未设置时,缓存会无限增长。对于长期运行的应用,
强烈建议至少设置其中一个选项以防止内存泄漏。
Eviction is LRU (least recently used): a cache hit promotes the entry to the
most-recently-used position, and when maxSize is exceeded the least recently used
key is evicted, so frequently accessed values stay cached.
淘汰策略为 LRU(最近最少使用):命中会把条目提升为最近使用,超出 maxSize 时
淘汰最久未使用的 key,因此高频访问的值会保留在缓存中。
The default key resolver generates a fast key (prefixed with the value type) for a single
number/string/boolean argument; other arguments fall back to JSON.stringify, which
has limitations:
TypeError{a:1,b:2} ≠ {b:2,a:1})undefined, functions, and Symbols are ignored or converted to nullJSON.stringify([1]) vs JSON.stringify({"0":1}))
Use keyResolver for more robust key generation.默认的 key 生成器对单个 number/string/boolean 参数生成快速 key(带类型前缀);
其余参数回退到 JSON.stringify,存在以下限制:
TypeError{a:1,b:2} ≠ {b:2,a:1})undefined、函数和 Symbol 会被忽略或转为 nullkeyResolver 进行更健壮的 key 生成。const add = (a: number, b: number) => a + b
const memoizedAdd = memoize(add)
memoizedAdd(1, 2) // => 3, computed
memoizedAdd(1, 2) // => 3, cached
memoizedAdd(2, 1) // => 3, different args, computed
With TTL (expires after 1000ms) / 缓存有效期(毫秒)。
const fn = memoize(someExpensiveFn, { ttl: 1000 })
fn('key') // computed
fn('key') // cached (within TTL)
With maxSize (LRU eviction when limit exceeded) / 最大缓存条目数量(超出上限时 LRU 淘汰)
const fn = memoize(someExpensiveFn, { maxSize: 100 })
Memoize a function, caching its results based on arguments. Supports max cache size and TTL (time-to-live) expiration.
记忆化函数,缓存基于参数的结果。支持最大缓存大小和 TTL 过期机制