simple-in-memory-cache 0.4.0 → 0.4.3
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/dist/cache.d.ts +4 -4
- package/dist/cache.js +5 -3
- package/dist/cache.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js.map +1 -1
- package/license.md +21 -0
- package/package.json +65 -43
- package/readme.md +71 -11
- package/dist/cache.test.d.ts +0 -1
- package/dist/cache.test.js +0 -96
- package/dist/cache.test.js.map +0 -1
package/dist/cache.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type IsoDuration } from 'iso-time';
|
|
2
2
|
export interface SimpleInMemoryCache<T> {
|
|
3
3
|
get: (key: string) => T | undefined;
|
|
4
4
|
set: (key: string, value: T, options?: {
|
|
5
|
-
expiration?:
|
|
5
|
+
expiration?: IsoDuration | null;
|
|
6
6
|
}) => void;
|
|
7
7
|
keys: () => string[];
|
|
8
8
|
}
|
|
@@ -12,6 +12,6 @@ export interface SimpleInMemoryCacheState<T> {
|
|
|
12
12
|
expiresAtMse: number;
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
export declare const createCache: <T>({ expiration: defaultExpiration
|
|
16
|
-
expiration?:
|
|
15
|
+
export declare const createCache: <T>({ expiration: defaultExpiration }?: {
|
|
16
|
+
expiration?: IsoDuration | null;
|
|
17
17
|
}) => SimpleInMemoryCache<T>;
|
package/dist/cache.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createCache = void 0;
|
|
4
|
-
const
|
|
4
|
+
const iso_time_1 = require("iso-time");
|
|
5
5
|
const getMseNow = () => new Date().getTime();
|
|
6
|
-
const createCache = ({ expiration: defaultExpiration
|
|
6
|
+
const createCache = ({ expiration: defaultExpiration } = {
|
|
7
|
+
expiration: { minutes: 5 },
|
|
8
|
+
}) => {
|
|
7
9
|
// initialize a fresh in-memory cache object
|
|
8
10
|
const cache = {};
|
|
9
11
|
// define how to set an item into the cache
|
|
@@ -14,7 +16,7 @@ const createCache = ({ expiration: defaultExpiration, } = { expiration: { minute
|
|
|
14
16
|
return;
|
|
15
17
|
}
|
|
16
18
|
// handle setting
|
|
17
|
-
const expiresAtMse = getMseNow() + (expiration ? (0,
|
|
19
|
+
const expiresAtMse = getMseNow() + (expiration ? (0, iso_time_1.toMilliseconds)(expiration) : Infinity); // infinity if null
|
|
18
20
|
cache[key] = { value, expiresAtMse };
|
|
19
21
|
};
|
|
20
22
|
// define how to get an item from the cache
|
package/dist/cache.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":";;;AAAA,uCAA4D;AAgB5D,MAAM,SAAS,GAAG,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;AAEtC,MAAM,WAAW,GAAG,CACzB,EAAE,UAAU,EAAE,iBAAiB,KAA0C;IACvE,UAAU,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE;CAC3B,EACuB,EAAE;IAC1B,4CAA4C;IAC5C,MAAM,KAAK,GAAgC,EAAE,CAAC;IAE9C,2CAA2C;IAC3C,MAAM,GAAG,GAAG,CACV,GAAW,EACX,KAAoB,EACpB,EACE,UAAU,GAAG,iBAAiB,MACS,EAAE,EAC3C,EAAE;QACF,4BAA4B;QAC5B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;YAClB,OAAO;QACT,CAAC;QAED,iBAAiB;QACjB,MAAM,YAAY,GAChB,SAAS,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,IAAA,yBAAc,EAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,mBAAmB;QACzF,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;IACvC,CAAC,CAAC;IAEF,2CAA2C;IAC3C,MAAM,GAAG,GAAG,CAAC,GAAW,EAAE,EAAE;QAC1B,MAAM,YAAY,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,CAAC,YAAY;YAAE,OAAO,SAAS,CAAC,CAAC,kCAAkC;QACvE,IAAI,YAAY,CAAC,YAAY,IAAI,SAAS,EAAE;YAAE,OAAO,SAAS,CAAC,CAAC,qCAAqC;QACrG,OAAO,YAAY,CAAC,KAAK,CAAC,CAAC,mEAAmE;IAChG,CAAC,CAAC;IAEF,oCAAoC;IACpC,MAAM,IAAI,GAAG,GAAG,EAAE,CAChB,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC;SAClB,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,YAAY,GAAG,SAAS,EAAE,CAAC;SACxD,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC;IAEzB,iBAAiB;IACjB,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC;AAC5B,CAAC,CAAC;AA5CW,QAAA,WAAW,eA4CtB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export type { SimpleInMemoryCache } from './cache';
|
|
2
|
+
export { createCache } from './cache';
|
package/dist/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":";;;AACA,iCAAsC;AAA7B,oGAAA,WAAW,OAAA"}
|
package/license.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 ehmpathy
|
|
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/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "simple-in-memory-cache",
|
|
3
3
|
"author": "ehmpathy",
|
|
4
4
|
"description": "A simple in-memory cache, for nodejs and the browser, with time based expiration policies.",
|
|
5
|
-
"version": "0.4.
|
|
5
|
+
"version": "0.4.3",
|
|
6
6
|
"repository": "ehmpathy/simple-in-memory-cache",
|
|
7
7
|
"homepage": "https://github.com/ehmpathy/simple-in-memory-cache",
|
|
8
8
|
"keywords": [
|
|
@@ -31,63 +31,85 @@
|
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build:ts": "tsc -p ./tsconfig.build.json",
|
|
33
33
|
"commit:with-cli": "npx cz",
|
|
34
|
-
"fix:format:
|
|
35
|
-
"fix:format": "npm run fix:format:
|
|
36
|
-
"fix:lint": "
|
|
37
|
-
"
|
|
38
|
-
"build:
|
|
39
|
-
"build": "
|
|
34
|
+
"fix:format:biome": "biome check --write",
|
|
35
|
+
"fix:format": "npm run fix:format:biome",
|
|
36
|
+
"fix:lint": "biome check --write",
|
|
37
|
+
"fix": "npm run fix:format && npm run fix:lint",
|
|
38
|
+
"build:clean:bun": "rm -f ./bin/*.bc",
|
|
39
|
+
"build:clean:tsc": "(chmod -R u+w dist 2>/dev/null || true) && rm -rf dist/",
|
|
40
|
+
"build:clean": "npm run build:clean:tsc && npm run build:clean:bun",
|
|
41
|
+
"build:compile:tsc": "tsc -p ./tsconfig.build.json && tsc-alias -p ./tsconfig.build.json",
|
|
42
|
+
"build:compile": "npm run build:compile:tsc && npm run build:compile:bun --if-present",
|
|
43
|
+
"build": "npm run build:clean && npm run build:compile && npm run build:complete --if-present",
|
|
44
|
+
"test:auth": "[ \"$ECHO\" = 'true' ] && echo 'source .agent/repo=.this/role=any/skills/use.apikeys.sh' || bash -c 'source .agent/repo=.this/role=any/skills/use.apikeys.sh'",
|
|
40
45
|
"test:commits": "LAST_TAG=$(git describe --tags --abbrev=0 @^ 2> /dev/null || git rev-list --max-parents=0 HEAD) && npx commitlint --from $LAST_TAG --to HEAD --verbose",
|
|
41
|
-
"test:types": "tsc -p ./tsconfig.
|
|
42
|
-
"test:format:
|
|
43
|
-
"test:format": "npm run test:format:
|
|
44
|
-
"test:lint:deps": "npx depcheck -c
|
|
45
|
-
"test:lint:
|
|
46
|
-
"test:lint": "
|
|
47
|
-
"test:
|
|
48
|
-
"test:
|
|
49
|
-
"test:
|
|
50
|
-
"test": "
|
|
51
|
-
"test
|
|
46
|
+
"test:types": "tsc -p ./tsconfig.json --noEmit",
|
|
47
|
+
"test:format:biome": "biome format",
|
|
48
|
+
"test:format": "npm run test:format:biome",
|
|
49
|
+
"test:lint:deps": "npx depcheck -c ./.depcheckrc.yml",
|
|
50
|
+
"test:lint:biome": "biome check --diagnostic-level=error",
|
|
51
|
+
"test:lint:biome:all": "biome check",
|
|
52
|
+
"test:lint": "npm run test:lint:biome && npm run test:lint:cycles && npm run test:lint:deps",
|
|
53
|
+
"test:unit": "set -eu && jest -c ./jest.unit.config.ts --forceExit --verbose --passWithNoTests $([ -n \"${CI:-}\" ] && echo '--ci') $([ \"${THOROUGH:-}\" != \"true\" ] && echo '--changedSince=main') $([ \"${RESNAP:-}\" = \"true\" ] && echo '--updateSnapshot')",
|
|
54
|
+
"test:integration": "set -eu && jest -c ./jest.integration.config.ts --forceExit --verbose --passWithNoTests $([ -n \"${CI:-}\" ] && echo '--ci') $([ \"${THOROUGH:-}\" != \"true\" ] && echo '--changedSince=main') $([ \"${RESNAP:-}\" = \"true\" ] && echo '--updateSnapshot')",
|
|
55
|
+
"test:acceptance:locally": "set -eu && npm run build && LOCALLY=true jest -c ./jest.acceptance.config.ts --forceExit --verbose --runInBand --passWithNoTests $([ \"${RESNAP:-}\" = \"true\" ] && echo '--updateSnapshot')",
|
|
56
|
+
"test": "set -eu && npm run test:commits && npm run test:types && npm run test:format && npm run test:lint && npm run test:unit && npm run test:integration && npm run test:acceptance:locally",
|
|
57
|
+
"test:acceptance": "set -eu && npm run build && jest -c ./jest.acceptance.config.ts --forceExit --verbose --runInBand --passWithNoTests $([ -n \"${CI:-}\" ] && echo '--ci') $([ \"${RESNAP:-}\" = \"true\" ] && echo '--updateSnapshot')",
|
|
52
58
|
"prepush": "npm run test && npm run build",
|
|
53
59
|
"prepublish": "npm run build",
|
|
54
60
|
"preversion": "npm run prepush",
|
|
55
61
|
"postversion": "git push origin HEAD --tags --no-verify",
|
|
56
|
-
"prepare:husky": "
|
|
57
|
-
"
|
|
62
|
+
"prepare:husky": "husky install && chmod ug+x .husky/*",
|
|
63
|
+
"prepare:rhachet": "rhachet init --hooks --roles mechanic behaver driver architect ergonomist reviewer dreamer dispatcher",
|
|
64
|
+
"prepare": "if [ -e .git ] && [ -z $CI ]; then npm run prepare:husky && npm run prepare:rhachet; fi",
|
|
65
|
+
"test:lint:cycles": "dpdm --no-warning --no-tree --exit-code circular:1 --exclude \"$(yq -r '.exclude | join(\"|\") // \"^$\"' .dpdmrc.yaml)\" 'src/**/*.ts'",
|
|
66
|
+
"upgrade:rhachet": "rhachet upgrade"
|
|
67
|
+
},
|
|
68
|
+
"dependencies": {
|
|
69
|
+
"domain-objects": "0.31.9",
|
|
70
|
+
"helpful-errors": "1.7.3",
|
|
71
|
+
"iso-time": "^1.11.7",
|
|
72
|
+
"type-fns": "1.21.2"
|
|
58
73
|
},
|
|
59
74
|
"devDependencies": {
|
|
60
|
-
"@
|
|
61
|
-
"@commitlint/
|
|
62
|
-
"@
|
|
63
|
-
"@
|
|
64
|
-
"@
|
|
65
|
-
"@
|
|
66
|
-
"@
|
|
75
|
+
"@biomejs/biome": "2.3.8",
|
|
76
|
+
"@commitlint/cli": "19.5.0",
|
|
77
|
+
"@commitlint/config-conventional": "19.5.0",
|
|
78
|
+
"@jest/globals": "30.2.0",
|
|
79
|
+
"@swc/core": "1.15.3",
|
|
80
|
+
"@swc/jest": "0.2.39",
|
|
81
|
+
"@tsconfig/node20": "20.1.5",
|
|
82
|
+
"@tsconfig/strictest": "2.0.5",
|
|
83
|
+
"@types/jest": "30.0.0",
|
|
84
|
+
"@types/node": "22.15.21",
|
|
67
85
|
"commitlint": "^17.6.7",
|
|
68
|
-
"core-js": "3.26.1",
|
|
69
86
|
"cz-conventional-changelog": "3.3.0",
|
|
70
|
-
"declapract": "^0.
|
|
71
|
-
"declapract-typescript-ehmpathy": "^0.
|
|
87
|
+
"declapract": "^0.13.21",
|
|
88
|
+
"declapract-typescript-ehmpathy": "^0.47.74",
|
|
89
|
+
"declastruct": "1.9.1",
|
|
90
|
+
"declastruct-github": "1.4.0",
|
|
72
91
|
"depcheck": "1.4.3",
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"eslint-config-prettier": "8.5.0",
|
|
76
|
-
"eslint-plugin-import": "2.26.0",
|
|
77
|
-
"eslint-plugin-prettier": "4.2.1",
|
|
92
|
+
"dpdm": "4.0.1",
|
|
93
|
+
"esbuild-register": "3.6.0",
|
|
78
94
|
"husky": "8.0.3",
|
|
79
|
-
"jest": "
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
"
|
|
83
|
-
"
|
|
95
|
+
"jest": "30.2.0",
|
|
96
|
+
"rhachet": "1.41.17",
|
|
97
|
+
"rhachet-brains-anthropic": "0.4.1",
|
|
98
|
+
"rhachet-brains-xai": "0.3.3",
|
|
99
|
+
"rhachet-roles-bhrain": "0.28.0",
|
|
100
|
+
"rhachet-roles-bhuild": "0.21.14",
|
|
101
|
+
"rhachet-roles-ehmpathy": "1.35.12",
|
|
102
|
+
"rhachet-roles-rhachet": "^0.1.7",
|
|
103
|
+
"test-fns": "1.15.8",
|
|
104
|
+
"tsc-alias": "1.8.10",
|
|
105
|
+
"tsx": "4.20.6",
|
|
106
|
+
"typescript": "5.4.5",
|
|
107
|
+
"yalc": "1.0.0-pre.53"
|
|
84
108
|
},
|
|
85
109
|
"config": {
|
|
86
110
|
"commitizen": {
|
|
87
111
|
"path": "./node_modules/cz-conventional-changelog"
|
|
88
112
|
}
|
|
89
113
|
},
|
|
90
|
-
"
|
|
91
|
-
"@ehmpathy/uni-time": "^1.7.4"
|
|
92
|
-
}
|
|
114
|
+
"packageManager": "pnpm@10.24.0"
|
|
93
115
|
}
|
package/readme.md
CHANGED
|
@@ -3,36 +3,96 @@
|
|
|
3
3
|

|
|
4
4
|

|
|
5
5
|
|
|
6
|
-
A simple in-memory cache
|
|
6
|
+
A simple, typed, in-memory cache for nodejs and the browser with time-based expiration policies.
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
## install
|
|
9
9
|
|
|
10
10
|
```sh
|
|
11
11
|
npm install --save simple-in-memory-cache
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
## usage
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
### set and get
|
|
17
17
|
|
|
18
18
|
```ts
|
|
19
19
|
import { createCache } from 'simple-in-memory-cache';
|
|
20
20
|
|
|
21
21
|
const { set, get } = createCache();
|
|
22
|
-
set('
|
|
23
|
-
const
|
|
22
|
+
set('purpose of life', 42);
|
|
23
|
+
const purpose = get('purpose of life'); // returns 42
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
### expiration
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
items in the cache expire after 5 minutes by default.
|
|
29
|
+
|
|
30
|
+
change the default expiration on cache creation:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
const { set, get } = createCache({ expiration: { minutes: 10 } });
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
override expiration per item:
|
|
29
37
|
|
|
30
38
|
```ts
|
|
31
|
-
|
|
39
|
+
set('ice cream state', 'solid', { expiration: { seconds: 30 } });
|
|
32
40
|
```
|
|
33
41
|
|
|
34
|
-
|
|
42
|
+
set an item to never expire:
|
|
35
43
|
|
|
36
44
|
```ts
|
|
37
|
-
set('
|
|
45
|
+
set('speed of light', 299792458, { expiration: null });
|
|
38
46
|
```
|
|
47
|
+
|
|
48
|
+
### invalidation
|
|
49
|
+
|
|
50
|
+
invalidate a cached item with `set(key, undefined)`:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
set('purpose of life', 42);
|
|
54
|
+
get('purpose of life'); // returns 42
|
|
55
|
+
|
|
56
|
+
set('purpose of life', undefined);
|
|
57
|
+
get('purpose of life'); // returns undefined
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### keys
|
|
61
|
+
|
|
62
|
+
list all non-expired keys in the cache:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
const { set, keys } = createCache();
|
|
66
|
+
set('a', 1);
|
|
67
|
+
set('b', 2);
|
|
68
|
+
keys(); // returns ['a', 'b']
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## types
|
|
72
|
+
|
|
73
|
+
the cache is fully typed:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { createCache, SimpleInMemoryCache } from 'simple-in-memory-cache';
|
|
77
|
+
|
|
78
|
+
const cache: SimpleInMemoryCache<number> = createCache<number>();
|
|
79
|
+
cache.set('answer', 42);
|
|
80
|
+
const answer: number | undefined = cache.get('answer');
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## api
|
|
84
|
+
|
|
85
|
+
### `createCache<T>(options?)`
|
|
86
|
+
|
|
87
|
+
creates a new cache instance.
|
|
88
|
+
|
|
89
|
+
**options:**
|
|
90
|
+
- `expiration?: IsoDuration | null` — default expiration for items (default: `{ minutes: 5 }`)
|
|
91
|
+
|
|
92
|
+
**returns:** `SimpleInMemoryCache<T>`
|
|
93
|
+
|
|
94
|
+
### `SimpleInMemoryCache<T>`
|
|
95
|
+
|
|
96
|
+
- `get(key: string): T | undefined` — retrieve an item (returns `undefined` if absent or expired)
|
|
97
|
+
- `set(key: string, value: T, options?): void` — store an item (or invalidate with `undefined`)
|
|
98
|
+
- `keys(): string[]` — list all non-expired keys
|
package/dist/cache.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/cache.test.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
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
|
-
const cache_1 = require("./cache");
|
|
13
|
-
jest.setTimeout(30 * 1000); // give up to 60 seconds, since we deal with timeouts that we want to test on the ~15 second range
|
|
14
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
15
|
-
describe('cache', () => {
|
|
16
|
-
it('should be able to add an item to the cache', () => {
|
|
17
|
-
const { set } = (0, cache_1.createCache)();
|
|
18
|
-
set('meaning of life', 42);
|
|
19
|
-
});
|
|
20
|
-
it('should be able to get an item from the cache', () => {
|
|
21
|
-
const { set, get } = (0, cache_1.createCache)();
|
|
22
|
-
set('how many licks does it take to get to the center of a tootsie pop?', 3);
|
|
23
|
-
const licks = get('how many licks does it take to get to the center of a tootsie pop?');
|
|
24
|
-
expect(licks).toEqual(3);
|
|
25
|
-
});
|
|
26
|
-
it('should respect the default expiration for the cache', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
-
const { set, get } = (0, cache_1.createCache)({ expiration: { seconds: 10 } }); // we're gonna use this cache to keep track of the popcorn in the microwave - we should check more regularly since it changes quickly!
|
|
28
|
-
set('how popped is the popcorn?', 'not popped');
|
|
29
|
-
// prove that we recorded the value and its accessible immediately after setting
|
|
30
|
-
const popcornStatus = get('how popped is the popcorn?');
|
|
31
|
-
expect(popcornStatus).toEqual('not popped');
|
|
32
|
-
// prove that the value is still accessible after 9 seconds, since default ttl is 10 seconds
|
|
33
|
-
yield sleep(9 * 1000);
|
|
34
|
-
const popcornStatusAfter9Sec = get('how popped is the popcorn?');
|
|
35
|
-
expect(popcornStatusAfter9Sec).toEqual('not popped'); // still should say not popped
|
|
36
|
-
// and prove that after a total of 9 seconds, the status is no longer in the cache
|
|
37
|
-
yield sleep(1 * 1000); // sleep 1 more second
|
|
38
|
-
const popcornStatusAfter10Sec = get('how popped is the popcorn?');
|
|
39
|
-
expect(popcornStatusAfter10Sec).toEqual(undefined); // no longer defined, since the default seconds until expiration was 15
|
|
40
|
-
}));
|
|
41
|
-
it('should respect the item level expiration for the cache', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
-
const { set, get } = (0, cache_1.createCache)(); // remember, default expiration is greater than 1 min
|
|
43
|
-
set('ice cream state', 'solid', { expiration: { seconds: 5 } }); // ice cream changes quickly in the heat! lets keep a quick eye on this
|
|
44
|
-
// prove that we recorded the value and its accessible immediately after setting
|
|
45
|
-
const iceCreamState = get('ice cream state');
|
|
46
|
-
expect(iceCreamState).toEqual('solid');
|
|
47
|
-
// prove that the value is still accessible after 4 seconds, since default ttl is 5 seconds
|
|
48
|
-
yield sleep(4 * 1000);
|
|
49
|
-
const iceCreamStateAfter4Sec = get('ice cream state');
|
|
50
|
-
expect(iceCreamStateAfter4Sec).toEqual('solid'); // still should say solid
|
|
51
|
-
// and prove that after a total of 5 seconds, the state is no longer in the cache
|
|
52
|
-
yield sleep(1 * 1000); // sleep 1 more second
|
|
53
|
-
const iceCreamStateAfter5Sec = get('ice cream state');
|
|
54
|
-
expect(iceCreamStateAfter5Sec).toEqual(undefined); // no longer defined, since the item level seconds until expiration was 5
|
|
55
|
-
}));
|
|
56
|
-
it('should consider secondsUntilExpiration of null as never expiring', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
57
|
-
const { set, get } = (0, cache_1.createCache)({
|
|
58
|
-
expiration: { seconds: 0 }, // expire immediately
|
|
59
|
-
});
|
|
60
|
-
// prove that setting something to the cache with default state will have it expired immediately
|
|
61
|
-
yield set('dory-memory', 'something'); // lets see if dory can remember something
|
|
62
|
-
const doryMemory = yield get('dory-memory');
|
|
63
|
-
expect(doryMemory).toEqual(undefined); // its already gone! dang default expiration
|
|
64
|
-
// prove that if we record the memory with expires-at Infinity, it persists
|
|
65
|
-
yield set('elephant-memory', 'something', {
|
|
66
|
-
expiration: null,
|
|
67
|
-
});
|
|
68
|
-
const elephantMemory = yield get('elephant-memory');
|
|
69
|
-
expect(elephantMemory).toEqual('something');
|
|
70
|
-
}));
|
|
71
|
-
it('should accurately get keys', () => {
|
|
72
|
-
// create the cache
|
|
73
|
-
const { set, keys } = (0, cache_1.createCache)();
|
|
74
|
-
// check key is added when value is set
|
|
75
|
-
set('meaning-of-life', '42');
|
|
76
|
-
const keys1 = keys();
|
|
77
|
-
expect(keys1.length).toEqual(1);
|
|
78
|
-
expect(keys1[0]).toEqual('meaning-of-life');
|
|
79
|
-
// check that there are no duplicates when key value is updated
|
|
80
|
-
set('meaning-of-life', '42.0');
|
|
81
|
-
const keys2 = keys();
|
|
82
|
-
expect(keys2.length).toEqual(1);
|
|
83
|
-
expect(keys2[0]).toEqual('meaning-of-life');
|
|
84
|
-
// check that multiple keys can be set
|
|
85
|
-
set('purpose-of-life', 'propagation');
|
|
86
|
-
const keys3 = keys();
|
|
87
|
-
expect(keys3.length).toEqual(2);
|
|
88
|
-
expect(keys3[1]).toEqual('purpose-of-life');
|
|
89
|
-
// check that invalidation removes the key
|
|
90
|
-
set('meaning-of-life', undefined);
|
|
91
|
-
const keys4 = keys();
|
|
92
|
-
expect(keys4.length).toEqual(1);
|
|
93
|
-
expect(keys4[0]).toEqual('purpose-of-life');
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
//# sourceMappingURL=cache.test.js.map
|
package/dist/cache.test.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cache.test.js","sourceRoot":"","sources":["../src/cache.test.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,mCAAsC;AAEtC,IAAI,CAAC,UAAU,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,kGAAkG;AAE9H,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE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