@extra-memoize/memory-cache 0.2.11 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/caches/cache.js +24 -0
- package/lib/caches/cache.js.map +1 -0
- package/lib/caches/expirable-cache-with-stale-if-error.js +35 -0
- package/lib/caches/expirable-cache-with-stale-if-error.js.map +1 -0
- package/lib/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.js +39 -0
- package/lib/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.js.map +1 -0
- package/lib/{es2018/caches → caches}/expirable-cache-with-stale-while-revalidate.js +8 -12
- package/lib/caches/expirable-cache-with-stale-while-revalidate.js.map +1 -0
- package/lib/caches/expirable-cache.js +23 -0
- package/lib/caches/expirable-cache.js.map +1 -0
- package/lib/caches/index.d.ts +10 -0
- package/lib/caches/index.js +11 -0
- package/lib/caches/index.js.map +1 -0
- package/lib/caches/lru-cache.js +22 -0
- package/lib/caches/lru-cache.js.map +1 -0
- package/lib/caches/tlru-cache-with-stale-if-error.js +35 -0
- package/lib/caches/tlru-cache-with-stale-if-error.js.map +1 -0
- package/lib/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.js +39 -0
- package/lib/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.js.map +1 -0
- package/lib/{es2018/caches → caches}/tlru-cache-with-stale-while-revalidate.js +8 -12
- package/lib/caches/tlru-cache-with-stale-while-revalidate.js.map +1 -0
- package/lib/caches/tlru-cache.js +23 -0
- package/lib/caches/tlru-cache.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -0
- package/package.json +33 -35
- package/src/caches/expirable-cache-with-stale-if-error.ts +4 -4
- package/src/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.ts +5 -5
- package/src/caches/expirable-cache-with-stale-while-revalidate.ts +1 -1
- package/src/caches/index.ts +10 -10
- package/src/caches/tlru-cache-with-stale-if-error.ts +4 -4
- package/src/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.ts +5 -5
- package/src/caches/tlru-cache-with-stale-while-revalidate.ts +1 -1
- package/src/index.ts +1 -1
- package/lib/es2015/caches/cache.js +0 -28
- package/lib/es2015/caches/cache.js.map +0 -1
- package/lib/es2015/caches/expirable-cache-with-stale-if-error.js +0 -38
- package/lib/es2015/caches/expirable-cache-with-stale-if-error.js.map +0 -1
- package/lib/es2015/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.js +0 -42
- package/lib/es2015/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.js.map +0 -1
- package/lib/es2015/caches/expirable-cache-with-stale-while-revalidate.js +0 -39
- package/lib/es2015/caches/expirable-cache-with-stale-while-revalidate.js.map +0 -1
- package/lib/es2015/caches/expirable-cache.js +0 -27
- package/lib/es2015/caches/expirable-cache.js.map +0 -1
- package/lib/es2015/caches/index.d.ts +0 -10
- package/lib/es2015/caches/index.js +0 -27
- package/lib/es2015/caches/index.js.map +0 -1
- package/lib/es2015/caches/lru-cache.js +0 -26
- package/lib/es2015/caches/lru-cache.js.map +0 -1
- package/lib/es2015/caches/tlru-cache-with-stale-if-error.js +0 -38
- package/lib/es2015/caches/tlru-cache-with-stale-if-error.js.map +0 -1
- package/lib/es2015/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.js +0 -42
- package/lib/es2015/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.js.map +0 -1
- package/lib/es2015/caches/tlru-cache-with-stale-while-revalidate.js +0 -39
- package/lib/es2015/caches/tlru-cache-with-stale-while-revalidate.js.map +0 -1
- package/lib/es2015/caches/tlru-cache.js +0 -27
- package/lib/es2015/caches/tlru-cache.js.map +0 -1
- package/lib/es2015/index.d.ts +0 -1
- package/lib/es2015/index.js +0 -18
- package/lib/es2015/index.js.map +0 -1
- package/lib/es2018/caches/cache.d.ts +0 -8
- package/lib/es2018/caches/cache.js +0 -28
- package/lib/es2018/caches/cache.js.map +0 -1
- package/lib/es2018/caches/expirable-cache-with-stale-if-error.d.ts +0 -9
- package/lib/es2018/caches/expirable-cache-with-stale-if-error.js +0 -38
- package/lib/es2018/caches/expirable-cache-with-stale-if-error.js.map +0 -1
- package/lib/es2018/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.d.ts +0 -13
- package/lib/es2018/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.js +0 -42
- package/lib/es2018/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.js.map +0 -1
- package/lib/es2018/caches/expirable-cache-with-stale-while-revalidate.d.ts +0 -10
- package/lib/es2018/caches/expirable-cache-with-stale-while-revalidate.js.map +0 -1
- package/lib/es2018/caches/expirable-cache.d.ts +0 -9
- package/lib/es2018/caches/expirable-cache.js +0 -27
- package/lib/es2018/caches/expirable-cache.js.map +0 -1
- package/lib/es2018/caches/index.d.ts +0 -10
- package/lib/es2018/caches/index.js +0 -27
- package/lib/es2018/caches/index.js.map +0 -1
- package/lib/es2018/caches/lru-cache.d.ts +0 -8
- package/lib/es2018/caches/lru-cache.js +0 -26
- package/lib/es2018/caches/lru-cache.js.map +0 -1
- package/lib/es2018/caches/tlru-cache-with-stale-if-error.d.ts +0 -9
- package/lib/es2018/caches/tlru-cache-with-stale-if-error.js +0 -38
- package/lib/es2018/caches/tlru-cache-with-stale-if-error.js.map +0 -1
- package/lib/es2018/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.d.ts +0 -13
- package/lib/es2018/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.js +0 -42
- package/lib/es2018/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.js.map +0 -1
- package/lib/es2018/caches/tlru-cache-with-stale-while-revalidate.d.ts +0 -15
- package/lib/es2018/caches/tlru-cache-with-stale-while-revalidate.js.map +0 -1
- package/lib/es2018/caches/tlru-cache.d.ts +0 -9
- package/lib/es2018/caches/tlru-cache.js +0 -27
- package/lib/es2018/caches/tlru-cache.js.map +0 -1
- package/lib/es2018/index.d.ts +0 -1
- package/lib/es2018/index.js +0 -18
- package/lib/es2018/index.js.map +0 -1
- /package/lib/{es2015/caches → caches}/cache.d.ts +0 -0
- /package/lib/{es2015/caches → caches}/expirable-cache-with-stale-if-error.d.ts +0 -0
- /package/lib/{es2015/caches → caches}/expirable-cache-with-stale-while-revalidate-and-stale-if-error.d.ts +0 -0
- /package/lib/{es2015/caches → caches}/expirable-cache-with-stale-while-revalidate.d.ts +0 -0
- /package/lib/{es2015/caches → caches}/expirable-cache.d.ts +0 -0
- /package/lib/{es2015/caches → caches}/lru-cache.d.ts +0 -0
- /package/lib/{es2015/caches → caches}/tlru-cache-with-stale-if-error.d.ts +0 -0
- /package/lib/{es2015/caches → caches}/tlru-cache-with-stale-while-revalidate-and-stale-if-error.d.ts +0 -0
- /package/lib/{es2015/caches → caches}/tlru-cache-with-stale-while-revalidate.d.ts +0 -0
- /package/lib/{es2015/caches → caches}/tlru-cache.d.ts +0 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
import { State } from 'extra-memoize';
|
2
|
+
export class Cache {
|
3
|
+
constructor() {
|
4
|
+
this.map = new Map();
|
5
|
+
}
|
6
|
+
set(key, value) {
|
7
|
+
this.map.set(key, value);
|
8
|
+
}
|
9
|
+
get(key) {
|
10
|
+
if (this.map.has(key)) {
|
11
|
+
return [State.Hit, this.map.get(key)];
|
12
|
+
}
|
13
|
+
else {
|
14
|
+
return [State.Miss];
|
15
|
+
}
|
16
|
+
}
|
17
|
+
delete(key) {
|
18
|
+
this.map.delete(key);
|
19
|
+
}
|
20
|
+
clear() {
|
21
|
+
this.map.clear();
|
22
|
+
}
|
23
|
+
}
|
24
|
+
//# sourceMappingURL=cache.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../src/caches/cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,EAAE,MAAM,eAAe,CAAA;AAE7C,MAAM,OAAO,KAAK;IAAlB;QACU,QAAG,GAAG,IAAI,GAAG,EAAE,CAAA;IAqBzB,CAAC;IAnBC,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IAC1B,CAAC;IAED,GAAG,CAAC,GAAW;QACb,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACtB,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;QACvC,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;IACH,CAAC;IAED,MAAM,CAAC,GAAW;QAChB,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IACtB,CAAC;IAED,KAAK;QACH,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;IAClB,CAAC;CACF"}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
import { ExpirableCache } from './expirable-cache.js';
|
2
|
+
import { State } from 'extra-memoize';
|
3
|
+
export class ExpirableCacheWithStaleIfError {
|
4
|
+
constructor(timeToLive, staleIfError) {
|
5
|
+
this.timeToLive = timeToLive;
|
6
|
+
this.staleIfError = staleIfError;
|
7
|
+
this.cache = new ExpirableCache(timeToLive + staleIfError);
|
8
|
+
}
|
9
|
+
set(key, value) {
|
10
|
+
this.cache.set(key, {
|
11
|
+
value,
|
12
|
+
updatedAt: Date.now()
|
13
|
+
});
|
14
|
+
}
|
15
|
+
get(key) {
|
16
|
+
const [state, record] = this.cache.get(key);
|
17
|
+
if (state === State.Miss) {
|
18
|
+
return [State.Miss];
|
19
|
+
}
|
20
|
+
else {
|
21
|
+
const timestamp = Date.now();
|
22
|
+
if (record.updatedAt + this.timeToLive > timestamp) {
|
23
|
+
return [State.Hit, record.value];
|
24
|
+
}
|
25
|
+
else if (record.updatedAt + this.timeToLive + this.staleIfError > timestamp) {
|
26
|
+
return [State.StaleIfError, record.value];
|
27
|
+
}
|
28
|
+
else {
|
29
|
+
// just in case
|
30
|
+
return [State.Miss];
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
}
|
35
|
+
//# sourceMappingURL=expirable-cache-with-stale-if-error.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"expirable-cache-with-stale-if-error.js","sourceRoot":"","sources":["../../src/caches/expirable-cache-with-stale-if-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAsB,KAAK,EAAE,MAAM,eAAe,CAAA;AAOzD,MAAM,OAAO,8BAA8B;IAGzC,YAAoB,UAAkB,EAAU,YAAoB;QAAhD,eAAU,GAAV,UAAU,CAAQ;QAAU,iBAAY,GAAZ,YAAY,CAAQ;QAClE,IAAI,CAAC,KAAK,GAAG,IAAI,cAAc,CAAC,UAAU,GAAG,YAAY,CAAC,CAAA;IAC5D,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;YAClB,KAAK;YACL,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC,CAAA;IACJ,CAAC;IAED,GAAG,CAAC,GAAW;QACb,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,KAAK,KAAK,KAAK,CAAC,IAAI,EAAE,CAAC;YACzB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;aAAM,CAAC;YACN,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;YAC5B,IAAI,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,SAAS,EAAE,CAAC;gBACnD,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YAClC,CAAC;iBAAM,IAAI,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,YAAY,GAAG,SAAS,EAAE,CAAC;gBAC9E,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YAC3C,CAAC;iBAAM,CAAC;gBACN,eAAe;gBACf,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;YACrB,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import { ExpirableCache } from './expirable-cache.js';
|
2
|
+
import { State } from 'extra-memoize';
|
3
|
+
export class ExpirableCacheWithStaleWhileRevalidateAndStaleIfError {
|
4
|
+
constructor(timeToLive, staleWhileRevalidate, staleIfError) {
|
5
|
+
this.timeToLive = timeToLive;
|
6
|
+
this.staleWhileRevalidate = staleWhileRevalidate;
|
7
|
+
this.staleIfError = staleIfError;
|
8
|
+
this.cache = new ExpirableCache(timeToLive + staleWhileRevalidate + staleIfError);
|
9
|
+
}
|
10
|
+
set(key, value) {
|
11
|
+
this.cache.set(key, {
|
12
|
+
value,
|
13
|
+
updatedAt: Date.now()
|
14
|
+
});
|
15
|
+
}
|
16
|
+
get(key) {
|
17
|
+
const [state, record] = this.cache.get(key);
|
18
|
+
if (state === State.Miss) {
|
19
|
+
return [State.Miss];
|
20
|
+
}
|
21
|
+
else {
|
22
|
+
const timestamp = Date.now();
|
23
|
+
if (record.updatedAt + this.timeToLive > timestamp) {
|
24
|
+
return [State.Hit, record.value];
|
25
|
+
}
|
26
|
+
else if (record.updatedAt + this.timeToLive + this.staleWhileRevalidate > timestamp) {
|
27
|
+
return [State.StaleWhileRevalidate, record.value];
|
28
|
+
}
|
29
|
+
else if (record.updatedAt + this.timeToLive + this.staleWhileRevalidate + this.staleIfError > timestamp) {
|
30
|
+
return [State.StaleIfError, record.value];
|
31
|
+
}
|
32
|
+
else {
|
33
|
+
// just in case
|
34
|
+
return [State.Miss];
|
35
|
+
}
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
//# sourceMappingURL=expirable-cache-with-stale-while-revalidate-and-stale-if-error.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"expirable-cache-with-stale-while-revalidate-and-stale-if-error.js","sourceRoot":"","sources":["../../src/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAA6C,KAAK,EAAE,MAAM,eAAe,CAAA;AAOhF,MAAM,OAAO,qDAAqD;IAGhE,YACU,UAAkB,EAClB,oBAA4B,EAC5B,YAAoB;QAFpB,eAAU,GAAV,UAAU,CAAQ;QAClB,yBAAoB,GAApB,oBAAoB,CAAQ;QAC5B,iBAAY,GAAZ,YAAY,CAAQ;QAE5B,IAAI,CAAC,KAAK,GAAG,IAAI,cAAc,CAAC,UAAU,GAAG,oBAAoB,GAAG,YAAY,CAAC,CAAA;IACnF,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;YAClB,KAAK;YACL,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC,CAAA;IACJ,CAAC;IAED,GAAG,CAAC,GAAW;QAOb,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,KAAK,KAAK,KAAK,CAAC,IAAI,EAAE,CAAC;YACzB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;aAAM,CAAC;YACN,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;YAC5B,IAAI,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,SAAS,EAAE,CAAC;gBACnD,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YAClC,CAAC;iBAAM,IAAI,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,SAAS,EAAE,CAAC;gBACtF,OAAO,CAAC,KAAK,CAAC,oBAAoB,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YACnD,CAAC;iBAAM,IAAI,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,YAAY,GAAG,SAAS,EAAE,CAAC;gBAC1G,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YAC3C,CAAC;iBAAM,CAAC;gBACN,eAAe;gBACf,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;YACrB,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
@@ -1,13 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
const expirable_cache_1 = require("./expirable-cache");
|
5
|
-
const extra_memoize_1 = require("extra-memoize");
|
6
|
-
class ExpirableCacheWithStaleWhileRevalidate {
|
1
|
+
import { ExpirableCache } from './expirable-cache.js';
|
2
|
+
import { State } from 'extra-memoize';
|
3
|
+
export class ExpirableCacheWithStaleWhileRevalidate {
|
7
4
|
constructor(timeToLive, staleWhileRevalidate) {
|
8
5
|
this.timeToLive = timeToLive;
|
9
6
|
this.staleWhileRevalidate = staleWhileRevalidate;
|
10
|
-
this.cache = new
|
7
|
+
this.cache = new ExpirableCache(timeToLive + staleWhileRevalidate);
|
11
8
|
}
|
12
9
|
set(key, value) {
|
13
10
|
this.cache.set(key, {
|
@@ -17,15 +14,15 @@ class ExpirableCacheWithStaleWhileRevalidate {
|
|
17
14
|
}
|
18
15
|
get(key) {
|
19
16
|
const [state, record] = this.cache.get(key);
|
20
|
-
if (state ===
|
21
|
-
return [
|
17
|
+
if (state === State.Miss) {
|
18
|
+
return [State.Miss];
|
22
19
|
}
|
23
20
|
else {
|
24
21
|
if (this.isStaleWhileRevalidate(record)) {
|
25
|
-
return [
|
22
|
+
return [State.StaleWhileRevalidate, record.value];
|
26
23
|
}
|
27
24
|
else {
|
28
|
-
return [
|
25
|
+
return [State.Hit, record.value];
|
29
26
|
}
|
30
27
|
}
|
31
28
|
}
|
@@ -35,5 +32,4 @@ class ExpirableCacheWithStaleWhileRevalidate {
|
|
35
32
|
&& record.updatedAt + this.timeToLive + this.staleWhileRevalidate > timestamp;
|
36
33
|
}
|
37
34
|
}
|
38
|
-
exports.ExpirableCacheWithStaleWhileRevalidate = ExpirableCacheWithStaleWhileRevalidate;
|
39
35
|
//# sourceMappingURL=expirable-cache-with-stale-while-revalidate.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"expirable-cache-with-stale-while-revalidate.js","sourceRoot":"","sources":["../../src/caches/expirable-cache-with-stale-while-revalidate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAA8B,KAAK,EAAE,MAAM,eAAe,CAAA;AAOjE,MAAM,OAAO,sCAAsC;IAGjD,YAAoB,UAAkB,EAAU,oBAA4B;QAAxD,eAAU,GAAV,UAAU,CAAQ;QAAU,yBAAoB,GAApB,oBAAoB,CAAQ;QAC1E,IAAI,CAAC,KAAK,GAAG,IAAI,cAAc,CAAC,UAAU,GAAG,oBAAoB,CAAC,CAAA;IACpE,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;YAClB,KAAK;YACL,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC,CAAA;IACJ,CAAC;IAED,GAAG,CAAC,GAAW;QACb,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,KAAK,KAAK,KAAK,CAAC,IAAI,EAAE,CAAC;YACzB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;aAAM,CAAC;YACN,IAAI,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,EAAE,CAAC;gBACxC,OAAO,CAAC,KAAK,CAAC,oBAAoB,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YACnD,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YAClC,CAAC;QACH,CAAC;IACH,CAAC;IAEO,sBAAsB,CAAC,MAAkB;QAC/C,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAC5B,OAAO,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,IAAI,SAAS;eAC/C,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,SAAS,CAAA;IACnF,CAAC;CACF"}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { ExpirableMap } from '@blackglory/structures';
|
2
|
+
import { State } from 'extra-memoize';
|
3
|
+
export class ExpirableCache {
|
4
|
+
constructor(timeToLive) {
|
5
|
+
this.timeToLive = timeToLive;
|
6
|
+
this.map = new ExpirableMap();
|
7
|
+
}
|
8
|
+
get(key) {
|
9
|
+
if (this.map.has(key)) {
|
10
|
+
return [State.Hit, this.map.get(key)];
|
11
|
+
}
|
12
|
+
else {
|
13
|
+
return [State.Miss];
|
14
|
+
}
|
15
|
+
}
|
16
|
+
set(key, value) {
|
17
|
+
this.map.set(key, value, this.timeToLive);
|
18
|
+
}
|
19
|
+
clear() {
|
20
|
+
this.map.clear();
|
21
|
+
}
|
22
|
+
}
|
23
|
+
//# sourceMappingURL=expirable-cache.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"expirable-cache.js","sourceRoot":"","sources":["../../src/caches/expirable-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAA;AACrD,OAAO,EAAU,KAAK,EAAE,MAAM,eAAe,CAAA;AAE7C,MAAM,OAAO,cAAc;IAGzB,YAAoB,UAAkB;QAAlB,eAAU,GAAV,UAAU,CAAQ;QACpC,IAAI,CAAC,GAAG,GAAG,IAAI,YAAY,EAAE,CAAA;IAC/B,CAAC;IAED,GAAG,CAAC,GAAW;QACb,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACtB,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC,CAAA;QACxC,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;IACH,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,CAAA;IAC3C,CAAC;IAED,KAAK;QACH,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;IAClB,CAAC;CACF"}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
export * from './cache.js';
|
2
|
+
export * from './lru-cache.js';
|
3
|
+
export * from './expirable-cache.js';
|
4
|
+
export * from './expirable-cache-with-stale-while-revalidate.js';
|
5
|
+
export * from './expirable-cache-with-stale-if-error.js';
|
6
|
+
export * from './expirable-cache-with-stale-while-revalidate-and-stale-if-error.js';
|
7
|
+
export * from './tlru-cache.js';
|
8
|
+
export * from './tlru-cache-with-stale-while-revalidate.js';
|
9
|
+
export * from './tlru-cache-with-stale-if-error.js';
|
10
|
+
export * from './tlru-cache-with-stale-while-revalidate-and-stale-if-error.js';
|
@@ -0,0 +1,11 @@
|
|
1
|
+
export * from './cache.js';
|
2
|
+
export * from './lru-cache.js';
|
3
|
+
export * from './expirable-cache.js';
|
4
|
+
export * from './expirable-cache-with-stale-while-revalidate.js';
|
5
|
+
export * from './expirable-cache-with-stale-if-error.js';
|
6
|
+
export * from './expirable-cache-with-stale-while-revalidate-and-stale-if-error.js';
|
7
|
+
export * from './tlru-cache.js';
|
8
|
+
export * from './tlru-cache-with-stale-while-revalidate.js';
|
9
|
+
export * from './tlru-cache-with-stale-if-error.js';
|
10
|
+
export * from './tlru-cache-with-stale-while-revalidate-and-stale-if-error.js';
|
11
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/caches/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAE1B,cAAc,gBAAgB,CAAA;AAE9B,cAAc,sBAAsB,CAAA;AACpC,cAAc,kDAAkD,CAAA;AAChE,cAAc,0CAA0C,CAAA;AACxD,cAAc,qEAAqE,CAAA;AAEnF,cAAc,iBAAiB,CAAA;AAC/B,cAAc,6CAA6C,CAAA;AAC3D,cAAc,qCAAqC,CAAA;AACnD,cAAc,gEAAgE,CAAA"}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { LRUMap } from '@blackglory/structures';
|
2
|
+
import { State } from 'extra-memoize';
|
3
|
+
export class LRUCache {
|
4
|
+
constructor(limit) {
|
5
|
+
this.map = new LRUMap(limit);
|
6
|
+
}
|
7
|
+
set(key, value) {
|
8
|
+
this.map.set(key, value);
|
9
|
+
}
|
10
|
+
get(key) {
|
11
|
+
if (this.map.has(key)) {
|
12
|
+
return [State.Hit, this.map.get(key)];
|
13
|
+
}
|
14
|
+
else {
|
15
|
+
return [State.Miss];
|
16
|
+
}
|
17
|
+
}
|
18
|
+
clear() {
|
19
|
+
this.map.clear();
|
20
|
+
}
|
21
|
+
}
|
22
|
+
//# sourceMappingURL=lru-cache.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"lru-cache.js","sourceRoot":"","sources":["../../src/caches/lru-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAC/C,OAAO,EAAU,KAAK,EAAE,MAAM,eAAe,CAAA;AAE7C,MAAM,OAAO,QAAQ;IAGnB,YAAY,KAAa;QACvB,IAAI,CAAC,GAAG,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IAC1B,CAAC;IAED,GAAG,CAAC,GAAW;QACb,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACtB,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC,CAAA;QACxC,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;IACH,CAAC;IAED,KAAK;QACH,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;IAClB,CAAC;CACF"}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
import { TLRUCache } from './tlru-cache.js';
|
2
|
+
import { State } from 'extra-memoize';
|
3
|
+
export class TLRUCacheWithStaleIfError {
|
4
|
+
constructor(limit, timeToLive, staleIfError) {
|
5
|
+
this.timeToLive = timeToLive;
|
6
|
+
this.staleIfError = staleIfError;
|
7
|
+
this.cache = new TLRUCache(limit, timeToLive + staleIfError);
|
8
|
+
}
|
9
|
+
set(key, value) {
|
10
|
+
this.cache.set(key, {
|
11
|
+
value,
|
12
|
+
updatedAt: Date.now()
|
13
|
+
});
|
14
|
+
}
|
15
|
+
get(key) {
|
16
|
+
const [state, record] = this.cache.get(key);
|
17
|
+
if (state === State.Miss) {
|
18
|
+
return [State.Miss];
|
19
|
+
}
|
20
|
+
else {
|
21
|
+
const timestamp = Date.now();
|
22
|
+
if (record.updatedAt + this.timeToLive > timestamp) {
|
23
|
+
return [State.Hit, record.value];
|
24
|
+
}
|
25
|
+
else if (record.updatedAt + this.timeToLive + this.staleIfError > timestamp) {
|
26
|
+
return [State.StaleIfError, record.value];
|
27
|
+
}
|
28
|
+
else {
|
29
|
+
// just in case
|
30
|
+
return [State.Miss];
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
}
|
35
|
+
//# sourceMappingURL=tlru-cache-with-stale-if-error.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"tlru-cache-with-stale-if-error.js","sourceRoot":"","sources":["../../src/caches/tlru-cache-with-stale-if-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAsB,KAAK,EAAE,MAAM,eAAe,CAAA;AAOzD,MAAM,OAAO,yBAAyB;IAGpC,YACE,KAAa,EACL,UAAkB,EAClB,YAAoB;QADpB,eAAU,GAAV,UAAU,CAAQ;QAClB,iBAAY,GAAZ,YAAY,CAAQ;QAE5B,IAAI,CAAC,KAAK,GAAG,IAAI,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY,CAAC,CAAA;IAC9D,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;YAClB,KAAK;YACL,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC,CAAA;IACJ,CAAC;IAED,GAAG,CAAC,GAAW;QACb,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,KAAK,KAAK,KAAK,CAAC,IAAI,EAAE,CAAC;YACzB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;aAAM,CAAC;YACN,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;YAC5B,IAAI,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,SAAS,EAAE,CAAC;gBACnD,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YAClC,CAAC;iBAAM,IAAI,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,YAAY,GAAG,SAAS,EAAE,CAAC;gBAC9E,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YAC3C,CAAC;iBAAM,CAAC;gBACN,eAAe;gBACf,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;YACrB,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import { TLRUCache } from './tlru-cache.js';
|
2
|
+
import { State } from 'extra-memoize';
|
3
|
+
export class TLRUCacheWithStaleWhileRevalidateAndStaleIfError {
|
4
|
+
constructor(limit, timeToLive, staleWhileRevalidate, staleIfError) {
|
5
|
+
this.timeToLive = timeToLive;
|
6
|
+
this.staleWhileRevalidate = staleWhileRevalidate;
|
7
|
+
this.staleIfError = staleIfError;
|
8
|
+
this.cache = new TLRUCache(limit, timeToLive + staleWhileRevalidate + staleIfError);
|
9
|
+
}
|
10
|
+
set(key, value) {
|
11
|
+
this.cache.set(key, {
|
12
|
+
value,
|
13
|
+
updatedAt: Date.now()
|
14
|
+
});
|
15
|
+
}
|
16
|
+
get(key) {
|
17
|
+
const [state, record] = this.cache.get(key);
|
18
|
+
if (state === State.Miss) {
|
19
|
+
return [State.Miss];
|
20
|
+
}
|
21
|
+
else {
|
22
|
+
const timestamp = Date.now();
|
23
|
+
if (record.updatedAt + this.timeToLive > timestamp) {
|
24
|
+
return [State.Hit, record.value];
|
25
|
+
}
|
26
|
+
else if (record.updatedAt + this.timeToLive + this.staleWhileRevalidate > timestamp) {
|
27
|
+
return [State.StaleWhileRevalidate, record.value];
|
28
|
+
}
|
29
|
+
else if (record.updatedAt + this.timeToLive + this.staleWhileRevalidate + this.staleIfError > timestamp) {
|
30
|
+
return [State.StaleIfError, record.value];
|
31
|
+
}
|
32
|
+
else {
|
33
|
+
// just in case
|
34
|
+
return [State.Miss];
|
35
|
+
}
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
//# sourceMappingURL=tlru-cache-with-stale-while-revalidate-and-stale-if-error.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"tlru-cache-with-stale-while-revalidate-and-stale-if-error.js","sourceRoot":"","sources":["../../src/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAA6C,KAAK,EAAE,MAAM,eAAe,CAAA;AAOhF,MAAM,OAAO,gDAAgD;IAG3D,YACE,KAAa,EACL,UAAkB,EAClB,oBAA4B,EAC5B,YAAoB;QAFpB,eAAU,GAAV,UAAU,CAAQ;QAClB,yBAAoB,GAApB,oBAAoB,CAAQ;QAC5B,iBAAY,GAAZ,YAAY,CAAQ;QAE5B,IAAI,CAAC,KAAK,GAAG,IAAI,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,oBAAoB,GAAG,YAAY,CAAC,CAAA;IACrF,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;YAClB,KAAK;YACL,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC,CAAA;IACJ,CAAC;IAED,GAAG,CAAC,GAAW;QAOb,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,KAAK,KAAK,KAAK,CAAC,IAAI,EAAE,CAAC;YACzB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;aAAM,CAAC;YACN,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;YAC5B,IAAI,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,SAAS,EAAE,CAAC;gBACnD,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YAClC,CAAC;iBAAM,IAAI,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,SAAS,EAAE,CAAC;gBACtF,OAAO,CAAC,KAAK,CAAC,oBAAoB,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YACnD,CAAC;iBAAM,IAAI,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,YAAY,GAAG,SAAS,EAAE,CAAC;gBAC1G,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YAC3C,CAAC;iBAAM,CAAC;gBACN,eAAe;gBACf,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;YACrB,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
@@ -1,13 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
const tlru_cache_1 = require("./tlru-cache");
|
5
|
-
const extra_memoize_1 = require("extra-memoize");
|
6
|
-
class TLRUCacheWithStaleWhileRevalidate {
|
1
|
+
import { TLRUCache } from './tlru-cache.js';
|
2
|
+
import { State } from 'extra-memoize';
|
3
|
+
export class TLRUCacheWithStaleWhileRevalidate {
|
7
4
|
constructor(limit, timeToLive, staleWhileRevalidate) {
|
8
5
|
this.timeToLive = timeToLive;
|
9
6
|
this.staleWhileRevalidate = staleWhileRevalidate;
|
10
|
-
this.cache = new
|
7
|
+
this.cache = new TLRUCache(limit, timeToLive + staleWhileRevalidate);
|
11
8
|
}
|
12
9
|
set(key, value) {
|
13
10
|
this.cache.set(key, {
|
@@ -17,15 +14,15 @@ class TLRUCacheWithStaleWhileRevalidate {
|
|
17
14
|
}
|
18
15
|
get(key) {
|
19
16
|
const [state, record] = this.cache.get(key);
|
20
|
-
if (state ===
|
21
|
-
return [
|
17
|
+
if (state === State.Miss) {
|
18
|
+
return [State.Miss];
|
22
19
|
}
|
23
20
|
else {
|
24
21
|
if (this.isStaleWhileRevalidate(record)) {
|
25
|
-
return [
|
22
|
+
return [State.StaleWhileRevalidate, record.value];
|
26
23
|
}
|
27
24
|
else {
|
28
|
-
return [
|
25
|
+
return [State.Hit, record.value];
|
29
26
|
}
|
30
27
|
}
|
31
28
|
}
|
@@ -35,5 +32,4 @@ class TLRUCacheWithStaleWhileRevalidate {
|
|
35
32
|
&& record.updatedAt + this.timeToLive + this.staleWhileRevalidate > timestamp;
|
36
33
|
}
|
37
34
|
}
|
38
|
-
exports.TLRUCacheWithStaleWhileRevalidate = TLRUCacheWithStaleWhileRevalidate;
|
39
35
|
//# sourceMappingURL=tlru-cache-with-stale-while-revalidate.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"tlru-cache-with-stale-while-revalidate.js","sourceRoot":"","sources":["../../src/caches/tlru-cache-with-stale-while-revalidate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAA8B,KAAK,EAAE,MAAM,eAAe,CAAA;AAOjE,MAAM,OAAO,iCAAiC;IAG5C,YACE,KAAa,EACL,UAAkB,EAClB,oBAA4B;QAD5B,eAAU,GAAV,UAAU,CAAQ;QAClB,yBAAoB,GAApB,oBAAoB,CAAQ;QAEpC,IAAI,CAAC,KAAK,GAAG,IAAI,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,oBAAoB,CAAC,CAAA;IACtE,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;YAClB,KAAK;YACL,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC,CAAA;IACJ,CAAC;IAED,GAAG,CAAC,GAAW;QACb,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,KAAK,KAAK,KAAK,CAAC,IAAI,EAAE,CAAC;YACzB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;aAAM,CAAC;YACN,IAAI,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,EAAE,CAAC;gBACxC,OAAO,CAAC,KAAK,CAAC,oBAAoB,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YACnD,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;YAClC,CAAC;QACH,CAAC;IACH,CAAC;IAED,sBAAsB,CAAC,MAAkB;QACvC,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAC5B,OAAO,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,IAAI,SAAS;eAC/C,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,SAAS,CAAA;IACnF,CAAC;CACF"}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { TLRUMap } from '@blackglory/structures';
|
2
|
+
import { State } from 'extra-memoize';
|
3
|
+
export class TLRUCache {
|
4
|
+
constructor(limit, timeToLive) {
|
5
|
+
this.timeToLive = timeToLive;
|
6
|
+
this.map = new TLRUMap(limit);
|
7
|
+
}
|
8
|
+
set(key, value) {
|
9
|
+
this.map.set(key, value, this.timeToLive);
|
10
|
+
}
|
11
|
+
get(key) {
|
12
|
+
if (this.map.has(key)) {
|
13
|
+
return [State.Hit, this.map.get(key)];
|
14
|
+
}
|
15
|
+
else {
|
16
|
+
return [State.Miss];
|
17
|
+
}
|
18
|
+
}
|
19
|
+
clear() {
|
20
|
+
this.map.clear();
|
21
|
+
}
|
22
|
+
}
|
23
|
+
//# sourceMappingURL=tlru-cache.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"tlru-cache.js","sourceRoot":"","sources":["../../src/caches/tlru-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAChD,OAAO,EAAU,KAAK,EAAE,MAAM,eAAe,CAAA;AAE7C,MAAM,OAAO,SAAS;IAGpB,YAAY,KAAa,EAAU,UAAkB;QAAlB,eAAU,GAAV,UAAU,CAAQ;QACnD,IAAI,CAAC,GAAG,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,CAAA;IAC/B,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,CAAA;IAC3C,CAAC;IAED,GAAG,CAAC,GAAW;QACb,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACtB,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC,CAAA;QACxC,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;IACH,CAAC;IAED,KAAK;QACH,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;IAClB,CAAC;CACF"}
|
package/lib/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./caches/index.js";
|
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kCAAgC"}
|
package/package.json
CHANGED
@@ -1,65 +1,63 @@
|
|
1
1
|
{
|
2
2
|
"name": "@extra-memoize/memory-cache",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.3.0",
|
4
4
|
"description": "",
|
5
5
|
"keywords": [],
|
6
6
|
"files": [
|
7
7
|
"lib",
|
8
8
|
"src"
|
9
9
|
],
|
10
|
-
"main": "lib/
|
11
|
-
"types": "lib/
|
12
|
-
"sideEffects": false,
|
10
|
+
"main": "lib/index.js",
|
11
|
+
"types": "lib/index.d.ts",
|
13
12
|
"repository": "git@github.com:extra-memoize/memory-cache.git",
|
14
13
|
"author": "BlackGlory <woshenmedoubuzhidao@blackglory.me>",
|
15
14
|
"license": "MIT",
|
15
|
+
"type": "module",
|
16
|
+
"sideEffects": false,
|
17
|
+
"engines": {
|
18
|
+
"node": ">=22"
|
19
|
+
},
|
16
20
|
"scripts": {
|
17
|
-
"
|
18
|
-
"
|
19
|
-
"test
|
20
|
-
"
|
21
|
-
"prepublishOnly": "run-s clean build",
|
21
|
+
"prepare": "ts-patch install -s",
|
22
|
+
"lint": "eslint --quiet src __tests__",
|
23
|
+
"test": "vitest run",
|
24
|
+
"prepublishOnly": "run-s prepare clean build",
|
22
25
|
"clean": "rimraf lib",
|
23
|
-
"build": "
|
24
|
-
"build:es2015": "run-s build:es2015:*",
|
25
|
-
"build:es2015:compile": "tsc --project tsconfig.build.json --module commonjs --target es2015 --outDir lib/es2015",
|
26
|
-
"build:es2015:patch": "tscpaths -p tsconfig.build.json -s ./src -o ./lib/es2015",
|
27
|
-
"build:es2018": "run-s build:es2018:*",
|
28
|
-
"build:es2018:compile": "tsc --project tsconfig.build.json --module commonjs --target es2018 --outDir lib/es2018",
|
29
|
-
"build:es2018:patch": "tscpaths -p tsconfig.build.json -s ./src -o ./lib/es2018",
|
26
|
+
"build": "tsc --project tsconfig.build.json --outDir lib",
|
30
27
|
"release": "standard-version"
|
31
28
|
},
|
32
29
|
"husky": {
|
33
30
|
"hooks": {
|
34
|
-
"pre-commit": "run-s lint build test",
|
31
|
+
"pre-commit": "run-s prepare clean lint build test",
|
35
32
|
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
|
36
33
|
}
|
37
34
|
},
|
38
35
|
"devDependencies": {
|
39
|
-
"@commitlint/cli": "^
|
40
|
-
"@commitlint/config-conventional": "^
|
41
|
-
"@
|
42
|
-
"
|
43
|
-
"
|
44
|
-
"
|
45
|
-
"extra-memoize": "^0.9.2",
|
46
|
-
"extra-promise": "^4.0.0",
|
36
|
+
"@commitlint/cli": "^19.8.1",
|
37
|
+
"@commitlint/config-conventional": "^19.8.1",
|
38
|
+
"@eslint/js": "^9.30.1",
|
39
|
+
"eslint": "^9.30.1",
|
40
|
+
"extra-memoize": "^0.10.0",
|
41
|
+
"extra-promise": "^7.0.0",
|
47
42
|
"husky": "^4.3.8",
|
48
|
-
"jest": "^29.3.1",
|
49
43
|
"npm-run-all": "^4.1.5",
|
50
|
-
"return-style": "^
|
51
|
-
"rimraf": "^
|
44
|
+
"return-style": "^3.0.1",
|
45
|
+
"rimraf": "^6.0.1",
|
52
46
|
"standard-version": "^9.5.0",
|
53
|
-
"ts-
|
54
|
-
"
|
55
|
-
"
|
56
|
-
"typescript": "^
|
47
|
+
"ts-patch": "^3.3.0",
|
48
|
+
"tslib": "^2.8.1",
|
49
|
+
"typescript": "^5.8.3",
|
50
|
+
"typescript-eslint": "^8.35.1",
|
51
|
+
"typescript-transform-paths": "^3.5.5",
|
52
|
+
"vite": "^7.0.2",
|
53
|
+
"vite-tsconfig-paths": "^5.1.4",
|
54
|
+
"vitest": "^3.2.4"
|
57
55
|
},
|
58
56
|
"dependencies": {
|
59
|
-
"@blackglory/prelude": "^0.
|
60
|
-
"@blackglory/structures": "^0.
|
57
|
+
"@blackglory/prelude": "^0.4.0",
|
58
|
+
"@blackglory/structures": "^0.14.10"
|
61
59
|
},
|
62
60
|
"peerDependencies": {
|
63
|
-
"extra-memoize": "^0.6.0 || ^0.7.0 || ^0.8.0 || ^0.9.0"
|
61
|
+
"extra-memoize": "^0.6.0 || ^0.7.0 || ^0.8.0 || ^0.9.0 || ^0.10.0"
|
64
62
|
}
|
65
63
|
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ExpirableCache } from './expirable-cache'
|
1
|
+
import { ExpirableCache } from './expirable-cache.js'
|
2
2
|
import { IStaleIfErrorCache, State } from 'extra-memoize'
|
3
3
|
|
4
4
|
interface IRecord<T> {
|
@@ -25,10 +25,10 @@ export class ExpirableCacheWithStaleIfError<T> implements IStaleIfErrorCache<T>
|
|
25
25
|
if (state === State.Miss) {
|
26
26
|
return [State.Miss]
|
27
27
|
} else {
|
28
|
-
const
|
29
|
-
if (
|
28
|
+
const timestamp = Date.now()
|
29
|
+
if (record.updatedAt + this.timeToLive > timestamp) {
|
30
30
|
return [State.Hit, record.value]
|
31
|
-
} else if (
|
31
|
+
} else if (record.updatedAt + this.timeToLive + this.staleIfError > timestamp) {
|
32
32
|
return [State.StaleIfError, record.value]
|
33
33
|
} else {
|
34
34
|
// just in case
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ExpirableCache } from './expirable-cache'
|
1
|
+
import { ExpirableCache } from './expirable-cache.js'
|
2
2
|
import { IStaleWhileRevalidateAndStaleIfErrorCache, State } from 'extra-memoize'
|
3
3
|
|
4
4
|
interface IRecord<T> {
|
@@ -35,12 +35,12 @@ export class ExpirableCacheWithStaleWhileRevalidateAndStaleIfError<T> implements
|
|
35
35
|
if (state === State.Miss) {
|
36
36
|
return [State.Miss]
|
37
37
|
} else {
|
38
|
-
const
|
39
|
-
if (
|
38
|
+
const timestamp = Date.now()
|
39
|
+
if (record.updatedAt + this.timeToLive > timestamp) {
|
40
40
|
return [State.Hit, record.value]
|
41
|
-
} else if (
|
41
|
+
} else if (record.updatedAt + this.timeToLive + this.staleWhileRevalidate > timestamp) {
|
42
42
|
return [State.StaleWhileRevalidate, record.value]
|
43
|
-
} else if (
|
43
|
+
} else if (record.updatedAt + this.timeToLive + this.staleWhileRevalidate + this.staleIfError > timestamp) {
|
44
44
|
return [State.StaleIfError, record.value]
|
45
45
|
} else {
|
46
46
|
// just in case
|
package/src/caches/index.ts
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
export * from './cache'
|
1
|
+
export * from './cache.js'
|
2
2
|
|
3
|
-
export * from './lru-cache'
|
3
|
+
export * from './lru-cache.js'
|
4
4
|
|
5
|
-
export * from './expirable-cache'
|
6
|
-
export * from './expirable-cache-with-stale-while-revalidate'
|
7
|
-
export * from './expirable-cache-with-stale-if-error'
|
8
|
-
export * from './expirable-cache-with-stale-while-revalidate-and-stale-if-error'
|
5
|
+
export * from './expirable-cache.js'
|
6
|
+
export * from './expirable-cache-with-stale-while-revalidate.js'
|
7
|
+
export * from './expirable-cache-with-stale-if-error.js'
|
8
|
+
export * from './expirable-cache-with-stale-while-revalidate-and-stale-if-error.js'
|
9
9
|
|
10
|
-
export * from './tlru-cache'
|
11
|
-
export * from './tlru-cache-with-stale-while-revalidate'
|
12
|
-
export * from './tlru-cache-with-stale-if-error'
|
13
|
-
export * from './tlru-cache-with-stale-while-revalidate-and-stale-if-error'
|
10
|
+
export * from './tlru-cache.js'
|
11
|
+
export * from './tlru-cache-with-stale-while-revalidate.js'
|
12
|
+
export * from './tlru-cache-with-stale-if-error.js'
|
13
|
+
export * from './tlru-cache-with-stale-while-revalidate-and-stale-if-error.js'
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { TLRUCache } from './tlru-cache'
|
1
|
+
import { TLRUCache } from './tlru-cache.js'
|
2
2
|
import { IStaleIfErrorCache, State } from 'extra-memoize'
|
3
3
|
|
4
4
|
interface IRecord<T> {
|
@@ -29,10 +29,10 @@ export class TLRUCacheWithStaleIfError<T> implements IStaleIfErrorCache<T> {
|
|
29
29
|
if (state === State.Miss) {
|
30
30
|
return [State.Miss]
|
31
31
|
} else {
|
32
|
-
const
|
33
|
-
if (
|
32
|
+
const timestamp = Date.now()
|
33
|
+
if (record.updatedAt + this.timeToLive > timestamp) {
|
34
34
|
return [State.Hit, record.value]
|
35
|
-
} else if (
|
35
|
+
} else if (record.updatedAt + this.timeToLive + this.staleIfError > timestamp) {
|
36
36
|
return [State.StaleIfError, record.value]
|
37
37
|
} else {
|
38
38
|
// just in case
|