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, arguments are serialized via JSON. 自定义 key 生成器。默认使用 JSON 序列化参数
OptionalmaxSize?: numberMaximum number of cached entries. 缓存条目最大数量
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 都未设置时,缓存会无限增长。对于长期运行的应用,
强烈建议至少设置其中一个选项以防止内存泄漏。
The default key resolver uses 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 生成器使用 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 过期机制