@peerbit/cache 1.0.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 ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2018 Protocol Labs Inc.
4
+ Copyright (c) 2018 Haja Networks Oy
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
@@ -0,0 +1,23 @@
1
+ export type CacheData<T> = {
2
+ value?: T | null;
3
+ time: number;
4
+ };
5
+ export declare class Cache<T = undefined> {
6
+ private _map;
7
+ private deleted;
8
+ private list;
9
+ max: number;
10
+ ttl?: number;
11
+ constructor(options: {
12
+ max: number;
13
+ ttl?: number;
14
+ });
15
+ has(key: string): boolean;
16
+ get map(): Map<string, CacheData<T>>;
17
+ get(key: string): T | null | undefined;
18
+ trim(time?: number): void;
19
+ del(key: string): void;
20
+ add(key: string, value?: T): void;
21
+ clear(): void;
22
+ get size(): number;
23
+ }
@@ -0,0 +1,73 @@
1
+ // Fifo
2
+ import yallist from "yallist";
3
+ export class Cache {
4
+ _map;
5
+ deleted;
6
+ list;
7
+ max;
8
+ ttl;
9
+ constructor(options) {
10
+ if (options.max <= 0) {
11
+ throw new Error("Expecting max >= 0");
12
+ }
13
+ this.max = options.max;
14
+ this.ttl = options.ttl;
15
+ this.clear();
16
+ }
17
+ has(key) {
18
+ this.trim();
19
+ if (this.deleted.has(key)) {
20
+ return false;
21
+ }
22
+ return this._map.has(key);
23
+ }
24
+ get map() {
25
+ return this._map;
26
+ }
27
+ get(key) {
28
+ this.trim();
29
+ if (this.deleted.has(key)) {
30
+ return undefined;
31
+ }
32
+ return this._map.get(key)?.value;
33
+ }
34
+ trim(time = +new Date()) {
35
+ const peek = this.list.head;
36
+ let outOfDate = peek &&
37
+ this.ttl !== undefined &&
38
+ this._map.get(peek.value).time < time - this.ttl;
39
+ while (outOfDate || this.list.length > this.max) {
40
+ const key = this.list.shift();
41
+ if (key !== undefined) {
42
+ outOfDate =
43
+ this.ttl !== undefined && this._map.get(key).time < time - this.ttl;
44
+ this._map.delete(key);
45
+ this.deleted.delete(key);
46
+ }
47
+ else {
48
+ break;
49
+ }
50
+ }
51
+ }
52
+ del(key) {
53
+ this.deleted.add(key);
54
+ }
55
+ add(key, value) {
56
+ const time = +new Date();
57
+ this.trim(time);
58
+ if (!this._map.has(key)) {
59
+ this.list.push(key);
60
+ }
61
+ this._map.set(key, { time, value: value ?? null });
62
+ this.deleted.delete(key);
63
+ }
64
+ clear() {
65
+ this.list = yallist.create();
66
+ this._map = new Map();
67
+ this.deleted = new Set();
68
+ }
69
+ get size() {
70
+ return this.list.length - this.deleted.size;
71
+ }
72
+ }
73
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO;AACP,OAAO,OAAO,MAAM,SAAS,CAAC;AAG9B,MAAM,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G,EAAE,CAAC;IAC1B,CAAC;IAED,IAAI,IAAI;QACP,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;IAC7C,CAAC;CACD"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@peerbit/cache",
3
+ "version": "1.0.0",
4
+ "description": "Simple cache",
5
+ "type": "module",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "files": [
10
+ "lib",
11
+ "src",
12
+ "!src/**/__tests__",
13
+ "!lib/**/__tests__",
14
+ "LICENSE"
15
+ ],
16
+ "module": "lib/esm/index.js",
17
+ "types": "lib/esm/index.d.ts",
18
+ "exports": {
19
+ "import": "./lib/esm/index.js",
20
+ "require": "./lib/cjs/index.js"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/dao-xyz/peerbit"
25
+ },
26
+ "homepage": "https://github.com/dao-xyz/peerbit",
27
+ "bugs": "https://github.com/dao-xyz/peerbit/issues",
28
+ "scripts": {
29
+ "clean": "shx rm -rf lib/*",
30
+ "build": "yarn clean && tsc -p tsconfig.json",
31
+ "postbuild": "echo '{\"type\":\"module\"} ' | node ../../../node_modules/.bin/json > lib/esm/package.json",
32
+ "test": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.ts --runInBand --forceExit",
33
+ "test:unit": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.unit.ts --runInBand --forceExit",
34
+ "test:integration": "node ../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit"
35
+ },
36
+ "license": "MIT",
37
+ "devDependencies": {
38
+ "@peerbit/time": "1.0.0",
39
+ "@types/yallist": "^4.0.1"
40
+ },
41
+ "dependencies": {
42
+ "yallist": "^4.0.0"
43
+ },
44
+ "localMaintainers": [
45
+ "dao.xyz"
46
+ ],
47
+ "gitHead": "069ce2f62a76c342875a1cc695c6f210beff13fd"
48
+ }
package/src/index.ts ADDED
@@ -0,0 +1,80 @@
1
+ // Fifo
2
+ import yallist from "yallist";
3
+
4
+ export type CacheData<T> = { value?: T | null; time: number };
5
+ export class Cache<T = undefined> {
6
+ private _map: Map<string, CacheData<T>>;
7
+ private deleted: Set<string>;
8
+ private list: yallist<string>;
9
+ max: number;
10
+ ttl?: number;
11
+ constructor(options: { max: number; ttl?: number }) {
12
+ if (options.max <= 0) {
13
+ throw new Error("Expecting max >= 0");
14
+ }
15
+ this.max = options.max;
16
+ this.ttl = options.ttl;
17
+ this.clear();
18
+ }
19
+ has(key: string) {
20
+ this.trim();
21
+ if (this.deleted.has(key)) {
22
+ return false;
23
+ }
24
+ return this._map.has(key);
25
+ }
26
+
27
+ get map(): Map<string, CacheData<T>> {
28
+ return this._map;
29
+ }
30
+
31
+ get(key: string): T | null | undefined {
32
+ this.trim();
33
+ if (this.deleted.has(key)) {
34
+ return undefined;
35
+ }
36
+ return this._map.get(key)?.value;
37
+ }
38
+
39
+ trim(time = +new Date()) {
40
+ const peek = this.list.head;
41
+ let outOfDate =
42
+ peek &&
43
+ this.ttl !== undefined &&
44
+ this._map.get(peek.value)!.time < time - this.ttl;
45
+ while (outOfDate || this.list.length > this.max) {
46
+ const key = this.list.shift();
47
+ if (key !== undefined) {
48
+ outOfDate =
49
+ this.ttl !== undefined && this._map.get(key)!.time < time - this.ttl;
50
+ this._map.delete(key);
51
+ this.deleted.delete(key);
52
+ } else {
53
+ break;
54
+ }
55
+ }
56
+ }
57
+
58
+ del(key: string) {
59
+ this.deleted.add(key);
60
+ }
61
+
62
+ add(key: string, value?: T) {
63
+ const time = +new Date();
64
+ this.trim(time);
65
+ if (!this._map.has(key)) {
66
+ this.list.push(key);
67
+ }
68
+ this._map.set(key, { time, value: value ?? null });
69
+ this.deleted.delete(key);
70
+ }
71
+ clear() {
72
+ this.list = yallist.create();
73
+ this._map = new Map();
74
+ this.deleted = new Set();
75
+ }
76
+
77
+ get size() {
78
+ return this.list.length - this.deleted.size;
79
+ }
80
+ }