From c6dbf4f3661848b4bade56136eb3bdf2984dc8d0 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 3 Jan 2025 23:38:31 +0900 Subject: [PATCH] fix: rename maxCachedArgsSize to maxCacheSizePerTarget --- src/memoize.ts | 12 ++++++------ src/memoizeWithPersistentCache.ts | 10 +++++----- tests/unit/memoize.test.ts | 4 ++-- tests/unit/memoizeWithPersistentCache.test.ts | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/memoize.ts b/src/memoize.ts index 9cd4160..f2e24e1 100644 --- a/src/memoize.ts +++ b/src/memoize.ts @@ -3,7 +3,7 @@ import { calcHashWithContext } from './caclHash.js'; /** * A memoization decorator/function that caches the results of method/getter/function calls to improve performance. * This decorator/function can be applied to methods and getters in a class as a decorator, and functions without context as a function. - * The cache size is limited by `maxCachedArgsSize` parameter (100 by default) in `memoizeFactory` function. + * The cache size is limited by `maxCacheSizePerTarget` parameter (100 by default) in `memoizeFactory` function. * When the cache size exceeds its limit, the oldest cached value is removed. * * @template This The type of the `this` context within the method, getter or function. @@ -24,7 +24,7 @@ export const memoize = memoizeFactory(); * @template Args - The types of the arguments to the method, getter or function. * @template Return - The return type of the method, getter or function. * @param {Object} options - The options for the memoize function. - * @param {number} [options.maxCachedArgsSize=100] - The maximum number of distinct values that can be cached. + * @param {number} [options.maxCacheSizePerTarget=100] - The maximum number of distinct values that can be cached. * @param {number} [options.cacheDuration=Number.POSITIVE_INFINITY] - The maximum number of milliseconds that a cached value is valid. * @param {Function} [options.calcHash] - A function to calculate the hash for a given context and arguments. Defaults to hashing the stringified context and arguments. * @param {Map[]} [options.caches] - An array of maps to store cached values. Useful for tracking and clearing caches externally. @@ -34,12 +34,12 @@ export function memoizeFactory({ cacheDuration = Number.POSITIVE_INFINITY, caches, calcHash = calcHashWithContext, - maxCachedArgsSize = 100, + maxCacheSizePerTarget = 100, }: { cacheDuration?: number; caches?: Map[]; calcHash?: (self: unknown, args: unknown[]) => string; - maxCachedArgsSize?: number; + maxCacheSizePerTarget?: number; } = {}) { return function memoize( target: ((this: This, ...args: Args) => Return) | ((...args: Args) => Return) | keyof This, @@ -64,7 +64,7 @@ export function memoizeFactory({ } const result = (target as (this: This) => Return).call(this); - if (cache.size >= maxCachedArgsSize) { + if (cache.size >= maxCacheSizePerTarget) { const oldestKey = cache.keys().next().value as string; cache.delete(oldestKey); } @@ -88,7 +88,7 @@ export function memoizeFactory({ ? (target as (this: This, ...args: Args) => Return).call(this, ...args) : (target as (...args: Args) => Return)(...args); - if (cache.size >= maxCachedArgsSize) { + if (cache.size >= maxCacheSizePerTarget) { const oldestKey = cache.keys().next().value as string; cache.delete(oldestKey); } diff --git a/src/memoizeWithPersistentCache.ts b/src/memoizeWithPersistentCache.ts index 0246935..a4c2978 100644 --- a/src/memoizeWithPersistentCache.ts +++ b/src/memoizeWithPersistentCache.ts @@ -7,7 +7,7 @@ import { sha3_512 } from './hash.js'; * @template Args - The types of the arguments to the method, getter or function. * @template Return - The return type of the method, getter or function. * @param {Object} options - The options for the memoize function. - * @param {number} [options.maxCachedArgsSize=100] - The maximum number of distinct values that can be cached. + * @param {number} [options.maxCacheSizePerTarget=100] - The maximum number of distinct values that can be cached. * @param {number} [options.cacheDuration=Number.POSITIVE_INFINITY] - The maximum number of milliseconds that a cached value is valid. * @param {Function} [options.calcHash] - A function to calculate the hash for a given context and arguments. Defaults to hashing the stringified context and arguments. * @param {Map[]} [options.caches] - An array of maps to store cached values. @@ -20,7 +20,7 @@ export function memoizeWithPersistentCacheFactory({ cacheDuration = Number.POSITIVE_INFINITY, caches, calcHash = (self, args) => sha3_512(JSON.stringify([self, args])), - maxCachedArgsSize = 100, + maxCacheSizePerTarget = 100, persistCache, removeCache, tryReadingCache, @@ -28,7 +28,7 @@ export function memoizeWithPersistentCacheFactory({ cacheDuration?: number; caches?: Map[]; calcHash?: (self: unknown, args: unknown) => string; - maxCachedArgsSize?: number; + maxCacheSizePerTarget?: number; persistCache: (persistentKey: string, hash: string, value: unknown, currentTime: number) => void; removeCache: (persistentKey: string, hash: string) => void; tryReadingCache: (persistentKey: string, hash: string) => [unknown, number] | undefined; @@ -83,7 +83,7 @@ export function memoizeWithPersistentCacheFactory({ } const result = (target as (this: This) => Return).call(this); - if (cache.size >= maxCachedArgsSize) { + if (cache.size >= maxCacheSizePerTarget) { const oldestKey = cache.keys().next().value as string; cache.delete(oldestKey); try { @@ -155,7 +155,7 @@ export function memoizeWithPersistentCacheFactory({ ? (target as (this: This, ...args: Args) => Return).call(this, ...args) : (target as (...args: Args) => Return)(...args); - if (cache.size >= maxCachedArgsSize) { + if (cache.size >= maxCacheSizePerTarget) { const oldestKey = cache.keys().next().value as string; cache.delete(oldestKey); try { diff --git a/tests/unit/memoize.test.ts b/tests/unit/memoize.test.ts index 2947555..57c1d5a 100644 --- a/tests/unit/memoize.test.ts +++ b/tests/unit/memoize.test.ts @@ -41,7 +41,7 @@ describe('memory cache', () => { const random2 = new RandomChild(10); const nextInteger1 = memoize((base: number = 1): number => base + getNextInteger()); - const nextInteger2 = memoizeFactory({ maxCachedArgsSize: 10, cacheDuration: -1 })( + const nextInteger2 = memoizeFactory({ maxCacheSizePerTarget: 10, cacheDuration: -1 })( (base: number = 1): number => base + getNextInteger() ); const nextInteger3 = memoizeFactory({ cacheDuration: 200 })((base: number = 1): number => base + getNextInteger()); @@ -125,7 +125,7 @@ describe('memory cache', () => { expect(nextInteger3()).toBe(second); }); - const memoizeOneValue = memoizeFactory({ maxCachedArgsSize: 1 }); + const memoizeOneValue = memoizeFactory({ maxCacheSizePerTarget: 1 }); class Klass { @memoizeOneValue get obj(): Record { diff --git a/tests/unit/memoizeWithPersistentCache.test.ts b/tests/unit/memoizeWithPersistentCache.test.ts index e48a690..399e0d7 100644 --- a/tests/unit/memoizeWithPersistentCache.test.ts +++ b/tests/unit/memoizeWithPersistentCache.test.ts @@ -103,12 +103,12 @@ describe('persistent cache', () => { expect(nextIntegerWithPersistence(200)).toBe(value2); }); - test('remove oldest cache entry when maxCachedArgsSize is reached', () => { + test('remove oldest cache entry when maxCacheSizePerTarget is reached', () => { const withSizeLimit = memoizeWithPersistentCacheFactory({ persistCache, tryReadingCache, removeCache, - maxCachedArgsSize: 2, + maxCacheSizePerTarget: 2, })('nextInteger')((base: number = 1): number => base + getNextInteger()); const value1 = withSizeLimit(100);