Skip to content

Commit

Permalink
fix: rename maxCachedArgsSize to maxCacheSizePerTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Jan 3, 2025
1 parent d76ea9c commit c6dbf4f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<string, [unknown, number]>[]} [options.caches] - An array of maps to store cached values. Useful for tracking and clearing caches externally.
Expand All @@ -34,12 +34,12 @@ export function memoizeFactory({
cacheDuration = Number.POSITIVE_INFINITY,
caches,
calcHash = calcHashWithContext,
maxCachedArgsSize = 100,
maxCacheSizePerTarget = 100,
}: {
cacheDuration?: number;
caches?: Map<string, [unknown, number]>[];
calcHash?: (self: unknown, args: unknown[]) => string;
maxCachedArgsSize?: number;
maxCacheSizePerTarget?: number;
} = {}) {
return function memoize<This, Args extends unknown[], Return>(
target: ((this: This, ...args: Args) => Return) | ((...args: Args) => Return) | keyof This,
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/memoizeWithPersistentCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, [unknown, number]>[]} [options.caches] - An array of maps to store cached values.
Expand All @@ -20,15 +20,15 @@ export function memoizeWithPersistentCacheFactory({
cacheDuration = Number.POSITIVE_INFINITY,
caches,
calcHash = (self, args) => sha3_512(JSON.stringify([self, args])),
maxCachedArgsSize = 100,
maxCacheSizePerTarget = 100,
persistCache,
removeCache,
tryReadingCache,
}: {
cacheDuration?: number;
caches?: Map<string, [unknown, number]>[];
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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/memoize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<string, string> {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/memoizeWithPersistentCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c6dbf4f

Please sign in to comment.