@extra-memoize/memory-cache 0.1.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/LICENSE +21 -0
- package/README.md +92 -0
- package/dist/es2015/index.min.mjs +2 -0
- package/dist/es2015/index.min.mjs.map +1 -0
- package/dist/es2015/index.mjs +12533 -0
- package/dist/es2015/index.mjs.map +1 -0
- package/dist/es2015/index.umd.js +12551 -0
- package/dist/es2015/index.umd.js.map +1 -0
- package/dist/es2015/index.umd.min.js +2 -0
- package/dist/es2015/index.umd.min.js.map +1 -0
- package/dist/es2018/index.min.mjs +2 -0
- package/dist/es2018/index.min.mjs.map +1 -0
- package/dist/es2018/index.mjs +12533 -0
- package/dist/es2018/index.mjs.map +1 -0
- package/dist/es2018/index.umd.js +12551 -0
- package/dist/es2018/index.umd.js.map +1 -0
- package/dist/es2018/index.umd.min.js +2 -0
- package/dist/es2018/index.umd.min.js.map +1 -0
- package/lib/es2015/caches/expirable-cache-with-stale-if-error.d.ts +9 -0
- package/lib/es2015/caches/expirable-cache-with-stale-if-error.js +36 -0
- package/lib/es2015/caches/expirable-cache-with-stale-if-error.js.map +1 -0
- package/lib/es2015/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.d.ts +10 -0
- package/lib/es2015/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.js +40 -0
- package/lib/es2015/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.js.map +1 -0
- package/lib/es2015/caches/expirable-cache-with-stale-while-revalidate.d.ts +10 -0
- package/lib/es2015/caches/expirable-cache-with-stale-while-revalidate.js +31 -0
- package/lib/es2015/caches/expirable-cache-with-stale-while-revalidate.js.map +1 -0
- package/lib/es2015/caches/expirable-cache.d.ts +9 -0
- package/lib/es2015/caches/expirable-cache.js +21 -0
- package/lib/es2015/caches/expirable-cache.js.map +1 -0
- package/lib/es2015/caches/index.d.ts +9 -0
- package/lib/es2015/caches/index.js +26 -0
- package/lib/es2015/caches/index.js.map +1 -0
- package/lib/es2015/caches/lru-cache.d.ts +8 -0
- package/lib/es2015/caches/lru-cache.js +20 -0
- package/lib/es2015/caches/lru-cache.js.map +1 -0
- package/lib/es2015/caches/tlru-cache-with-stale-if-error.d.ts +9 -0
- package/lib/es2015/caches/tlru-cache-with-stale-if-error.js +36 -0
- package/lib/es2015/caches/tlru-cache-with-stale-if-error.js.map +1 -0
- package/lib/es2015/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.d.ts +10 -0
- package/lib/es2015/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.js +40 -0
- package/lib/es2015/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.js.map +1 -0
- package/lib/es2015/caches/tlru-cache-with-stale-while-revalidate.d.ts +10 -0
- package/lib/es2015/caches/tlru-cache-with-stale-while-revalidate.js +31 -0
- package/lib/es2015/caches/tlru-cache-with-stale-while-revalidate.js.map +1 -0
- package/lib/es2015/caches/tlru-cache.d.ts +9 -0
- package/lib/es2015/caches/tlru-cache.js +21 -0
- package/lib/es2015/caches/tlru-cache.js.map +1 -0
- package/lib/es2015/index.d.ts +1 -0
- package/lib/es2015/index.js +18 -0
- package/lib/es2015/index.js.map +1 -0
- package/lib/es2018/caches/expirable-cache-with-stale-if-error.d.ts +9 -0
- package/lib/es2018/caches/expirable-cache-with-stale-if-error.js +36 -0
- package/lib/es2018/caches/expirable-cache-with-stale-if-error.js.map +1 -0
- package/lib/es2018/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.d.ts +10 -0
- package/lib/es2018/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.js +40 -0
- package/lib/es2018/caches/expirable-cache-with-stale-while-revalidate-and-stale-if-error.js.map +1 -0
- package/lib/es2018/caches/expirable-cache-with-stale-while-revalidate.d.ts +10 -0
- package/lib/es2018/caches/expirable-cache-with-stale-while-revalidate.js +31 -0
- package/lib/es2018/caches/expirable-cache-with-stale-while-revalidate.js.map +1 -0
- package/lib/es2018/caches/expirable-cache.d.ts +9 -0
- package/lib/es2018/caches/expirable-cache.js +21 -0
- package/lib/es2018/caches/expirable-cache.js.map +1 -0
- package/lib/es2018/caches/index.d.ts +9 -0
- package/lib/es2018/caches/index.js +26 -0
- package/lib/es2018/caches/index.js.map +1 -0
- package/lib/es2018/caches/lru-cache.d.ts +8 -0
- package/lib/es2018/caches/lru-cache.js +20 -0
- package/lib/es2018/caches/lru-cache.js.map +1 -0
- package/lib/es2018/caches/tlru-cache-with-stale-if-error.d.ts +9 -0
- package/lib/es2018/caches/tlru-cache-with-stale-if-error.js +36 -0
- package/lib/es2018/caches/tlru-cache-with-stale-if-error.js.map +1 -0
- package/lib/es2018/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.d.ts +10 -0
- package/lib/es2018/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.js +40 -0
- package/lib/es2018/caches/tlru-cache-with-stale-while-revalidate-and-stale-if-error.js.map +1 -0
- package/lib/es2018/caches/tlru-cache-with-stale-while-revalidate.d.ts +10 -0
- package/lib/es2018/caches/tlru-cache-with-stale-while-revalidate.js +31 -0
- package/lib/es2018/caches/tlru-cache-with-stale-while-revalidate.js.map +1 -0
- package/lib/es2018/caches/tlru-cache.d.ts +9 -0
- package/lib/es2018/caches/tlru-cache.js +21 -0
- package/lib/es2018/caches/tlru-cache.js.map +1 -0
- package/lib/es2018/index.d.ts +1 -0
- package/lib/es2018/index.js +18 -0
- package/lib/es2018/index.js.map +1 -0
- package/package.json +77 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ExpirableCacheWithStaleWhileRevalidate = void 0;
|
4
|
+
const expirable_cache_1 = require("./expirable-cache");
|
5
|
+
const prelude_1 = require("@blackglory/prelude");
|
6
|
+
class ExpirableCacheWithStaleWhileRevalidate {
|
7
|
+
constructor(timeToLive, staleWhileRevalidate) {
|
8
|
+
this.timeToLive = timeToLive;
|
9
|
+
this.staleWhileRevalidate = staleWhileRevalidate;
|
10
|
+
this.cache = new expirable_cache_1.ExpirableCache(timeToLive + staleWhileRevalidate);
|
11
|
+
}
|
12
|
+
set(key, value) {
|
13
|
+
this.cache.set(key, {
|
14
|
+
value,
|
15
|
+
updatedAt: Date.now()
|
16
|
+
});
|
17
|
+
}
|
18
|
+
get(key) {
|
19
|
+
var _a;
|
20
|
+
return (_a = this.cache.get(key)) === null || _a === void 0 ? void 0 : _a.value;
|
21
|
+
}
|
22
|
+
isStaleWhileRevalidate(key) {
|
23
|
+
const record = this.cache.get(key);
|
24
|
+
if ((0, prelude_1.isUndefined)(record))
|
25
|
+
return false;
|
26
|
+
return Date.now() - record.updatedAt > this.timeToLive
|
27
|
+
&& Date.now() - record.updatedAt <= this.timeToLive + this.staleWhileRevalidate;
|
28
|
+
}
|
29
|
+
}
|
30
|
+
exports.ExpirableCacheWithStaleWhileRevalidate = ExpirableCacheWithStaleWhileRevalidate;
|
31
|
+
//# 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,uDAAkD;AAElD,iDAAiD;AAOjD,MAAa,sCAAsC;IAGjD,YAAoB,UAAkB,EAAU,oBAA4B;QAAxD,eAAU,GAAV,UAAU,CAAQ;QAAU,yBAAoB,GAApB,oBAAoB,CAAQ;QAC1E,IAAI,CAAC,KAAK,GAAG,IAAI,gCAAc,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,OAAO,MAAA,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,0CAAE,KAAK,CAAA;IACnC,CAAC;IAED,sBAAsB,CAAC,GAAW;QAChC,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAClC,IAAI,IAAA,qBAAW,EAAC,MAAM,CAAC;YAAE,OAAO,KAAK,CAAA;QAErC,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU;eAC/C,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,SAAS,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,CAAA;IACrF,CAAC;CACF;AAzBD,wFAyBC"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { ICache } from 'extra-memoize';
|
2
|
+
export declare class ExpirableCache<T = any> implements ICache<T> {
|
3
|
+
private timeToLive;
|
4
|
+
private map;
|
5
|
+
constructor(timeToLive: number);
|
6
|
+
get(key: string): T | undefined;
|
7
|
+
set(key: string, value: T): void;
|
8
|
+
clear(): void;
|
9
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ExpirableCache = void 0;
|
4
|
+
const structures_1 = require("@blackglory/structures");
|
5
|
+
class ExpirableCache {
|
6
|
+
constructor(timeToLive) {
|
7
|
+
this.timeToLive = timeToLive;
|
8
|
+
this.map = new structures_1.ExpirableMap();
|
9
|
+
}
|
10
|
+
get(key) {
|
11
|
+
return this.map.get(key);
|
12
|
+
}
|
13
|
+
set(key, value) {
|
14
|
+
this.map.set(key, value, this.timeToLive);
|
15
|
+
}
|
16
|
+
clear() {
|
17
|
+
this.map.clear();
|
18
|
+
}
|
19
|
+
}
|
20
|
+
exports.ExpirableCache = ExpirableCache;
|
21
|
+
//# 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,uDAAqD;AAGrD,MAAa,cAAc;IAGzB,YAAoB,UAAkB;QAAlB,eAAU,GAAV,UAAU,CAAQ;QACpC,IAAI,CAAC,GAAG,GAAG,IAAI,yBAAY,EAAE,CAAA;IAC/B,CAAC;IAED,GAAG,CAAC,GAAW;QACb,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAC1B,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;AAlBD,wCAkBC"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
export * from './lru-cache';
|
2
|
+
export * from './expirable-cache';
|
3
|
+
export * from './expirable-cache-with-stale-while-revalidate';
|
4
|
+
export * from './expirable-cache-with-stale-if-error';
|
5
|
+
export * from './expirable-cache-with-stale-while-revalidate-and-stale-if-error';
|
6
|
+
export * from './tlru-cache';
|
7
|
+
export * from './tlru-cache-with-stale-while-revalidate';
|
8
|
+
export * from './tlru-cache-with-stale-if-error';
|
9
|
+
export * from './tlru-cache-with-stale-while-revalidate-and-stale-if-error';
|
@@ -0,0 +1,26 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./lru-cache"), exports);
|
18
|
+
__exportStar(require("./expirable-cache"), exports);
|
19
|
+
__exportStar(require("./expirable-cache-with-stale-while-revalidate"), exports);
|
20
|
+
__exportStar(require("./expirable-cache-with-stale-if-error"), exports);
|
21
|
+
__exportStar(require("./expirable-cache-with-stale-while-revalidate-and-stale-if-error"), exports);
|
22
|
+
__exportStar(require("./tlru-cache"), exports);
|
23
|
+
__exportStar(require("./tlru-cache-with-stale-while-revalidate"), exports);
|
24
|
+
__exportStar(require("./tlru-cache-with-stale-if-error"), exports);
|
25
|
+
__exportStar(require("./tlru-cache-with-stale-while-revalidate-and-stale-if-error"), exports);
|
26
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/caches/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA2B;AAE3B,oDAAiC;AACjC,gFAA6D;AAC7D,wEAAqD;AACrD,mGAAgF;AAEhF,+CAA4B;AAC5B,2EAAwD;AACxD,mEAAgD;AAChD,8FAA2E"}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.LRUCache = void 0;
|
4
|
+
const structures_1 = require("@blackglory/structures");
|
5
|
+
class LRUCache {
|
6
|
+
constructor(limit) {
|
7
|
+
this.map = new structures_1.LRUMap(limit);
|
8
|
+
}
|
9
|
+
set(key, value) {
|
10
|
+
this.map.set(key, value);
|
11
|
+
}
|
12
|
+
get(key) {
|
13
|
+
return this.map.get(key);
|
14
|
+
}
|
15
|
+
clear() {
|
16
|
+
this.map.clear();
|
17
|
+
}
|
18
|
+
}
|
19
|
+
exports.LRUCache = LRUCache;
|
20
|
+
//# 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,uDAA+C;AAG/C,MAAa,QAAQ;IAGnB,YAAY,KAAa;QACvB,IAAI,CAAC,GAAG,GAAG,IAAI,mBAAM,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,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAC1B,CAAC;IAED,KAAK;QACH,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;IAClB,CAAC;CACF;AAlBD,4BAkBC"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { IStaleIfErrorCache, State } from 'extra-memoize';
|
2
|
+
export declare class TLRUCacheWithStaleIfError<T = any> implements IStaleIfErrorCache<T> {
|
3
|
+
private timeToLive;
|
4
|
+
private staleIfError;
|
5
|
+
private cache;
|
6
|
+
constructor(limit: number, timeToLive: number, staleIfError: number);
|
7
|
+
set(key: string, value: T): void;
|
8
|
+
get(key: string): [State.Miss, undefined] | [State.Hit | State.StaleIfError, T];
|
9
|
+
}
|
@@ -0,0 +1,36 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.TLRUCacheWithStaleIfError = void 0;
|
4
|
+
const tlru_cache_1 = require("./tlru-cache");
|
5
|
+
const extra_memoize_1 = require("extra-memoize");
|
6
|
+
const prelude_1 = require("@blackglory/prelude");
|
7
|
+
class TLRUCacheWithStaleIfError {
|
8
|
+
constructor(limit, timeToLive, staleIfError) {
|
9
|
+
this.timeToLive = timeToLive;
|
10
|
+
this.staleIfError = staleIfError;
|
11
|
+
this.cache = new tlru_cache_1.TLRUCache(limit, timeToLive + staleIfError);
|
12
|
+
}
|
13
|
+
set(key, value) {
|
14
|
+
this.cache.set(key, {
|
15
|
+
value,
|
16
|
+
updatedAt: Date.now()
|
17
|
+
});
|
18
|
+
}
|
19
|
+
get(key) {
|
20
|
+
const record = this.cache.get(key);
|
21
|
+
if ((0, prelude_1.isUndefined)(record))
|
22
|
+
return [extra_memoize_1.State.Miss, undefined];
|
23
|
+
const elapsed = Date.now() - record.updatedAt;
|
24
|
+
if (elapsed <= this.timeToLive) {
|
25
|
+
return [extra_memoize_1.State.Hit, record.value];
|
26
|
+
}
|
27
|
+
else if (elapsed <= this.timeToLive + this.staleIfError) {
|
28
|
+
return [extra_memoize_1.State.StaleIfError, record.value];
|
29
|
+
}
|
30
|
+
else {
|
31
|
+
return [extra_memoize_1.State.Miss, undefined];
|
32
|
+
}
|
33
|
+
}
|
34
|
+
}
|
35
|
+
exports.TLRUCacheWithStaleIfError = TLRUCacheWithStaleIfError;
|
36
|
+
//# 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,6CAAwC;AACxC,iDAAyD;AACzD,iDAAiD;AAOjD,MAAa,yBAAyB;IAGpC,YACE,KAAa,EACL,UAAkB,EAClB,YAAoB;QADpB,eAAU,GAAV,UAAU,CAAQ;QAClB,iBAAY,GAAZ,YAAY,CAAQ;QAE5B,IAAI,CAAC,KAAK,GAAG,IAAI,sBAAS,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,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAClC,IAAI,IAAA,qBAAW,EAAC,MAAM,CAAC;YAAE,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;QAEvD,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,SAAS,CAAA;QAC7C,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,EAAE;YAC9B,OAAO,CAAC,qBAAK,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;SACjC;aAAM,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,YAAY,EAAE;YACzD,OAAO,CAAC,qBAAK,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;SAC1C;aAAM;YAEL,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;SAC/B;IACH,CAAC;CACF;AAhCD,8DAgCC"}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { IStaleWhileRevalidateAndStaleIfErrorCache, State } from 'extra-memoize';
|
2
|
+
export declare class TLRUCacheWithStaleWhileRevalidateAndStaleIfError<T = any> implements IStaleWhileRevalidateAndStaleIfErrorCache<T> {
|
3
|
+
private timeToLive;
|
4
|
+
private staleWhileRevalidate;
|
5
|
+
private staleIfError;
|
6
|
+
private cache;
|
7
|
+
constructor(limit: number, timeToLive: number, staleWhileRevalidate: number, staleIfError: number);
|
8
|
+
set(key: string, value: T): void;
|
9
|
+
get(key: string): [State.Miss, undefined] | [State.Hit | State.StaleWhileRevalidate | State.StaleIfError, T];
|
10
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.TLRUCacheWithStaleWhileRevalidateAndStaleIfError = void 0;
|
4
|
+
const tlru_cache_1 = require("./tlru-cache");
|
5
|
+
const extra_memoize_1 = require("extra-memoize");
|
6
|
+
const prelude_1 = require("@blackglory/prelude");
|
7
|
+
class TLRUCacheWithStaleWhileRevalidateAndStaleIfError {
|
8
|
+
constructor(limit, timeToLive, staleWhileRevalidate, staleIfError) {
|
9
|
+
this.timeToLive = timeToLive;
|
10
|
+
this.staleWhileRevalidate = staleWhileRevalidate;
|
11
|
+
this.staleIfError = staleIfError;
|
12
|
+
this.cache = new tlru_cache_1.TLRUCache(limit, timeToLive + staleWhileRevalidate + staleIfError);
|
13
|
+
}
|
14
|
+
set(key, value) {
|
15
|
+
this.cache.set(key, {
|
16
|
+
value,
|
17
|
+
updatedAt: Date.now()
|
18
|
+
});
|
19
|
+
}
|
20
|
+
get(key) {
|
21
|
+
const record = this.cache.get(key);
|
22
|
+
if ((0, prelude_1.isUndefined)(record))
|
23
|
+
return [extra_memoize_1.State.Miss, undefined];
|
24
|
+
const elapsed = Date.now() - record.updatedAt;
|
25
|
+
if (elapsed <= this.timeToLive) {
|
26
|
+
return [extra_memoize_1.State.Hit, record.value];
|
27
|
+
}
|
28
|
+
else if (elapsed <= this.timeToLive + this.staleWhileRevalidate) {
|
29
|
+
return [extra_memoize_1.State.StaleWhileRevalidate, record.value];
|
30
|
+
}
|
31
|
+
else if (elapsed <= this.timeToLive + this.staleWhileRevalidate + this.staleIfError) {
|
32
|
+
return [extra_memoize_1.State.StaleIfError, record.value];
|
33
|
+
}
|
34
|
+
else {
|
35
|
+
return [extra_memoize_1.State.Miss, undefined];
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
exports.TLRUCacheWithStaleWhileRevalidateAndStaleIfError = TLRUCacheWithStaleWhileRevalidateAndStaleIfError;
|
40
|
+
//# 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,6CAAwC;AACxC,iDAAgF;AAChF,iDAAiD;AAOjD,MAAa,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,sBAAS,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;QAEb,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAClC,IAAI,IAAA,qBAAW,EAAC,MAAM,CAAC;YAAE,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;QAEvD,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,SAAS,CAAA;QAC7C,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,EAAE;YAC9B,OAAO,CAAC,qBAAK,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;SACjC;aAAM,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,EAAE;YACjE,OAAO,CAAC,qBAAK,CAAC,oBAAoB,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;SAClD;aAAM,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,YAAY,EAAE;YACrF,OAAO,CAAC,qBAAK,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;SAC1C;aAAM;YAEL,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;SAC/B;IACH,CAAC;CACF;AApCD,4GAoCC"}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { IStaleWhileRevalidateCache } from 'extra-memoize';
|
2
|
+
export declare class TLRUCacheWithStaleWhileRevalidate<T = any> implements IStaleWhileRevalidateCache<T> {
|
3
|
+
private timeToLive;
|
4
|
+
private staleWhileRevalidate;
|
5
|
+
private cache;
|
6
|
+
constructor(limit: number, timeToLive: number, staleWhileRevalidate: number);
|
7
|
+
set(key: string, value: T): void;
|
8
|
+
get(key: string): T | undefined;
|
9
|
+
isStaleWhileRevalidate(key: string): boolean;
|
10
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.TLRUCacheWithStaleWhileRevalidate = void 0;
|
4
|
+
const tlru_cache_1 = require("./tlru-cache");
|
5
|
+
const prelude_1 = require("@blackglory/prelude");
|
6
|
+
class TLRUCacheWithStaleWhileRevalidate {
|
7
|
+
constructor(limit, timeToLive, staleWhileRevalidate) {
|
8
|
+
this.timeToLive = timeToLive;
|
9
|
+
this.staleWhileRevalidate = staleWhileRevalidate;
|
10
|
+
this.cache = new tlru_cache_1.TLRUCache(limit, timeToLive + staleWhileRevalidate);
|
11
|
+
}
|
12
|
+
set(key, value) {
|
13
|
+
this.cache.set(key, {
|
14
|
+
value,
|
15
|
+
updatedAt: Date.now()
|
16
|
+
});
|
17
|
+
}
|
18
|
+
get(key) {
|
19
|
+
var _a;
|
20
|
+
return (_a = this.cache.get(key)) === null || _a === void 0 ? void 0 : _a.value;
|
21
|
+
}
|
22
|
+
isStaleWhileRevalidate(key) {
|
23
|
+
const record = this.cache.get(key);
|
24
|
+
if ((0, prelude_1.isUndefined)(record))
|
25
|
+
return false;
|
26
|
+
return Date.now() - record.updatedAt > this.timeToLive
|
27
|
+
&& Date.now() - record.updatedAt <= this.timeToLive + this.staleWhileRevalidate;
|
28
|
+
}
|
29
|
+
}
|
30
|
+
exports.TLRUCacheWithStaleWhileRevalidate = TLRUCacheWithStaleWhileRevalidate;
|
31
|
+
//# 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,6CAAwC;AAExC,iDAAiD;AAOjD,MAAa,iCAAiC;IAG5C,YACE,KAAa,EACL,UAAkB,EAClB,oBAA4B;QAD5B,eAAU,GAAV,UAAU,CAAQ;QAClB,yBAAoB,GAApB,oBAAoB,CAAQ;QAEpC,IAAI,CAAC,KAAK,GAAG,IAAI,sBAAS,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,OAAO,MAAA,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,0CAAE,KAAK,CAAA;IACnC,CAAC;IAED,sBAAsB,CAAC,GAAW;QAChC,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAClC,IAAI,IAAA,qBAAW,EAAC,MAAM,CAAC;YAAE,OAAO,KAAK,CAAA;QAErC,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU;eAC/C,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,SAAS,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,CAAA;IACrF,CAAC;CACF;AA7BD,8EA6BC"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { ICache } from 'extra-memoize';
|
2
|
+
export declare class TLRUCache<T = any> implements ICache<T> {
|
3
|
+
private timeToLive;
|
4
|
+
private map;
|
5
|
+
constructor(limit: number, timeToLive: number);
|
6
|
+
set(key: string, value: T): void;
|
7
|
+
get(key: string): T | undefined;
|
8
|
+
clear(): void;
|
9
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.TLRUCache = void 0;
|
4
|
+
const structures_1 = require("@blackglory/structures");
|
5
|
+
class TLRUCache {
|
6
|
+
constructor(limit, timeToLive) {
|
7
|
+
this.timeToLive = timeToLive;
|
8
|
+
this.map = new structures_1.TLRUMap(limit);
|
9
|
+
}
|
10
|
+
set(key, value) {
|
11
|
+
this.map.set(key, value, this.timeToLive);
|
12
|
+
}
|
13
|
+
get(key) {
|
14
|
+
return this.map.get(key);
|
15
|
+
}
|
16
|
+
clear() {
|
17
|
+
this.map.clear();
|
18
|
+
}
|
19
|
+
}
|
20
|
+
exports.TLRUCache = TLRUCache;
|
21
|
+
//# 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,uDAAgD;AAGhD,MAAa,SAAS;IAGpB,YAAY,KAAa,EAAU,UAAkB;QAAlB,eAAU,GAAV,UAAU,CAAQ;QACnD,IAAI,CAAC,GAAG,GAAG,IAAI,oBAAO,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,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAC1B,CAAC;IAED,KAAK;QACH,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;IAClB,CAAC;CACF;AAlBD,8BAkBC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './caches/index';
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./caches/index"), exports);
|
18
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAuB"}
|
package/package.json
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
{
|
2
|
+
"name": "@extra-memoize/memory-cache",
|
3
|
+
"version": "0.1.0",
|
4
|
+
"description": "",
|
5
|
+
"keywords": [],
|
6
|
+
"files": [
|
7
|
+
"lib",
|
8
|
+
"dist"
|
9
|
+
],
|
10
|
+
"main": "lib/es2018/index.js",
|
11
|
+
"types": "lib/es2018/index.d.ts",
|
12
|
+
"sideEffects": false,
|
13
|
+
"repository": "git@github.com:extra-memoize/memory-cache.git",
|
14
|
+
"author": "BlackGlory <woshenmedoubuzhidao@blackglory.me>",
|
15
|
+
"license": "MIT",
|
16
|
+
"scripts": {
|
17
|
+
"lint": "eslint --ext .js,.jsx,.ts,.tsx --quiet src __tests__",
|
18
|
+
"test": "jest --runInBand --config jest.config.js",
|
19
|
+
"test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand",
|
20
|
+
"test:coverage": "jest --coverage --config jest.config.js",
|
21
|
+
"prepublishOnly": "run-s clean build bundle",
|
22
|
+
"clean": "run-p clean:*",
|
23
|
+
"clean:build": "rimraf lib",
|
24
|
+
"clean:bundle": "rimraf dist",
|
25
|
+
"build": "run-p build:*",
|
26
|
+
"build:es2015": "run-s build:es2015:*",
|
27
|
+
"build:es2015:compile": "tsc --project tsconfig.build.json --module commonjs --target es2015 --outDir lib/es2015",
|
28
|
+
"build:es2015:patch": "tscpaths -p tsconfig.build.json -s ./src -o ./lib/es2015",
|
29
|
+
"build:es2018": "run-s build:es2018:*",
|
30
|
+
"build:es2018:compile": "tsc --project tsconfig.build.json --module commonjs --target es2018 --outDir lib/es2018",
|
31
|
+
"build:es2018:patch": "tscpaths -p tsconfig.build.json -s ./src -o ./lib/es2018",
|
32
|
+
"bundle": "rollup --config rollup.config.js",
|
33
|
+
"release": "standard-version"
|
34
|
+
},
|
35
|
+
"husky": {
|
36
|
+
"hooks": {
|
37
|
+
"pre-commit": "run-s lint build test",
|
38
|
+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
|
39
|
+
}
|
40
|
+
},
|
41
|
+
"devDependencies": {
|
42
|
+
"@blackglory/jest-matchers": "^0.3.1",
|
43
|
+
"@commitlint/cli": "^16.2.3",
|
44
|
+
"@commitlint/config-conventional": "^16.2.1",
|
45
|
+
"@rollup/plugin-commonjs": "^21.0.3",
|
46
|
+
"@rollup/plugin-json": "^4.1.0",
|
47
|
+
"@rollup/plugin-node-resolve": "^13.1.3",
|
48
|
+
"@rollup/plugin-replace": "^4.0.0",
|
49
|
+
"@rollup/plugin-typescript": "^8.3.1",
|
50
|
+
"@types/jest": "^27.4.1",
|
51
|
+
"@typescript-eslint/eslint-plugin": "^5.18.0",
|
52
|
+
"@typescript-eslint/parser": "^5.18.0",
|
53
|
+
"eslint": "^8.12.0",
|
54
|
+
"extra-memoize": "^0.5.0",
|
55
|
+
"extra-promise": "^1.0.2",
|
56
|
+
"husky": "^4.3.8",
|
57
|
+
"jest": "^27.5.1",
|
58
|
+
"npm-run-all": "^4.1.5",
|
59
|
+
"return-style": "^1.0.0",
|
60
|
+
"rimraf": "^3.0.2",
|
61
|
+
"rollup": "^2.70.1",
|
62
|
+
"rollup-plugin-analyzer": "^4.0.0",
|
63
|
+
"rollup-plugin-terser": "^7.0.2",
|
64
|
+
"standard-version": "^9.3.2",
|
65
|
+
"ts-jest": "^27.1.4",
|
66
|
+
"tscpaths": "^0.0.9",
|
67
|
+
"tslib": "^2.3.1",
|
68
|
+
"typescript": "^4.6.3"
|
69
|
+
},
|
70
|
+
"dependencies": {
|
71
|
+
"@blackglory/prelude": "^0.1.1",
|
72
|
+
"@blackglory/structures": "^0.3.5"
|
73
|
+
},
|
74
|
+
"peerDependencies": {
|
75
|
+
"extra-memoize": "^0.5.0"
|
76
|
+
}
|
77
|
+
}
|