Skip to content

Commit

Permalink
refactor: key -> hash
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Dec 28, 2024
1 parent d12203b commit d0eae88
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ export function memoizeFactory({
return function (this: This): Return {
console.log(`Entering getter ${String(context.name)}.`);

const key = calcHash(this, counter, []);
const hash = calcHash(this, counter, []);
const now = Date.now();

// Check in-memory cache first
if (cache.has(key)) {
const [cachedValue, cachedAt] = cache.get(key) as [Return, number];
if (cache.has(hash)) {
const [cachedValue, cachedAt] = cache.get(hash) as [Return, number];
if (now - cachedAt <= cacheDuration) {
console.log(`Exiting getter ${String(context.name)}.`);
return cachedValue;
}

cache.delete(key);
cache.delete(hash);
try {
const promise = removeCache?.(key) as unknown;
const promise = removeCache?.(hash) as unknown;
if (promise instanceof Promise) promise.catch(noop);
} catch {
// do nothing.
Expand All @@ -87,16 +87,16 @@ export function memoizeFactory({

// Try reading from persistent cache
try {
const persistentCache = tryReadingCache?.(key);
const persistentCache = tryReadingCache?.(hash);
if (persistentCache) {
const [cachedAt, cachedValue] = persistentCache;
if (now - cachedAt <= cacheDuration) {
cache.set(key, [cachedValue as Return, cachedAt]);
cache.set(hash, [cachedValue as Return, cachedAt]);
console.log(`Exiting getter ${String(context.name)}.`);
return cachedValue as Return;
}

const promise = removeCache?.(key) as unknown;
const promise = removeCache?.(hash) as unknown;
if (promise instanceof Promise) promise.catch(noop);
}
} catch {
Expand All @@ -114,19 +114,19 @@ export function memoizeFactory({
// do nothing.
}
}
cache.set(key, [result, now]);
cache.set(hash, [result, now]);
if (result instanceof Promise) {
void (async () => {
try {
const promise = persistCache?.(key, now, result) as unknown;
const promise = persistCache?.(hash, now, result) as unknown;
if (promise instanceof Promise) promise.catch(noop);
} catch {
// do nothing.
}
});
} else {
try {
const promise = persistCache?.(key, now, result) as unknown;
const promise = persistCache?.(hash, now, result) as unknown;
if (promise instanceof Promise) promise.catch(noop);
} catch {
// do nothing.
Expand All @@ -145,20 +145,20 @@ export function memoizeFactory({
`Entering ${context ? `method ${String(context.name)}` : 'function'}(${calcHash(this, counter, args)}).`
);

const key = calcHash(this, counter, args);
const hash = calcHash(this, counter, args);
const now = Date.now();

// Check in-memory cache first
if (cache.has(key)) {
const [cachedValue, cachedAt] = cache.get(key) as [Return, number];
if (cache.has(hash)) {
const [cachedValue, cachedAt] = cache.get(hash) as [Return, number];
if (now - cachedAt <= cacheDuration) {
console.log(`Exiting ${context ? `method ${String(context.name)}` : 'function'}.`);
return cachedValue;
}

cache.delete(key);
cache.delete(hash);
try {
const promise = removeCache?.(key) as unknown;
const promise = removeCache?.(hash) as unknown;
if (promise instanceof Promise) promise.catch(noop);
} catch {
// do nothing.
Expand All @@ -167,16 +167,16 @@ export function memoizeFactory({

// Try reading from persistent cache
try {
const persistentCache = tryReadingCache?.(key);
const persistentCache = tryReadingCache?.(hash);
if (persistentCache) {
const [cachedAt, cachedValue] = persistentCache;
if (now - cachedAt <= cacheDuration) {
cache.set(key, [cachedValue as Return, cachedAt]);
cache.set(hash, [cachedValue as Return, cachedAt]);
console.log(`Exiting ${context ? `method ${String(context.name)}` : 'function'}.`);
return cachedValue as Return;
}

const promise = removeCache?.(key) as unknown;
const promise = removeCache?.(hash) as unknown;
if (promise instanceof Promise) promise.catch(noop);
}
} catch {
Expand All @@ -197,19 +197,19 @@ export function memoizeFactory({
// do nothing.
}
}
cache.set(key, [result, now]);
cache.set(hash, [result, now]);
if (result instanceof Promise) {
void (async () => {
try {
const promise = persistCache?.(key, now, result) as unknown;
const promise = persistCache?.(hash, now, result) as unknown;
if (promise instanceof Promise) promise.catch(noop);
} catch {
// do nothing.
}
});
} else {
try {
const promise = persistCache?.(key, now, result) as unknown;
const promise = persistCache?.(hash, now, result) as unknown;
if (promise instanceof Promise) promise.catch(noop);
} catch {
// do nothing.
Expand Down

0 comments on commit d0eae88

Please sign in to comment.