@extra-memoize/extra-disk-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 +59 -0
- package/lib/es2015/async-disk-cache.d.ts +11 -0
- package/lib/es2015/async-disk-cache.js +36 -0
- package/lib/es2015/async-disk-cache.js.map +1 -0
- package/lib/es2015/index.d.ts +4 -0
- package/lib/es2015/index.js +21 -0
- package/lib/es2015/index.js.map +1 -0
- package/lib/es2015/stale-if-error-async-disk-cache.d.ts +13 -0
- package/lib/es2015/stale-if-error-async-disk-cache.js +49 -0
- package/lib/es2015/stale-if-error-async-disk-cache.js.map +1 -0
- package/lib/es2015/stale-while-revalidate-and-stale-if-error-async-disk-cache.d.ts +14 -0
- package/lib/es2015/stale-while-revalidate-and-stale-if-error-async-disk-cache.js +53 -0
- package/lib/es2015/stale-while-revalidate-and-stale-if-error-async-disk-cache.js.map +1 -0
- package/lib/es2015/stale-while-revalidate-async-disk-cache.d.ts +14 -0
- package/lib/es2015/stale-while-revalidate-async-disk-cache.js +48 -0
- package/lib/es2015/stale-while-revalidate-async-disk-cache.js.map +1 -0
- package/lib/es2015/utils.d.ts +3 -0
- package/lib/es2015/utils.js +12 -0
- package/lib/es2015/utils.js.map +1 -0
- package/lib/es2018/async-disk-cache.d.ts +11 -0
- package/lib/es2018/async-disk-cache.js +23 -0
- package/lib/es2018/async-disk-cache.js.map +1 -0
- package/lib/es2018/index.d.ts +4 -0
- package/lib/es2018/index.js +21 -0
- package/lib/es2018/index.js.map +1 -0
- package/lib/es2018/stale-if-error-async-disk-cache.d.ts +13 -0
- package/lib/es2018/stale-if-error-async-disk-cache.js +36 -0
- package/lib/es2018/stale-if-error-async-disk-cache.js.map +1 -0
- package/lib/es2018/stale-while-revalidate-and-stale-if-error-async-disk-cache.d.ts +14 -0
- package/lib/es2018/stale-while-revalidate-and-stale-if-error-async-disk-cache.js +40 -0
- package/lib/es2018/stale-while-revalidate-and-stale-if-error-async-disk-cache.js.map +1 -0
- package/lib/es2018/stale-while-revalidate-async-disk-cache.d.ts +14 -0
- package/lib/es2018/stale-while-revalidate-async-disk-cache.js +33 -0
- package/lib/es2018/stale-while-revalidate-async-disk-cache.js.map +1 -0
- package/lib/es2018/utils.d.ts +3 -0
- package/lib/es2018/utils.js +12 -0
- package/lib/es2018/utils.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 BlackGlory <woshenmedoubuzhidao@blackglory.me>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# @extra-memoize/extra-disk-cache
|
|
2
|
+
## Install
|
|
3
|
+
```sh
|
|
4
|
+
npm install --save @extra-memoize/extra-disk-cache
|
|
5
|
+
# or
|
|
6
|
+
yarn add @extra-memoize/extra-disk-cache
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
### API
|
|
10
|
+
#### AsyncDiskCache
|
|
11
|
+
```ts
|
|
12
|
+
class AsyncDiskCache<T> implements IAsyncCache<T> {
|
|
13
|
+
constructor(
|
|
14
|
+
cache: DiskCache
|
|
15
|
+
, toBuffer?: (value: T) => Buffer
|
|
16
|
+
, fromBuffer?: (buffer: Buffer) => T
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
#### StaleWhileRevalidateAsyncDiskCache
|
|
22
|
+
```ts
|
|
23
|
+
class StaleWhileRevalidateAsyncDiskCache<T> implements IStaleWhileRevalidateAsyncCache<T> {
|
|
24
|
+
constructor(
|
|
25
|
+
cache: DiskCache
|
|
26
|
+
, timeToLive: number
|
|
27
|
+
, staleWhileRevalidate: number
|
|
28
|
+
, toBuffer?: (value: T) => Buffer
|
|
29
|
+
, fromBuffer?: (buffer: Buffer) => T
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
#### StaleIfErrorAsyncDiskCache
|
|
35
|
+
```ts
|
|
36
|
+
class StaleIfErrorAsyncDiskCache<T> implements IStaleIfErrorAsyncCache<T> {
|
|
37
|
+
constructor(
|
|
38
|
+
cache: DiskCache
|
|
39
|
+
, timeToLive: number
|
|
40
|
+
, staleIfError: number
|
|
41
|
+
, toBuffer?: (value: T) => Buffer
|
|
42
|
+
, fromBuffer?: (buffer: Buffer) => T
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache
|
|
48
|
+
```ts
|
|
49
|
+
class StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache<T> implements IStaleWhileRevalidateAndStaleIfErrorAsyncCache<T> {
|
|
50
|
+
constructor(
|
|
51
|
+
cache: DiskCache
|
|
52
|
+
, timeToLive: number
|
|
53
|
+
, staleWhileRevalidate: number
|
|
54
|
+
, staleIfError: number
|
|
55
|
+
, toBuffer?: (value: T) => Buffer
|
|
56
|
+
, fromBuffer?: (buffer: Buffer) => T
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IAsyncCache } from 'extra-memoize';
|
|
3
|
+
import { DiskCache } from 'extra-disk-cache';
|
|
4
|
+
export declare class AsyncDiskCache<T> implements IAsyncCache<T> {
|
|
5
|
+
private cache;
|
|
6
|
+
private toBuffer;
|
|
7
|
+
private fromBuffer;
|
|
8
|
+
constructor(cache: DiskCache, toBuffer?: (value: T) => Buffer, fromBuffer?: (buffer: Buffer) => T);
|
|
9
|
+
get(key: string): Promise<T | undefined>;
|
|
10
|
+
set(key: string, value: T): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.AsyncDiskCache = void 0;
|
|
13
|
+
const prelude_1 = require("@blackglory/prelude");
|
|
14
|
+
const utils_1 = require("./utils");
|
|
15
|
+
class AsyncDiskCache {
|
|
16
|
+
constructor(cache, toBuffer = utils_1.defaultToBuffer, fromBuffer = utils_1.defaultFromBuffer) {
|
|
17
|
+
this.cache = cache;
|
|
18
|
+
this.toBuffer = toBuffer;
|
|
19
|
+
this.fromBuffer = fromBuffer;
|
|
20
|
+
}
|
|
21
|
+
get(key) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const value = yield this.cache.getData(key);
|
|
24
|
+
if ((0, prelude_1.isUndefined)(value))
|
|
25
|
+
return undefined;
|
|
26
|
+
return this.fromBuffer(value);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
set(key, value) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
yield this.cache.setData(key, this.toBuffer(value));
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.AsyncDiskCache = AsyncDiskCache;
|
|
36
|
+
//# sourceMappingURL=async-disk-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-disk-cache.js","sourceRoot":"","sources":["../../src/async-disk-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,iDAAiD;AACjD,mCAA4D;AAE5D,MAAa,cAAc;IACzB,YACU,KAAgB,EAChB,WAAiC,uBAAe,EAChD,aAAoC,yBAAiB;QAFrD,UAAK,GAAL,KAAK,CAAW;QAChB,aAAQ,GAAR,QAAQ,CAAwC;QAChD,eAAU,GAAV,UAAU,CAA2C;IAC5D,CAAC;IAEE,GAAG,CAAC,GAAW;;YACnB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;YAC3C,IAAI,IAAA,qBAAW,EAAC,KAAK,CAAC;gBAAE,OAAO,SAAS,CAAA;YAExC,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;QAC/B,CAAC;KAAA;IAEK,GAAG,CAAC,GAAW,EAAE,KAAQ;;YAC7B,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;QACrD,CAAC;KAAA;CACF;AAjBD,wCAiBC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
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("./async-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);
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,4EAAyD;AACzD,oEAAiD;AACjD,+FAA4E"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IStaleIfErrorAsyncCache, State } from 'extra-memoize';
|
|
3
|
+
import { DiskCache } from 'extra-disk-cache';
|
|
4
|
+
export declare class StaleIfErrorAsyncDiskCache<T> implements IStaleIfErrorAsyncCache<T> {
|
|
5
|
+
private cache;
|
|
6
|
+
private timeToLive;
|
|
7
|
+
private staleIfError;
|
|
8
|
+
private toBuffer;
|
|
9
|
+
private fromBuffer;
|
|
10
|
+
constructor(cache: DiskCache, timeToLive: number, staleIfError: number, toBuffer?: (value: T) => Buffer, fromBuffer?: (buffer: Buffer) => T);
|
|
11
|
+
get(key: string): Promise<[State.Miss, undefined] | [State.Hit | State.StaleIfError, T]>;
|
|
12
|
+
set(key: string, value: T): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.StaleIfErrorAsyncDiskCache = void 0;
|
|
13
|
+
const extra_memoize_1 = require("extra-memoize");
|
|
14
|
+
const prelude_1 = require("@blackglory/prelude");
|
|
15
|
+
const utils_1 = require("./utils");
|
|
16
|
+
class StaleIfErrorAsyncDiskCache {
|
|
17
|
+
constructor(cache, timeToLive, staleIfError, toBuffer = utils_1.defaultToBuffer, fromBuffer = utils_1.defaultFromBuffer) {
|
|
18
|
+
this.cache = cache;
|
|
19
|
+
this.timeToLive = timeToLive;
|
|
20
|
+
this.staleIfError = staleIfError;
|
|
21
|
+
this.toBuffer = toBuffer;
|
|
22
|
+
this.fromBuffer = fromBuffer;
|
|
23
|
+
}
|
|
24
|
+
get(key) {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
const value = yield this.cache.getData(key);
|
|
27
|
+
const metadata = this.cache.getMetadata(key);
|
|
28
|
+
if ((0, prelude_1.isUndefined)(value) || (0, prelude_1.isUndefined)(metadata))
|
|
29
|
+
return [extra_memoize_1.State.Miss, undefined];
|
|
30
|
+
const elapsed = Date.now() - metadata.updatedAt;
|
|
31
|
+
if (elapsed <= this.timeToLive) {
|
|
32
|
+
return [extra_memoize_1.State.Hit, this.fromBuffer(value)];
|
|
33
|
+
}
|
|
34
|
+
else if (elapsed <= this.timeToLive + this.staleIfError) {
|
|
35
|
+
return [extra_memoize_1.State.StaleIfError, this.fromBuffer(value)];
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return [extra_memoize_1.State.Miss, undefined];
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
set(key, value) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
yield this.cache.set(key, this.toBuffer(value), Date.now(), this.timeToLive, this.staleIfError);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.StaleIfErrorAsyncDiskCache = StaleIfErrorAsyncDiskCache;
|
|
49
|
+
//# sourceMappingURL=stale-if-error-async-disk-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stale-if-error-async-disk-cache.js","sourceRoot":"","sources":["../../src/stale-if-error-async-disk-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAA8D;AAE9D,iDAAiD;AACjD,mCAA4D;AAE5D,MAAa,0BAA0B;IACrC,YACU,KAAgB,EAChB,UAAkB,EAClB,YAAoB,EACpB,WAAiC,uBAAe,EAChD,aAAoC,yBAAiB;QAJrD,UAAK,GAAL,KAAK,CAAW;QAChB,eAAU,GAAV,UAAU,CAAQ;QAClB,iBAAY,GAAZ,YAAY,CAAQ;QACpB,aAAQ,GAAR,QAAQ,CAAwC;QAChD,eAAU,GAAV,UAAU,CAA2C;IAC5D,CAAC;IAEE,GAAG,CAAC,GAAW;;YAInB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;YAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;YAC5C,IAAI,IAAA,qBAAW,EAAC,KAAK,CAAC,IAAI,IAAA,qBAAW,EAAC,QAAQ,CAAC;gBAAE,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;YAE/E,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,CAAC,SAAS,CAAA;YAC/C,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,EAAE;gBAC9B,OAAO,CAAC,qBAAK,CAAC,GAAG,EAAE,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;aAC3C;iBAAM,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,YAAY,EAAE;gBACzD,OAAO,CAAC,qBAAK,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;aACpD;iBAAM;gBAEL,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;aAC/B;QACH,CAAC;KAAA;IAEK,GAAG,CAAC,GAAW,EAAE,KAAQ;;YAC7B,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAClB,GAAG,EACH,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EACpB,IAAI,CAAC,GAAG,EAAE,EACV,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,YAAY,CAClB,CAAA;QACH,CAAC;KAAA;CACF;AArCD,gEAqCC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IStaleWhileRevalidateAndStaleIfErrorAsyncCache, State } from 'extra-memoize';
|
|
3
|
+
import { DiskCache } from 'extra-disk-cache';
|
|
4
|
+
export declare class StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache<T> implements IStaleWhileRevalidateAndStaleIfErrorAsyncCache<T> {
|
|
5
|
+
private cache;
|
|
6
|
+
private timeToLive;
|
|
7
|
+
private staleWhileRevalidate;
|
|
8
|
+
private staleIfError;
|
|
9
|
+
private toBuffer;
|
|
10
|
+
private fromBuffer;
|
|
11
|
+
constructor(cache: DiskCache, timeToLive: number, staleWhileRevalidate: number, staleIfError: number, toBuffer?: (value: T) => Buffer, fromBuffer?: (buffer: Buffer) => T);
|
|
12
|
+
get(key: string): Promise<[State.Miss, undefined] | [State.Hit | State.StaleWhileRevalidate | State.StaleIfError, T]>;
|
|
13
|
+
set(key: string, value: T): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache = void 0;
|
|
13
|
+
const extra_memoize_1 = require("extra-memoize");
|
|
14
|
+
const prelude_1 = require("@blackglory/prelude");
|
|
15
|
+
const utils_1 = require("./utils");
|
|
16
|
+
class StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache {
|
|
17
|
+
constructor(cache, timeToLive, staleWhileRevalidate, staleIfError, toBuffer = utils_1.defaultToBuffer, fromBuffer = utils_1.defaultFromBuffer) {
|
|
18
|
+
this.cache = cache;
|
|
19
|
+
this.timeToLive = timeToLive;
|
|
20
|
+
this.staleWhileRevalidate = staleWhileRevalidate;
|
|
21
|
+
this.staleIfError = staleIfError;
|
|
22
|
+
this.toBuffer = toBuffer;
|
|
23
|
+
this.fromBuffer = fromBuffer;
|
|
24
|
+
}
|
|
25
|
+
get(key) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
const value = yield this.cache.getData(key);
|
|
28
|
+
const metadata = this.cache.getMetadata(key);
|
|
29
|
+
if ((0, prelude_1.isUndefined)(value) || (0, prelude_1.isUndefined)(metadata))
|
|
30
|
+
return [extra_memoize_1.State.Miss, undefined];
|
|
31
|
+
const elapsed = Date.now() - metadata.updatedAt;
|
|
32
|
+
if (elapsed <= this.timeToLive) {
|
|
33
|
+
return [extra_memoize_1.State.Hit, this.fromBuffer(value)];
|
|
34
|
+
}
|
|
35
|
+
else if (elapsed <= this.timeToLive + this.staleWhileRevalidate) {
|
|
36
|
+
return [extra_memoize_1.State.StaleWhileRevalidate, this.fromBuffer(value)];
|
|
37
|
+
}
|
|
38
|
+
else if (elapsed <= this.timeToLive + this.staleWhileRevalidate + this.staleIfError) {
|
|
39
|
+
return [extra_memoize_1.State.StaleIfError, this.fromBuffer(value)];
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return [extra_memoize_1.State.Miss, undefined];
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
set(key, value) {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
yield this.cache.set(key, this.toBuffer(value), Date.now(), this.timeToLive, this.staleWhileRevalidate + this.staleIfError);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache = StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache;
|
|
53
|
+
//# sourceMappingURL=stale-while-revalidate-and-stale-if-error-async-disk-cache.js.map
|
|
@@ -0,0 +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":";;;;;;;;;;;;AAAA,iDAAqF;AAErF,iDAAiD;AACjD,mCAA4D;AAE5D,MAAa,iDAAiD;IAC5D,YACU,KAAgB,EAChB,UAAkB,EAClB,oBAA4B,EAC5B,YAAoB,EACpB,WAAiC,uBAAe,EAChD,aAAoC,yBAAiB;QALrD,UAAK,GAAL,KAAK,CAAW;QAChB,eAAU,GAAV,UAAU,CAAQ;QAClB,yBAAoB,GAApB,oBAAoB,CAAQ;QAC5B,iBAAY,GAAZ,YAAY,CAAQ;QACpB,aAAQ,GAAR,QAAQ,CAAwC;QAChD,eAAU,GAAV,UAAU,CAA2C;IAC5D,CAAC;IAEE,GAAG,CAAC,GAAW;;YAInB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;YAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;YAC5C,IAAI,IAAA,qBAAW,EAAC,KAAK,CAAC,IAAI,IAAA,qBAAW,EAAC,QAAQ,CAAC;gBAAE,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;YAE/E,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,CAAC,SAAS,CAAA;YAC/C,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,EAAE;gBAC9B,OAAO,CAAC,qBAAK,CAAC,GAAG,EAAE,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;aAC3C;iBAAM,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,EAAE;gBACjE,OAAO,CAAC,qBAAK,CAAC,oBAAoB,EAAE,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;aAC5D;iBAAM,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,YAAY,EAAE;gBACrF,OAAO,CAAC,qBAAK,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;aACpD;iBAAM;gBAEL,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;aAC/B;QACH,CAAC;KAAA;IAEK,GAAG,CAAC,GAAW,EAAE,KAAQ;;YAC7B,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAClB,GAAG,EACH,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EACpB,IAAI,CAAC,GAAG,EAAE,EACV,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,YAAY,CAC9C,CAAA;QACH,CAAC;KAAA;CACF;AAxCD,8GAwCC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IStaleWhileRevalidateAsyncCache } from 'extra-memoize';
|
|
3
|
+
import { DiskCache } from 'extra-disk-cache';
|
|
4
|
+
export declare class StaleWhileRevalidateAsyncDiskCache<T> implements IStaleWhileRevalidateAsyncCache<T> {
|
|
5
|
+
private cache;
|
|
6
|
+
private timeToLive;
|
|
7
|
+
private staleWhileRevalidate;
|
|
8
|
+
private toBuffer;
|
|
9
|
+
private fromBuffer;
|
|
10
|
+
constructor(cache: DiskCache, timeToLive: number, staleWhileRevalidate: number, toBuffer?: (value: T) => Buffer, fromBuffer?: (buffer: Buffer) => T);
|
|
11
|
+
get(key: string): Promise<T | undefined>;
|
|
12
|
+
set(key: string, value: T): Promise<void>;
|
|
13
|
+
isStaleWhileRevalidate(key: string): Promise<boolean>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.StaleWhileRevalidateAsyncDiskCache = void 0;
|
|
13
|
+
const prelude_1 = require("@blackglory/prelude");
|
|
14
|
+
const utils_1 = require("./utils");
|
|
15
|
+
class StaleWhileRevalidateAsyncDiskCache {
|
|
16
|
+
constructor(cache, timeToLive, staleWhileRevalidate, toBuffer = utils_1.defaultToBuffer, fromBuffer = utils_1.defaultFromBuffer) {
|
|
17
|
+
this.cache = cache;
|
|
18
|
+
this.timeToLive = timeToLive;
|
|
19
|
+
this.staleWhileRevalidate = staleWhileRevalidate;
|
|
20
|
+
this.toBuffer = toBuffer;
|
|
21
|
+
this.fromBuffer = fromBuffer;
|
|
22
|
+
}
|
|
23
|
+
get(key) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const value = yield this.cache.getData(key);
|
|
26
|
+
if ((0, prelude_1.isUndefined)(value))
|
|
27
|
+
return undefined;
|
|
28
|
+
return this.fromBuffer(value);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
set(key, value) {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
yield this.cache.set(key, this.toBuffer(value), Date.now(), this.timeToLive, this.staleWhileRevalidate);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
isStaleWhileRevalidate(key) {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const metadata = this.cache.getMetadata(key);
|
|
39
|
+
if ((0, prelude_1.isUndefined)(metadata))
|
|
40
|
+
return false;
|
|
41
|
+
const elapsed = Date.now() - metadata.updatedAt;
|
|
42
|
+
return elapsed > this.timeToLive
|
|
43
|
+
&& elapsed < this.timeToLive + this.staleWhileRevalidate;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.StaleWhileRevalidateAsyncDiskCache = StaleWhileRevalidateAsyncDiskCache;
|
|
48
|
+
//# sourceMappingURL=stale-while-revalidate-async-disk-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stale-while-revalidate-async-disk-cache.js","sourceRoot":"","sources":["../../src/stale-while-revalidate-async-disk-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,iDAAiD;AACjD,mCAA4D;AAE5D,MAAa,kCAAkC;IAC7C,YACU,KAAgB,EAChB,UAAkB,EAClB,oBAA4B,EAC5B,WAAiC,uBAAe,EAChD,aAAoC,yBAAiB;QAJrD,UAAK,GAAL,KAAK,CAAW;QAChB,eAAU,GAAV,UAAU,CAAQ;QAClB,yBAAoB,GAApB,oBAAoB,CAAQ;QAC5B,aAAQ,GAAR,QAAQ,CAAwC;QAChD,eAAU,GAAV,UAAU,CAA2C;IAC5D,CAAC;IAEE,GAAG,CAAC,GAAW;;YACnB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;YAC3C,IAAI,IAAA,qBAAW,EAAC,KAAK,CAAC;gBAAE,OAAO,SAAS,CAAA;YAExC,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;QAC/B,CAAC;KAAA;IAEK,GAAG,CAAC,GAAW,EAAE,KAAQ;;YAC7B,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAClB,GAAG,EACH,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EACpB,IAAI,CAAC,GAAG,EAAE,EACV,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,oBAAoB,CAC1B,CAAA;QACH,CAAC;KAAA;IAEK,sBAAsB,CAAC,GAAW;;YACtC,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;YAC5C,IAAI,IAAA,qBAAW,EAAC,QAAQ,CAAC;gBAAE,OAAO,KAAK,CAAA;YAEvC,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,CAAC,SAAS,CAAA;YAC/C,OAAO,OAAO,GAAG,IAAI,CAAC,UAAU;mBACzB,OAAO,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,CAAA;QAC9D,CAAC;KAAA;CACF;AAlCD,gFAkCC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultFromBuffer = exports.defaultToBuffer = void 0;
|
|
4
|
+
function defaultToBuffer(value) {
|
|
5
|
+
return Buffer.from(JSON.stringify(value));
|
|
6
|
+
}
|
|
7
|
+
exports.defaultToBuffer = defaultToBuffer;
|
|
8
|
+
function defaultFromBuffer(buffer) {
|
|
9
|
+
return JSON.parse(buffer.toString());
|
|
10
|
+
}
|
|
11
|
+
exports.defaultFromBuffer = defaultFromBuffer;
|
|
12
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAAA,SAAgB,eAAe,CAAI,KAAQ;IACzC,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAA;AAC3C,CAAC;AAFD,0CAEC;AAED,SAAgB,iBAAiB,CAAI,MAAc;IACjD,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAA;AACtC,CAAC;AAFD,8CAEC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IAsyncCache } from 'extra-memoize';
|
|
3
|
+
import { DiskCache } from 'extra-disk-cache';
|
|
4
|
+
export declare class AsyncDiskCache<T> implements IAsyncCache<T> {
|
|
5
|
+
private cache;
|
|
6
|
+
private toBuffer;
|
|
7
|
+
private fromBuffer;
|
|
8
|
+
constructor(cache: DiskCache, toBuffer?: (value: T) => Buffer, fromBuffer?: (buffer: Buffer) => T);
|
|
9
|
+
get(key: string): Promise<T | undefined>;
|
|
10
|
+
set(key: string, value: T): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AsyncDiskCache = void 0;
|
|
4
|
+
const prelude_1 = require("@blackglory/prelude");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
class AsyncDiskCache {
|
|
7
|
+
constructor(cache, toBuffer = utils_1.defaultToBuffer, fromBuffer = utils_1.defaultFromBuffer) {
|
|
8
|
+
this.cache = cache;
|
|
9
|
+
this.toBuffer = toBuffer;
|
|
10
|
+
this.fromBuffer = fromBuffer;
|
|
11
|
+
}
|
|
12
|
+
async get(key) {
|
|
13
|
+
const value = await this.cache.getData(key);
|
|
14
|
+
if ((0, prelude_1.isUndefined)(value))
|
|
15
|
+
return undefined;
|
|
16
|
+
return this.fromBuffer(value);
|
|
17
|
+
}
|
|
18
|
+
async set(key, value) {
|
|
19
|
+
await this.cache.setData(key, this.toBuffer(value));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.AsyncDiskCache = AsyncDiskCache;
|
|
23
|
+
//# sourceMappingURL=async-disk-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-disk-cache.js","sourceRoot":"","sources":["../../src/async-disk-cache.ts"],"names":[],"mappings":";;;AAEA,iDAAiD;AACjD,mCAA4D;AAE5D,MAAa,cAAc;IACzB,YACU,KAAgB,EAChB,WAAiC,uBAAe,EAChD,aAAoC,yBAAiB;QAFrD,UAAK,GAAL,KAAK,CAAW;QAChB,aAAQ,GAAR,QAAQ,CAAwC;QAChD,eAAU,GAAV,UAAU,CAA2C;IAC5D,CAAC;IAEJ,KAAK,CAAC,GAAG,CAAC,GAAW;QACnB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,IAAA,qBAAW,EAAC,KAAK,CAAC;YAAE,OAAO,SAAS,CAAA;QAExC,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;IAC/B,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,KAAQ;QAC7B,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;IACrD,CAAC;CACF;AAjBD,wCAiBC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
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("./async-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);
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,4EAAyD;AACzD,oEAAiD;AACjD,+FAA4E"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IStaleIfErrorAsyncCache, State } from 'extra-memoize';
|
|
3
|
+
import { DiskCache } from 'extra-disk-cache';
|
|
4
|
+
export declare class StaleIfErrorAsyncDiskCache<T> implements IStaleIfErrorAsyncCache<T> {
|
|
5
|
+
private cache;
|
|
6
|
+
private timeToLive;
|
|
7
|
+
private staleIfError;
|
|
8
|
+
private toBuffer;
|
|
9
|
+
private fromBuffer;
|
|
10
|
+
constructor(cache: DiskCache, timeToLive: number, staleIfError: number, toBuffer?: (value: T) => Buffer, fromBuffer?: (buffer: Buffer) => T);
|
|
11
|
+
get(key: string): Promise<[State.Miss, undefined] | [State.Hit | State.StaleIfError, T]>;
|
|
12
|
+
set(key: string, value: T): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StaleIfErrorAsyncDiskCache = void 0;
|
|
4
|
+
const extra_memoize_1 = require("extra-memoize");
|
|
5
|
+
const prelude_1 = require("@blackglory/prelude");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
class StaleIfErrorAsyncDiskCache {
|
|
8
|
+
constructor(cache, timeToLive, staleIfError, toBuffer = utils_1.defaultToBuffer, fromBuffer = utils_1.defaultFromBuffer) {
|
|
9
|
+
this.cache = cache;
|
|
10
|
+
this.timeToLive = timeToLive;
|
|
11
|
+
this.staleIfError = staleIfError;
|
|
12
|
+
this.toBuffer = toBuffer;
|
|
13
|
+
this.fromBuffer = fromBuffer;
|
|
14
|
+
}
|
|
15
|
+
async get(key) {
|
|
16
|
+
const value = await this.cache.getData(key);
|
|
17
|
+
const metadata = this.cache.getMetadata(key);
|
|
18
|
+
if ((0, prelude_1.isUndefined)(value) || (0, prelude_1.isUndefined)(metadata))
|
|
19
|
+
return [extra_memoize_1.State.Miss, undefined];
|
|
20
|
+
const elapsed = Date.now() - metadata.updatedAt;
|
|
21
|
+
if (elapsed <= this.timeToLive) {
|
|
22
|
+
return [extra_memoize_1.State.Hit, this.fromBuffer(value)];
|
|
23
|
+
}
|
|
24
|
+
else if (elapsed <= this.timeToLive + this.staleIfError) {
|
|
25
|
+
return [extra_memoize_1.State.StaleIfError, this.fromBuffer(value)];
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
return [extra_memoize_1.State.Miss, undefined];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async set(key, value) {
|
|
32
|
+
await this.cache.set(key, this.toBuffer(value), Date.now(), this.timeToLive, this.staleIfError);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.StaleIfErrorAsyncDiskCache = StaleIfErrorAsyncDiskCache;
|
|
36
|
+
//# sourceMappingURL=stale-if-error-async-disk-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stale-if-error-async-disk-cache.js","sourceRoot":"","sources":["../../src/stale-if-error-async-disk-cache.ts"],"names":[],"mappings":";;;AAAA,iDAA8D;AAE9D,iDAAiD;AACjD,mCAA4D;AAE5D,MAAa,0BAA0B;IACrC,YACU,KAAgB,EAChB,UAAkB,EAClB,YAAoB,EACpB,WAAiC,uBAAe,EAChD,aAAoC,yBAAiB;QAJrD,UAAK,GAAL,KAAK,CAAW;QAChB,eAAU,GAAV,UAAU,CAAQ;QAClB,iBAAY,GAAZ,YAAY,CAAQ;QACpB,aAAQ,GAAR,QAAQ,CAAwC;QAChD,eAAU,GAAV,UAAU,CAA2C;IAC5D,CAAC;IAEJ,KAAK,CAAC,GAAG,CAAC,GAAW;QAInB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;QAC5C,IAAI,IAAA,qBAAW,EAAC,KAAK,CAAC,IAAI,IAAA,qBAAW,EAAC,QAAQ,CAAC;YAAE,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;QAE/E,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,CAAC,SAAS,CAAA;QAC/C,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,EAAE;YAC9B,OAAO,CAAC,qBAAK,CAAC,GAAG,EAAE,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;SAC3C;aAAM,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,YAAY,EAAE;YACzD,OAAO,CAAC,qBAAK,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;SACpD;aAAM;YAEL,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;SAC/B;IACH,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,KAAQ;QAC7B,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAClB,GAAG,EACH,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EACpB,IAAI,CAAC,GAAG,EAAE,EACV,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,YAAY,CAClB,CAAA;IACH,CAAC;CACF;AArCD,gEAqCC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IStaleWhileRevalidateAndStaleIfErrorAsyncCache, State } from 'extra-memoize';
|
|
3
|
+
import { DiskCache } from 'extra-disk-cache';
|
|
4
|
+
export declare class StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache<T> implements IStaleWhileRevalidateAndStaleIfErrorAsyncCache<T> {
|
|
5
|
+
private cache;
|
|
6
|
+
private timeToLive;
|
|
7
|
+
private staleWhileRevalidate;
|
|
8
|
+
private staleIfError;
|
|
9
|
+
private toBuffer;
|
|
10
|
+
private fromBuffer;
|
|
11
|
+
constructor(cache: DiskCache, timeToLive: number, staleWhileRevalidate: number, staleIfError: number, toBuffer?: (value: T) => Buffer, fromBuffer?: (buffer: Buffer) => T);
|
|
12
|
+
get(key: string): Promise<[State.Miss, undefined] | [State.Hit | State.StaleWhileRevalidate | State.StaleIfError, T]>;
|
|
13
|
+
set(key: string, value: T): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache = void 0;
|
|
4
|
+
const extra_memoize_1 = require("extra-memoize");
|
|
5
|
+
const prelude_1 = require("@blackglory/prelude");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
class StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache {
|
|
8
|
+
constructor(cache, timeToLive, staleWhileRevalidate, staleIfError, toBuffer = utils_1.defaultToBuffer, fromBuffer = utils_1.defaultFromBuffer) {
|
|
9
|
+
this.cache = cache;
|
|
10
|
+
this.timeToLive = timeToLive;
|
|
11
|
+
this.staleWhileRevalidate = staleWhileRevalidate;
|
|
12
|
+
this.staleIfError = staleIfError;
|
|
13
|
+
this.toBuffer = toBuffer;
|
|
14
|
+
this.fromBuffer = fromBuffer;
|
|
15
|
+
}
|
|
16
|
+
async get(key) {
|
|
17
|
+
const value = await this.cache.getData(key);
|
|
18
|
+
const metadata = this.cache.getMetadata(key);
|
|
19
|
+
if ((0, prelude_1.isUndefined)(value) || (0, prelude_1.isUndefined)(metadata))
|
|
20
|
+
return [extra_memoize_1.State.Miss, undefined];
|
|
21
|
+
const elapsed = Date.now() - metadata.updatedAt;
|
|
22
|
+
if (elapsed <= this.timeToLive) {
|
|
23
|
+
return [extra_memoize_1.State.Hit, this.fromBuffer(value)];
|
|
24
|
+
}
|
|
25
|
+
else if (elapsed <= this.timeToLive + this.staleWhileRevalidate) {
|
|
26
|
+
return [extra_memoize_1.State.StaleWhileRevalidate, this.fromBuffer(value)];
|
|
27
|
+
}
|
|
28
|
+
else if (elapsed <= this.timeToLive + this.staleWhileRevalidate + this.staleIfError) {
|
|
29
|
+
return [extra_memoize_1.State.StaleIfError, this.fromBuffer(value)];
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
return [extra_memoize_1.State.Miss, undefined];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async set(key, value) {
|
|
36
|
+
await this.cache.set(key, this.toBuffer(value), Date.now(), this.timeToLive, this.staleWhileRevalidate + this.staleIfError);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache = StaleWhileRevalidateAndStaleIfErrorAsyncDiskCache;
|
|
40
|
+
//# sourceMappingURL=stale-while-revalidate-and-stale-if-error-async-disk-cache.js.map
|
|
@@ -0,0 +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":";;;AAAA,iDAAqF;AAErF,iDAAiD;AACjD,mCAA4D;AAE5D,MAAa,iDAAiD;IAC5D,YACU,KAAgB,EAChB,UAAkB,EAClB,oBAA4B,EAC5B,YAAoB,EACpB,WAAiC,uBAAe,EAChD,aAAoC,yBAAiB;QALrD,UAAK,GAAL,KAAK,CAAW;QAChB,eAAU,GAAV,UAAU,CAAQ;QAClB,yBAAoB,GAApB,oBAAoB,CAAQ;QAC5B,iBAAY,GAAZ,YAAY,CAAQ;QACpB,aAAQ,GAAR,QAAQ,CAAwC;QAChD,eAAU,GAAV,UAAU,CAA2C;IAC5D,CAAC;IAEJ,KAAK,CAAC,GAAG,CAAC,GAAW;QAInB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;QAC5C,IAAI,IAAA,qBAAW,EAAC,KAAK,CAAC,IAAI,IAAA,qBAAW,EAAC,QAAQ,CAAC;YAAE,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;QAE/E,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,CAAC,SAAS,CAAA;QAC/C,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,EAAE;YAC9B,OAAO,CAAC,qBAAK,CAAC,GAAG,EAAE,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;SAC3C;aAAM,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,EAAE;YACjE,OAAO,CAAC,qBAAK,CAAC,oBAAoB,EAAE,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;SAC5D;aAAM,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,YAAY,EAAE;YACrF,OAAO,CAAC,qBAAK,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;SACpD;aAAM;YAEL,OAAO,CAAC,qBAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;SAC/B;IACH,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,KAAQ;QAC7B,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAClB,GAAG,EACH,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EACpB,IAAI,CAAC,GAAG,EAAE,EACV,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,YAAY,CAC9C,CAAA;IACH,CAAC;CACF;AAxCD,8GAwCC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IStaleWhileRevalidateAsyncCache } from 'extra-memoize';
|
|
3
|
+
import { DiskCache } from 'extra-disk-cache';
|
|
4
|
+
export declare class StaleWhileRevalidateAsyncDiskCache<T> implements IStaleWhileRevalidateAsyncCache<T> {
|
|
5
|
+
private cache;
|
|
6
|
+
private timeToLive;
|
|
7
|
+
private staleWhileRevalidate;
|
|
8
|
+
private toBuffer;
|
|
9
|
+
private fromBuffer;
|
|
10
|
+
constructor(cache: DiskCache, timeToLive: number, staleWhileRevalidate: number, toBuffer?: (value: T) => Buffer, fromBuffer?: (buffer: Buffer) => T);
|
|
11
|
+
get(key: string): Promise<T | undefined>;
|
|
12
|
+
set(key: string, value: T): Promise<void>;
|
|
13
|
+
isStaleWhileRevalidate(key: string): Promise<boolean>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StaleWhileRevalidateAsyncDiskCache = void 0;
|
|
4
|
+
const prelude_1 = require("@blackglory/prelude");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
class StaleWhileRevalidateAsyncDiskCache {
|
|
7
|
+
constructor(cache, timeToLive, staleWhileRevalidate, toBuffer = utils_1.defaultToBuffer, fromBuffer = utils_1.defaultFromBuffer) {
|
|
8
|
+
this.cache = cache;
|
|
9
|
+
this.timeToLive = timeToLive;
|
|
10
|
+
this.staleWhileRevalidate = staleWhileRevalidate;
|
|
11
|
+
this.toBuffer = toBuffer;
|
|
12
|
+
this.fromBuffer = fromBuffer;
|
|
13
|
+
}
|
|
14
|
+
async get(key) {
|
|
15
|
+
const value = await this.cache.getData(key);
|
|
16
|
+
if ((0, prelude_1.isUndefined)(value))
|
|
17
|
+
return undefined;
|
|
18
|
+
return this.fromBuffer(value);
|
|
19
|
+
}
|
|
20
|
+
async set(key, value) {
|
|
21
|
+
await this.cache.set(key, this.toBuffer(value), Date.now(), this.timeToLive, this.staleWhileRevalidate);
|
|
22
|
+
}
|
|
23
|
+
async isStaleWhileRevalidate(key) {
|
|
24
|
+
const metadata = this.cache.getMetadata(key);
|
|
25
|
+
if ((0, prelude_1.isUndefined)(metadata))
|
|
26
|
+
return false;
|
|
27
|
+
const elapsed = Date.now() - metadata.updatedAt;
|
|
28
|
+
return elapsed > this.timeToLive
|
|
29
|
+
&& elapsed < this.timeToLive + this.staleWhileRevalidate;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.StaleWhileRevalidateAsyncDiskCache = StaleWhileRevalidateAsyncDiskCache;
|
|
33
|
+
//# sourceMappingURL=stale-while-revalidate-async-disk-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stale-while-revalidate-async-disk-cache.js","sourceRoot":"","sources":["../../src/stale-while-revalidate-async-disk-cache.ts"],"names":[],"mappings":";;;AAEA,iDAAiD;AACjD,mCAA4D;AAE5D,MAAa,kCAAkC;IAC7C,YACU,KAAgB,EAChB,UAAkB,EAClB,oBAA4B,EAC5B,WAAiC,uBAAe,EAChD,aAAoC,yBAAiB;QAJrD,UAAK,GAAL,KAAK,CAAW;QAChB,eAAU,GAAV,UAAU,CAAQ;QAClB,yBAAoB,GAApB,oBAAoB,CAAQ;QAC5B,aAAQ,GAAR,QAAQ,CAAwC;QAChD,eAAU,GAAV,UAAU,CAA2C;IAC5D,CAAC;IAEJ,KAAK,CAAC,GAAG,CAAC,GAAW;QACnB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QAC3C,IAAI,IAAA,qBAAW,EAAC,KAAK,CAAC;YAAE,OAAO,SAAS,CAAA;QAExC,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;IAC/B,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,KAAQ;QAC7B,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAClB,GAAG,EACH,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EACpB,IAAI,CAAC,GAAG,EAAE,EACV,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,oBAAoB,CAC1B,CAAA;IACH,CAAC;IAED,KAAK,CAAC,sBAAsB,CAAC,GAAW;QACtC,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;QAC5C,IAAI,IAAA,qBAAW,EAAC,QAAQ,CAAC;YAAE,OAAO,KAAK,CAAA;QAEvC,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,CAAC,SAAS,CAAA;QAC/C,OAAO,OAAO,GAAG,IAAI,CAAC,UAAU;eACzB,OAAO,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,CAAA;IAC9D,CAAC;CACF;AAlCD,gFAkCC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultFromBuffer = exports.defaultToBuffer = void 0;
|
|
4
|
+
function defaultToBuffer(value) {
|
|
5
|
+
return Buffer.from(JSON.stringify(value));
|
|
6
|
+
}
|
|
7
|
+
exports.defaultToBuffer = defaultToBuffer;
|
|
8
|
+
function defaultFromBuffer(buffer) {
|
|
9
|
+
return JSON.parse(buffer.toString());
|
|
10
|
+
}
|
|
11
|
+
exports.defaultFromBuffer = defaultFromBuffer;
|
|
12
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAAA,SAAgB,eAAe,CAAI,KAAQ;IACzC,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAA;AAC3C,CAAC;AAFD,0CAEC;AAED,SAAgB,iBAAiB,CAAI,MAAc;IACjD,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAA;AACtC,CAAC;AAFD,8CAEC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@extra-memoize/extra-disk-cache",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"files": [
|
|
6
|
+
"lib"
|
|
7
|
+
],
|
|
8
|
+
"main": "lib/es2018/index.js",
|
|
9
|
+
"types": "lib/es2018/index.d.ts",
|
|
10
|
+
"sideEffects": false,
|
|
11
|
+
"repository": "git@github.com:extra-memoize/extra-disk-cache.git",
|
|
12
|
+
"author": "BlackGlory <woshenmedoubuzhidao@blackglory.me>",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"lint": "eslint --ext .js,.jsx,.ts,.tsx --quiet src",
|
|
16
|
+
"test": "jest --runInBand --config jest.config.js",
|
|
17
|
+
"test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand",
|
|
18
|
+
"test:coverage": "jest --coverage --config jest.config.js",
|
|
19
|
+
"prepublishOnly": "run-s clean build",
|
|
20
|
+
"clean": "run-p clean:*",
|
|
21
|
+
"clean:build": "rimraf lib",
|
|
22
|
+
"build": "run-p build:*",
|
|
23
|
+
"build:es2015": "run-s build:es2015:*",
|
|
24
|
+
"build:es2015:compile": "tsc --project tsconfig.build.json --module commonjs --target es2015 --outDir lib/es2015",
|
|
25
|
+
"build:es2015:patch": "tscpaths -p tsconfig.build.json -s ./src -o ./lib/es2015",
|
|
26
|
+
"build:es2018": "run-s build:es2018:*",
|
|
27
|
+
"build:es2018:compile": "tsc --project tsconfig.build.json --module commonjs --target es2018 --outDir lib/es2018",
|
|
28
|
+
"build:es2018:patch": "tscpaths -p tsconfig.build.json -s ./src -o ./lib/es2018",
|
|
29
|
+
"release": "standard-version"
|
|
30
|
+
},
|
|
31
|
+
"husky": {
|
|
32
|
+
"hooks": {
|
|
33
|
+
"pre-commit": "run-s lint build",
|
|
34
|
+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@blackglory/jest-matchers": "^0.3.1",
|
|
39
|
+
"@commitlint/cli": "^16.2.3",
|
|
40
|
+
"@commitlint/config-conventional": "^16.2.1",
|
|
41
|
+
"@types/jest": "^27.4.1",
|
|
42
|
+
"@typescript-eslint/eslint-plugin": "^5.18.0",
|
|
43
|
+
"@typescript-eslint/parser": "^5.18.0",
|
|
44
|
+
"eslint": "^8.12.0",
|
|
45
|
+
"extra-disk-cache": "^0.4.3",
|
|
46
|
+
"extra-memoize": "^0.4.8",
|
|
47
|
+
"husky": "^4.3.8",
|
|
48
|
+
"jest": "^27.5.1",
|
|
49
|
+
"npm-run-all": "^4.1.5",
|
|
50
|
+
"return-style": "^1.0.0",
|
|
51
|
+
"rimraf": "^3.0.2",
|
|
52
|
+
"standard-version": "^9.3.2",
|
|
53
|
+
"ts-jest": "^27.1.4",
|
|
54
|
+
"tscpaths": "^0.0.9",
|
|
55
|
+
"tslib": "^2.3.1",
|
|
56
|
+
"typescript": "^4.6.3"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@blackglory/prelude": "^0.1.0"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"extra-disk-cache": "^0.4.3",
|
|
63
|
+
"extra-memoize": "^0.4.8"
|
|
64
|
+
}
|
|
65
|
+
}
|