Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc9ce447

Browse files
LetmaxAge option accept a function (#105)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parentff0f539 commitc9ce447

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

‎index.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ export type Options<
2525
/**
2626
Milliseconds until the cache entry expires.
2727
28+
If a function is provided, it receives the arguments and must return the max age.
29+
2830
@default Infinity
2931
*/
30-
readonlymaxAge?:number;
32+
readonlymaxAge?:number|((...arguments_:Parameters<FunctionToMemoize>)=>number);
3133

3234
/**
3335
Determines the cache key for storing the result based on the function arguments. By default, __only the first argument is considered__ and it only works with [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
@@ -68,7 +70,7 @@ export type Options<
6870
/**
6971
[Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input.
7072
71-
@paramfn - The function to be memoized.
73+
@paramfunction_ - The function to be memoized.
7274
7375
@example
7476
```
@@ -129,15 +131,17 @@ export default function memoize<
129131

130132
constresult=function_.apply(this,arguments_)asReturnType<FunctionToMemoize>;
131133

134+
constcomputedMaxAge=typeofmaxAge==='function' ?maxAge(...arguments_) :maxAge;
135+
132136
cache.set(key,{
133137
data:result,
134-
maxAge:maxAge ?Date.now()+maxAge :Number.POSITIVE_INFINITY,
138+
maxAge:computedMaxAge ?Date.now()+computedMaxAge :Number.POSITIVE_INFINITY,
135139
});
136140

137-
if(typeofmaxAge==='number'&&maxAge!==Number.POSITIVE_INFINITY){
141+
if(computedMaxAge&&computedMaxAge>0&&computedMaxAge!==Number.POSITIVE_INFINITY){
138142
consttimer=setTimeout(()=>{
139143
cache.delete(key);
140-
},maxAge);
144+
},computedMaxAge);
141145

142146
timer.unref?.();
143147

@@ -221,7 +225,7 @@ export function memoizeDecorator<
221225
/**
222226
Clear all cached data of a memoized function.
223227
224-
@paramfn - The memoized function.
228+
@paramfunction_ - The memoized function.
225229
*/
226230
exportfunctionmemoizeClear(function_:AnyFunction):void{
227231
constcache=cacheStore.get(function_);

‎readme.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,14 @@ Type: `object`
183183

184184
#####maxAge
185185

186-
Type:`number`\
187-
Default:`Infinity`
186+
Type:`number` |`Function`\
187+
Default:`Infinity`\
188+
Example:`arguments_ => arguments_ < new Date() ? Infinity : 60_000`
188189

189190
Milliseconds until the cache entry expires.
190191

192+
If a function is provided, it receives the arguments and must return the max age.
193+
191194
#####cacheKey
192195

193196
Type:`Function`\

‎test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,21 @@ test('maxAge - high concurrency', async t => {
339339
awaitdelay(100);
340340
t.is(memoized(),1);
341341
});
342+
343+
test('maxAge dependent on function parameters',asynct=>{
344+
letindex=0;
345+
constfixture=(x:number)=>index++;
346+
constmemoized=memoize(fixture,{
347+
maxAge:x=>x*100,
348+
});
349+
350+
t.is(memoized(1),0);// Initial call, cached
351+
awaitdelay(50);
352+
t.is(memoized(1),0);// Still cached
353+
awaitdelay(60);
354+
t.is(memoized(1),1);// Cache expired, should compute again
355+
356+
t.is(memoized(2),2);// Initial call with different parameter, cached
357+
awaitdelay(210);
358+
t.is(memoized(2),3);// Cache expired, should compute again
359+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp