@extra-memoize/extra-disk-cache 0.8.4 → 0.9.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/disk-cache.js +6 -10
- package/lib/disk-cache.js.map +1 -1
- package/lib/index.d.ts +4 -4
- package/lib/index.js +4 -20
- package/lib/index.js.map +1 -1
- package/lib/stale-if-error-async-disk-cache.js +9 -12
- package/lib/stale-if-error-async-disk-cache.js.map +1 -1
- package/lib/stale-while-revalidate-and-stale-if-error-async-disk-cache.js +10 -13
- package/lib/stale-while-revalidate-and-stale-if-error-async-disk-cache.js.map +1 -1
- package/lib/stale-while-revalidate-async-disk-cache.js +7 -11
- package/lib/stale-while-revalidate-async-disk-cache.js.map +1 -1
- package/package.json +33 -31
- package/src/disk-cache.ts +25 -0
- package/src/index.ts +4 -0
- package/src/stale-if-error-async-disk-cache.ts +38 -0
- package/src/stale-while-revalidate-and-stale-if-error-async-disk-cache.ts +41 -0
- package/src/stale-while-revalidate-async-disk-cache.ts +40 -0
package/lib/disk-cache.js
CHANGED
|
@@ -1,25 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const extra_memoize_1 = require("extra-memoize");
|
|
5
|
-
const prelude_1 = require("@blackglory/prelude");
|
|
6
|
-
class DiskCache {
|
|
1
|
+
import { State } from 'extra-memoize';
|
|
2
|
+
import { isUndefined } from '@blackglory/prelude';
|
|
3
|
+
export class DiskCache {
|
|
7
4
|
constructor(view, timeToLive) {
|
|
8
5
|
this.view = view;
|
|
9
6
|
this.timeToLive = timeToLive;
|
|
10
7
|
}
|
|
11
8
|
get(key) {
|
|
12
9
|
const item = this.view.getWithMetadata(key);
|
|
13
|
-
if (
|
|
14
|
-
return [
|
|
10
|
+
if (isUndefined(item)) {
|
|
11
|
+
return [State.Miss];
|
|
15
12
|
}
|
|
16
13
|
else {
|
|
17
|
-
return [
|
|
14
|
+
return [State.Hit, item.value];
|
|
18
15
|
}
|
|
19
16
|
}
|
|
20
17
|
set(key, value) {
|
|
21
18
|
this.view.set(key, value, this.timeToLive);
|
|
22
19
|
}
|
|
23
20
|
}
|
|
24
|
-
exports.DiskCache = DiskCache;
|
|
25
21
|
//# sourceMappingURL=disk-cache.js.map
|
package/lib/disk-cache.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"disk-cache.js","sourceRoot":"","sources":["../src/disk-cache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"disk-cache.js","sourceRoot":"","sources":["../src/disk-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,EAAE,MAAM,eAAe,CAAA;AAE7C,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAEjD,MAAM,OAAO,SAAS;IACpB,YACU,IAA8B,EAC9B,UAAmB;QADnB,SAAI,GAAJ,IAAI,CAA0B;QAC9B,eAAU,GAAV,UAAU,CAAS;IAC1B,CAAC;IAEJ,GAAG,CAAC,GAAW;QAGb,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC;YACtB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;QAChC,CAAC;IACH,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,CAAA;IAC5C,CAAC;CACF"}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from './disk-cache';
|
|
2
|
-
export * from './stale-while-revalidate-async-disk-cache';
|
|
3
|
-
export * from './stale-if-error-async-disk-cache';
|
|
4
|
-
export * from './stale-while-revalidate-and-stale-if-error-async-disk-cache';
|
|
1
|
+
export * from './disk-cache.js';
|
|
2
|
+
export * from './stale-while-revalidate-async-disk-cache.js';
|
|
3
|
+
export * from './stale-if-error-async-disk-cache.js';
|
|
4
|
+
export * from './stale-while-revalidate-and-stale-if-error-async-disk-cache.js';
|
package/lib/index.js
CHANGED
|
@@ -1,21 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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("./disk-cache"), exports);
|
|
18
|
-
__exportStar(require("./stale-while-revalidate-async-disk-cache"), exports);
|
|
19
|
-
__exportStar(require("./stale-if-error-async-disk-cache"), exports);
|
|
20
|
-
__exportStar(require("./stale-while-revalidate-and-stale-if-error-async-disk-cache"), exports);
|
|
1
|
+
export * from './disk-cache.js';
|
|
2
|
+
export * from './stale-while-revalidate-async-disk-cache.js';
|
|
3
|
+
export * from './stale-if-error-async-disk-cache.js';
|
|
4
|
+
export * from './stale-while-revalidate-and-stale-if-error-async-disk-cache.js';
|
|
21
5
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,8CAA8C,CAAA;AAC5D,cAAc,sCAAsC,CAAA;AACpD,cAAc,iEAAiE,CAAA"}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const extra_memoize_1 = require("extra-memoize");
|
|
5
|
-
const prelude_1 = require("@blackglory/prelude");
|
|
6
|
-
class StaleIfErrorDiskCache {
|
|
1
|
+
import { State } from 'extra-memoize';
|
|
2
|
+
import { isUndefined } from '@blackglory/prelude';
|
|
3
|
+
export class StaleIfErrorDiskCache {
|
|
7
4
|
constructor(view, timeToLive, staleIfError) {
|
|
8
5
|
this.view = view;
|
|
9
6
|
this.timeToLive = timeToLive;
|
|
@@ -11,19 +8,20 @@ class StaleIfErrorDiskCache {
|
|
|
11
8
|
}
|
|
12
9
|
get(key) {
|
|
13
10
|
const item = this.view.getWithMetadata(key);
|
|
14
|
-
if (
|
|
15
|
-
return [
|
|
11
|
+
if (isUndefined(item)) {
|
|
12
|
+
return [State.Miss];
|
|
16
13
|
}
|
|
17
14
|
else {
|
|
18
15
|
const timestamp = Date.now();
|
|
19
16
|
if (item.updatedAt + this.timeToLive > timestamp) {
|
|
20
|
-
return [
|
|
17
|
+
return [State.Hit, item.value];
|
|
21
18
|
}
|
|
22
19
|
else if (item.updatedAt + this.timeToLive + this.staleIfError > timestamp) {
|
|
23
|
-
return [
|
|
20
|
+
return [State.StaleIfError, item.value];
|
|
24
21
|
}
|
|
25
22
|
else {
|
|
26
|
-
|
|
23
|
+
// just in case
|
|
24
|
+
return [State.Miss];
|
|
27
25
|
}
|
|
28
26
|
}
|
|
29
27
|
}
|
|
@@ -31,5 +29,4 @@ class StaleIfErrorDiskCache {
|
|
|
31
29
|
this.view.set(key, value, this.timeToLive + this.staleIfError);
|
|
32
30
|
}
|
|
33
31
|
}
|
|
34
|
-
exports.StaleIfErrorDiskCache = StaleIfErrorDiskCache;
|
|
35
32
|
//# sourceMappingURL=stale-if-error-async-disk-cache.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stale-if-error-async-disk-cache.js","sourceRoot":"","sources":["../src/stale-if-error-async-disk-cache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stale-if-error-async-disk-cache.js","sourceRoot":"","sources":["../src/stale-if-error-async-disk-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,EAAE,MAAM,eAAe,CAAA;AAEzD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAEjD,MAAM,OAAO,qBAAqB;IAChC,YACU,IAA8B,EAC9B,UAAkB,EAClB,YAAoB;QAFpB,SAAI,GAAJ,IAAI,CAA0B;QAC9B,eAAU,GAAV,UAAU,CAAQ;QAClB,iBAAY,GAAZ,YAAY,CAAQ;IAC3B,CAAC;IAEJ,GAAG,CAAC,GAAW;QAGb,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC;YACtB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;aAAM,CAAC;YACN,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;YAC5B,IAAI,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,SAAS,EAAE,CAAC;gBACjD,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;YAChC,CAAC;iBAAM,IAAI,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,YAAY,GAAG,SAAS,EAAE,CAAC;gBAC5E,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;YACzC,CAAC;iBAAM,CAAC;gBACN,eAAe;gBACf,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;YACrB,CAAC;QACH,CAAC;IACH,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,IAAI,CAAC,GAAG,CACX,GAAG,EACH,KAAK,EACL,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,YAAY,CACpC,CAAA;IACH,CAAC;CACF"}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const extra_memoize_1 = require("extra-memoize");
|
|
5
|
-
const prelude_1 = require("@blackglory/prelude");
|
|
6
|
-
class StaleWhileRevalidateAndStaleIfErrorDiskCache {
|
|
1
|
+
import { State } from 'extra-memoize';
|
|
2
|
+
import { isUndefined } from '@blackglory/prelude';
|
|
3
|
+
export class StaleWhileRevalidateAndStaleIfErrorDiskCache {
|
|
7
4
|
constructor(view, timeToLive, staleWhileRevalidate, staleIfError) {
|
|
8
5
|
this.view = view;
|
|
9
6
|
this.timeToLive = timeToLive;
|
|
@@ -12,22 +9,23 @@ class StaleWhileRevalidateAndStaleIfErrorDiskCache {
|
|
|
12
9
|
}
|
|
13
10
|
get(key) {
|
|
14
11
|
const item = this.view.getWithMetadata(key);
|
|
15
|
-
if (
|
|
16
|
-
return [
|
|
12
|
+
if (isUndefined(item)) {
|
|
13
|
+
return [State.Miss];
|
|
17
14
|
}
|
|
18
15
|
else {
|
|
19
16
|
const timestamp = Date.now();
|
|
20
17
|
if (item.updatedAt + this.timeToLive > timestamp) {
|
|
21
|
-
return [
|
|
18
|
+
return [State.Hit, item.value];
|
|
22
19
|
}
|
|
23
20
|
else if (item.updatedAt + this.timeToLive + this.staleWhileRevalidate > timestamp) {
|
|
24
|
-
return [
|
|
21
|
+
return [State.StaleWhileRevalidate, item.value];
|
|
25
22
|
}
|
|
26
23
|
else if (item.updatedAt + this.timeToLive + this.staleWhileRevalidate + this.staleIfError > timestamp) {
|
|
27
|
-
return [
|
|
24
|
+
return [State.StaleIfError, item.value];
|
|
28
25
|
}
|
|
29
26
|
else {
|
|
30
|
-
|
|
27
|
+
// just in case
|
|
28
|
+
return [State.Miss];
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
31
|
}
|
|
@@ -35,5 +33,4 @@ class StaleWhileRevalidateAndStaleIfErrorDiskCache {
|
|
|
35
33
|
this.view.set(key, value, this.timeToLive + this.staleWhileRevalidate + this.staleIfError);
|
|
36
34
|
}
|
|
37
35
|
}
|
|
38
|
-
exports.StaleWhileRevalidateAndStaleIfErrorDiskCache = StaleWhileRevalidateAndStaleIfErrorDiskCache;
|
|
39
36
|
//# sourceMappingURL=stale-while-revalidate-and-stale-if-error-async-disk-cache.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stale-while-revalidate-and-stale-if-error-async-disk-cache.js","sourceRoot":"","sources":["../src/stale-while-revalidate-and-stale-if-error-async-disk-cache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stale-while-revalidate-and-stale-if-error-async-disk-cache.js","sourceRoot":"","sources":["../src/stale-while-revalidate-and-stale-if-error-async-disk-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6C,KAAK,EAAE,MAAM,eAAe,CAAA;AAEhF,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAEjD,MAAM,OAAO,4CAA4C;IACvD,YACU,IAA8B,EAC9B,UAAkB,EAClB,oBAA4B,EAC5B,YAAoB;QAHpB,SAAI,GAAJ,IAAI,CAA0B;QAC9B,eAAU,GAAV,UAAU,CAAQ;QAClB,yBAAoB,GAApB,oBAAoB,CAAQ;QAC5B,iBAAY,GAAZ,YAAY,CAAQ;IAC3B,CAAC;IAEJ,GAAG,CAAC,GAAW;QAGb,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC;YACtB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;aAAM,CAAC;YACN,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;YAC5B,IAAI,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,SAAS,EAAE,CAAC;gBACjD,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;YAChC,CAAC;iBAAM,IAAI,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,SAAS,EAAE,CAAC;gBACpF,OAAO,CAAC,KAAK,CAAC,oBAAoB,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;YACjD,CAAC;iBAAM,IAAI,IAAI,CAAC,SAAS,GAAE,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,YAAY,GAAG,SAAS,EAAE,CAAC;gBACvG,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;YACzC,CAAC;iBAAM,CAAC;gBACN,eAAe;gBACf,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;YACrB,CAAC;QACH,CAAC;IACH,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,IAAI,CAAC,GAAG,CACX,GAAG,EACH,KAAK,EACL,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,YAAY,CAChE,CAAA;IACH,CAAC;CACF"}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const extra_memoize_1 = require("extra-memoize");
|
|
5
|
-
const prelude_1 = require("@blackglory/prelude");
|
|
6
|
-
class StaleWhileRevalidateDiskCache {
|
|
1
|
+
import { State } from 'extra-memoize';
|
|
2
|
+
import { isUndefined } from '@blackglory/prelude';
|
|
3
|
+
export class StaleWhileRevalidateDiskCache {
|
|
7
4
|
constructor(view, timeToLive, staleWhileRevalidate) {
|
|
8
5
|
this.view = view;
|
|
9
6
|
this.timeToLive = timeToLive;
|
|
@@ -11,15 +8,15 @@ class StaleWhileRevalidateDiskCache {
|
|
|
11
8
|
}
|
|
12
9
|
get(key) {
|
|
13
10
|
const item = this.view.getWithMetadata(key);
|
|
14
|
-
if (
|
|
15
|
-
return [
|
|
11
|
+
if (isUndefined(item)) {
|
|
12
|
+
return [State.Miss];
|
|
16
13
|
}
|
|
17
14
|
else {
|
|
18
15
|
if (this.isStaleWhileRevalidate(item.updatedAt)) {
|
|
19
|
-
return [
|
|
16
|
+
return [State.StaleWhileRevalidate, item.value];
|
|
20
17
|
}
|
|
21
18
|
else {
|
|
22
|
-
return [
|
|
19
|
+
return [State.Hit, item.value];
|
|
23
20
|
}
|
|
24
21
|
}
|
|
25
22
|
}
|
|
@@ -32,5 +29,4 @@ class StaleWhileRevalidateDiskCache {
|
|
|
32
29
|
&& updatedAt + this.timeToLive + this.staleWhileRevalidate > timestamp;
|
|
33
30
|
}
|
|
34
31
|
}
|
|
35
|
-
exports.StaleWhileRevalidateDiskCache = StaleWhileRevalidateDiskCache;
|
|
36
32
|
//# sourceMappingURL=stale-while-revalidate-async-disk-cache.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stale-while-revalidate-async-disk-cache.js","sourceRoot":"","sources":["../src/stale-while-revalidate-async-disk-cache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stale-while-revalidate-async-disk-cache.js","sourceRoot":"","sources":["../src/stale-while-revalidate-async-disk-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,KAAK,EAAE,MAAM,eAAe,CAAA;AAEjE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAEjD,MAAM,OAAO,6BAA6B;IACxC,YACU,IAA8B,EAC9B,UAAkB,EAClB,oBAA4B;QAF5B,SAAI,GAAJ,IAAI,CAA0B;QAC9B,eAAU,GAAV,UAAU,CAAQ;QAClB,yBAAoB,GAApB,oBAAoB,CAAQ;IACnC,CAAC;IAEJ,GAAG,CAAC,GAAW;QAGb,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC;YACtB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;aAAM,CAAC;YACN,IAAI,IAAI,CAAC,sBAAsB,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;gBAChD,OAAO,CAAC,KAAK,CAAC,oBAAoB,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;YACjD,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;YAChC,CAAC;QACH,CAAC;IACH,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,IAAI,CAAC,IAAI,CAAC,GAAG,CACX,GAAG,EACH,KAAK,EACL,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,CAC5C,CAAA;IACH,CAAC;IAEO,sBAAsB,CAAC,SAAiB;QAC9C,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAC5B,OAAO,SAAS,GAAG,IAAI,CAAC,UAAU,IAAI,SAAS;eACxC,SAAS,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,SAAS,CAAA;IAC5E,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,60 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@extra-memoize/extra-disk-cache",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"files": [
|
|
6
|
-
"lib"
|
|
6
|
+
"lib",
|
|
7
|
+
"src"
|
|
7
8
|
],
|
|
8
9
|
"main": "lib/index.js",
|
|
9
10
|
"types": "lib/index.d.ts",
|
|
10
|
-
"sideEffects": false,
|
|
11
11
|
"repository": "git@github.com:extra-memoize/extra-disk-cache.git",
|
|
12
12
|
"author": "BlackGlory <woshenmedoubuzhidao@blackglory.me>",
|
|
13
13
|
"license": "MIT",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=22"
|
|
18
|
+
},
|
|
14
19
|
"scripts": {
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"test
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"clean:build": "rimraf lib",
|
|
22
|
-
"build": "run-s build:*",
|
|
23
|
-
"build:compile": "tsc --project tsconfig.build.json --module commonjs --target es2018 --outDir lib",
|
|
24
|
-
"build:patch": "tscpaths -p tsconfig.build.json -s ./src -o ./lib",
|
|
20
|
+
"prepare": "ts-patch install -s",
|
|
21
|
+
"lint": "eslint --quiet src",
|
|
22
|
+
"test": "vitest run --passWithNoTests",
|
|
23
|
+
"prepublishOnly": "run-s prepare clean build",
|
|
24
|
+
"clean": "rimraf lib",
|
|
25
|
+
"build": "tsc --project tsconfig.build.json --outDir lib",
|
|
25
26
|
"release": "standard-version"
|
|
26
27
|
},
|
|
27
28
|
"husky": {
|
|
28
29
|
"hooks": {
|
|
29
|
-
"pre-commit": "run-s lint build",
|
|
30
|
+
"pre-commit": "run-s prepare clean lint build test",
|
|
30
31
|
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
|
|
31
32
|
}
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
|
-
"@commitlint/cli": "^
|
|
35
|
-
"@commitlint/config-conventional": "^
|
|
36
|
-
"@
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"extra-disk-cache": "^0.11.1",
|
|
41
|
-
"extra-memoize": "^0.9.1",
|
|
35
|
+
"@commitlint/cli": "^19.8.1",
|
|
36
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
37
|
+
"@eslint/js": "^9.30.1",
|
|
38
|
+
"eslint": "^9.30.1",
|
|
39
|
+
"extra-disk-cache": "^0.12.2",
|
|
40
|
+
"extra-memoize": "^0.10.0",
|
|
42
41
|
"husky": "^4.3.8",
|
|
43
|
-
"jest": "^29.3.1",
|
|
44
42
|
"npm-run-all": "^4.1.5",
|
|
45
|
-
"return-style": "^
|
|
46
|
-
"rimraf": "^
|
|
43
|
+
"return-style": "^3.0.1",
|
|
44
|
+
"rimraf": "^6.0.1",
|
|
47
45
|
"standard-version": "^9.5.0",
|
|
48
|
-
"ts-
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"typescript": "
|
|
46
|
+
"ts-patch": "^3.3.0",
|
|
47
|
+
"tslib": "^2.8.1",
|
|
48
|
+
"typescript": "5.8",
|
|
49
|
+
"typescript-eslint": "^8.35.1",
|
|
50
|
+
"typescript-transform-paths": "^3.5.5",
|
|
51
|
+
"vite": "^7.0.2",
|
|
52
|
+
"vite-tsconfig-paths": "^5.1.4",
|
|
53
|
+
"vitest": "^3.2.4"
|
|
52
54
|
},
|
|
53
55
|
"dependencies": {
|
|
54
|
-
"@blackglory/prelude": "^0.
|
|
56
|
+
"@blackglory/prelude": "^0.4.0"
|
|
55
57
|
},
|
|
56
58
|
"peerDependencies": {
|
|
57
59
|
"extra-disk-cache": "^0.10.1 || ^0.11.1 || ^0.12.0",
|
|
58
|
-
"extra-memoize": "^0.6.0 || ^0.7.0 || ^0.8.0 || ^0.9.0"
|
|
60
|
+
"extra-memoize": "^0.6.0 || ^0.7.0 || ^0.8.0 || ^0.9.0 || ^0.10.0"
|
|
59
61
|
}
|
|
60
62
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ICache, State } from 'extra-memoize'
|
|
2
|
+
import { DiskCacheView } from 'extra-disk-cache'
|
|
3
|
+
import { isUndefined } from '@blackglory/prelude'
|
|
4
|
+
|
|
5
|
+
export class DiskCache<T> implements ICache<T> {
|
|
6
|
+
constructor(
|
|
7
|
+
private view: DiskCacheView<string, T>
|
|
8
|
+
, private timeToLive?: number
|
|
9
|
+
) {}
|
|
10
|
+
|
|
11
|
+
get(key: string):
|
|
12
|
+
| [State.Miss]
|
|
13
|
+
| [State.Hit, T] {
|
|
14
|
+
const item = this.view.getWithMetadata(key)
|
|
15
|
+
if (isUndefined(item)) {
|
|
16
|
+
return [State.Miss]
|
|
17
|
+
} else {
|
|
18
|
+
return [State.Hit, item.value]
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
set(key: string, value: T): void {
|
|
23
|
+
this.view.set(key, value, this.timeToLive)
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { IStaleIfErrorCache, State } from 'extra-memoize'
|
|
2
|
+
import { DiskCacheView } from 'extra-disk-cache'
|
|
3
|
+
import { isUndefined } from '@blackglory/prelude'
|
|
4
|
+
|
|
5
|
+
export class StaleIfErrorDiskCache<T> implements IStaleIfErrorCache<T> {
|
|
6
|
+
constructor(
|
|
7
|
+
private view: DiskCacheView<string, T>
|
|
8
|
+
, private timeToLive: number
|
|
9
|
+
, private staleIfError: number
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
get(key: string):
|
|
13
|
+
| [State.Miss]
|
|
14
|
+
| [State.Hit | State.StaleIfError, T] {
|
|
15
|
+
const item = this.view.getWithMetadata(key)
|
|
16
|
+
if (isUndefined(item)) {
|
|
17
|
+
return [State.Miss]
|
|
18
|
+
} else {
|
|
19
|
+
const timestamp = Date.now()
|
|
20
|
+
if (item.updatedAt + this.timeToLive > timestamp) {
|
|
21
|
+
return [State.Hit, item.value]
|
|
22
|
+
} else if (item.updatedAt + this.timeToLive + this.staleIfError > timestamp) {
|
|
23
|
+
return [State.StaleIfError, item.value]
|
|
24
|
+
} else {
|
|
25
|
+
// just in case
|
|
26
|
+
return [State.Miss]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
set(key: string, value: T): void {
|
|
32
|
+
this.view.set(
|
|
33
|
+
key
|
|
34
|
+
, value
|
|
35
|
+
, this.timeToLive + this.staleIfError
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { IStaleWhileRevalidateAndStaleIfErrorCache, State } from 'extra-memoize'
|
|
2
|
+
import { DiskCacheView } from 'extra-disk-cache'
|
|
3
|
+
import { isUndefined } from '@blackglory/prelude'
|
|
4
|
+
|
|
5
|
+
export class StaleWhileRevalidateAndStaleIfErrorDiskCache<T> implements IStaleWhileRevalidateAndStaleIfErrorCache<T> {
|
|
6
|
+
constructor(
|
|
7
|
+
private view: DiskCacheView<string, T>
|
|
8
|
+
, private timeToLive: number
|
|
9
|
+
, private staleWhileRevalidate: number
|
|
10
|
+
, private staleIfError: number
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
get(key: string):
|
|
14
|
+
| [State.Miss]
|
|
15
|
+
| [State.Hit | State.StaleWhileRevalidate | State.StaleIfError, T] {
|
|
16
|
+
const item = this.view.getWithMetadata(key)
|
|
17
|
+
if (isUndefined(item)) {
|
|
18
|
+
return [State.Miss]
|
|
19
|
+
} else {
|
|
20
|
+
const timestamp = Date.now()
|
|
21
|
+
if (item.updatedAt + this.timeToLive > timestamp) {
|
|
22
|
+
return [State.Hit, item.value]
|
|
23
|
+
} else if (item.updatedAt + this.timeToLive + this.staleWhileRevalidate > timestamp) {
|
|
24
|
+
return [State.StaleWhileRevalidate, item.value]
|
|
25
|
+
} else if (item.updatedAt+ this.timeToLive + this.staleWhileRevalidate + this.staleIfError > timestamp) {
|
|
26
|
+
return [State.StaleIfError, item.value]
|
|
27
|
+
} else {
|
|
28
|
+
// just in case
|
|
29
|
+
return [State.Miss]
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
set(key: string, value: T): void {
|
|
35
|
+
this.view.set(
|
|
36
|
+
key
|
|
37
|
+
, value
|
|
38
|
+
, this.timeToLive + this.staleWhileRevalidate + this.staleIfError
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { IStaleWhileRevalidateCache, State } from 'extra-memoize'
|
|
2
|
+
import { DiskCacheView } from 'extra-disk-cache'
|
|
3
|
+
import { isUndefined } from '@blackglory/prelude'
|
|
4
|
+
|
|
5
|
+
export class StaleWhileRevalidateDiskCache<T> implements IStaleWhileRevalidateCache<T> {
|
|
6
|
+
constructor(
|
|
7
|
+
private view: DiskCacheView<string, T>
|
|
8
|
+
, private timeToLive: number
|
|
9
|
+
, private staleWhileRevalidate: number
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
get(key: string):
|
|
13
|
+
| [State.Miss]
|
|
14
|
+
| [State.Hit | State.StaleWhileRevalidate, T] {
|
|
15
|
+
const item = this.view.getWithMetadata(key)
|
|
16
|
+
if (isUndefined(item)) {
|
|
17
|
+
return [State.Miss]
|
|
18
|
+
} else {
|
|
19
|
+
if (this.isStaleWhileRevalidate(item.updatedAt)) {
|
|
20
|
+
return [State.StaleWhileRevalidate, item.value]
|
|
21
|
+
} else {
|
|
22
|
+
return [State.Hit, item.value]
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
set(key: string, value: T): void {
|
|
28
|
+
this.view.set(
|
|
29
|
+
key
|
|
30
|
+
, value
|
|
31
|
+
, this.timeToLive + this.staleWhileRevalidate
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
private isStaleWhileRevalidate(updatedAt: number): boolean {
|
|
36
|
+
const timestamp = Date.now()
|
|
37
|
+
return updatedAt + this.timeToLive <= timestamp
|
|
38
|
+
&& updatedAt + this.timeToLive + this.staleWhileRevalidate > timestamp
|
|
39
|
+
}
|
|
40
|
+
}
|