@goatlab/node-backend 0.0.2 → 0.0.4

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.
@@ -0,0 +1,59 @@
1
+ import { type Milliseconds } from '@goatlab/js-utils';
2
+ import Keyv from 'keyv';
3
+ import type { KeyvRedisOptions } from '@keyv/redis';
4
+ export declare class Cache extends Keyv {
5
+ private _ns;
6
+ private usesLRUMemory?;
7
+ private keyvLru;
8
+ private memoryCache;
9
+ constructor({ connection, opts }: {
10
+ connection: string | undefined;
11
+ opts?: KeyvRedisOptions & {
12
+ usesLRUMemory?: boolean;
13
+ };
14
+ });
15
+ private isValidResult;
16
+ private isNotNullish;
17
+ private isNotEmptyString;
18
+ private isNotEmptyArray;
19
+ private isNotEmptyObject;
20
+ get<T>(key: string | string[]): Promise<T>;
21
+ delete(key: string): Promise<boolean>;
22
+ has(key: string[]): Promise<boolean[]>;
23
+ has(key: string): Promise<boolean>;
24
+ /**
25
+ * Get an item from the cache, or execute the given Closure and store the result.
26
+ *
27
+ * @param string $key
28
+ * @param int $ms - time in milliseconds
29
+ * @param $callback
30
+ */
31
+ remember<T>(key: string, ms: Milliseconds, fx: () => Promise<T>): Promise<T>;
32
+ /**
33
+ * Get an item from the cache, or execute the given Closure and store the result forever.
34
+ *
35
+ * @param string $key
36
+ * @param \Closure $callback
37
+ */
38
+ rememberForever<T>(key: string, fx: () => Promise<T>): Promise<T>;
39
+ /**
40
+ * Retrieve an item from the cache and delete it.
41
+ *
42
+ * @param string $key
43
+ */
44
+ pull(key: string): Promise<any>;
45
+ /**
46
+ * Remove an item from the cache.
47
+ *
48
+ * @param string $key
49
+ * @return bool
50
+ */
51
+ forget(key: string): Promise<boolean>;
52
+ /**
53
+ * Remove all items from the cache in the current namespace
54
+ *
55
+ * @return bool
56
+ */
57
+ flush(): Promise<void>;
58
+ deleteWhereStartsWith(value: string): Promise<void>;
59
+ }
package/dist/Cache.js ADDED
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Cache = void 0;
4
+ const js_utils_1 = require("@goatlab/js-utils");
5
+ const keyv_1 = require("keyv");
6
+ const redis_1 = require("@keyv/redis");
7
+ const keyv_lru_1 = require("keyv-lru");
8
+ class Cache extends keyv_1.default {
9
+ constructor({ connection, opts }) {
10
+ super({
11
+ store: connection
12
+ ? new redis_1.default(connection)
13
+ : new keyv_lru_1.KeyvLru({
14
+ max: 1000,
15
+ notify: false,
16
+ ttl: 0,
17
+ expire: 0
18
+ }),
19
+ ...opts
20
+ });
21
+ this.keyvLru = new keyv_lru_1.KeyvLru({
22
+ max: 1000,
23
+ notify: false,
24
+ ttl: 0,
25
+ expire: 0
26
+ });
27
+ this.memoryCache = new keyv_1.default({
28
+ store: this.keyvLru,
29
+ namespace: opts?.namespace || ''
30
+ });
31
+ this._ns = opts?.namespace || '';
32
+ this.usesLRUMemory = opts?.usesLRUMemory || false;
33
+ }
34
+ isValidResult(result) {
35
+ return (this.isNotNullish(result) &&
36
+ this.isNotEmptyString(result) &&
37
+ this.isNotEmptyArray(result) &&
38
+ this.isNotEmptyObject(result));
39
+ }
40
+ isNotNullish(value) {
41
+ return value !== null && value !== undefined;
42
+ }
43
+ isNotEmptyString(value) {
44
+ return typeof value !== 'string' || value.trim() !== '';
45
+ }
46
+ isNotEmptyArray(value) {
47
+ return !Array.isArray(value) || value.length !== 0;
48
+ }
49
+ isNotEmptyObject(value) {
50
+ if (typeof value !== 'object' || Array.isArray(value)) {
51
+ return true;
52
+ }
53
+ const nonNullValues = Object.values(value).filter(objValue => objValue !== null);
54
+ return nonNullValues.length !== 0;
55
+ }
56
+ async get(key) {
57
+ // Search first in LRU memory
58
+ // It will greatly improve performance
59
+ // for "frequent" users
60
+ if (this.usesLRUMemory) {
61
+ const memoryVal = await this.memoryCache.get(`${this._ns}:${key}`);
62
+ if (memoryVal) {
63
+ return memoryVal;
64
+ }
65
+ }
66
+ const result = await super.get(key);
67
+ if (this.usesLRUMemory && result) {
68
+ // We could also just overwrite the set method as well
69
+ await this.memoryCache.set(`${this._ns}:${key}`, result);
70
+ }
71
+ return result;
72
+ }
73
+ async delete(key) {
74
+ if (this.usesLRUMemory) {
75
+ await this.memoryCache.delete(`${this._ns}:${key}`);
76
+ }
77
+ return await super.delete(key);
78
+ }
79
+ async has(key) {
80
+ const value = await this.get(key);
81
+ if (Array.isArray(value)) {
82
+ return value.map(v => this.isValidResult(v));
83
+ }
84
+ return !!value;
85
+ }
86
+ /**
87
+ * Get an item from the cache, or execute the given Closure and store the result.
88
+ *
89
+ * @param string $key
90
+ * @param int $ms - time in milliseconds
91
+ * @param $callback
92
+ */
93
+ async remember(key, ms, fx) {
94
+ const value = await this.get(key);
95
+ if (value) {
96
+ return value;
97
+ }
98
+ const result = await fx();
99
+ if (this.isValidResult(result)) {
100
+ await this.set(key, result, ms);
101
+ }
102
+ return result;
103
+ }
104
+ /**
105
+ * Get an item from the cache, or execute the given Closure and store the result forever.
106
+ *
107
+ * @param string $key
108
+ * @param \Closure $callback
109
+ */
110
+ async rememberForever(key, fx) {
111
+ const value = await this.get(key);
112
+ if (value) {
113
+ return value;
114
+ }
115
+ const result = await fx();
116
+ if (this.isValidResult(result)) {
117
+ await this.set(key, result);
118
+ }
119
+ return result;
120
+ }
121
+ /**
122
+ * Retrieve an item from the cache and delete it.
123
+ *
124
+ * @param string $key
125
+ */
126
+ async pull(key) {
127
+ const value = await this.get(key);
128
+ if (value) {
129
+ await this.delete(key);
130
+ }
131
+ return value;
132
+ }
133
+ /**
134
+ * Remove an item from the cache.
135
+ *
136
+ * @param string $key
137
+ * @return bool
138
+ */
139
+ async forget(key) {
140
+ return await this.delete(key);
141
+ }
142
+ /**
143
+ * Remove all items from the cache in the current namespace
144
+ *
145
+ * @return bool
146
+ */
147
+ async flush() {
148
+ await this.clear();
149
+ }
150
+ async deleteWhereStartsWith(value) {
151
+ if (!this.iterator) {
152
+ await js_utils_1.Promises.map(Object.keys(this.opts.store['cache']['cache']), async (k) => {
153
+ if (k.startsWith(`${this._ns}:${value}`)) {
154
+ await this.delete(k.replace(`${this._ns}:`, ''));
155
+ }
156
+ });
157
+ return;
158
+ }
159
+ for await (const [key] of this.iterator(this._ns)) {
160
+ if (key.startsWith(value)) {
161
+ await this.delete(key);
162
+ }
163
+ }
164
+ }
165
+ }
166
+ exports.Cache = Cache;
167
+ //# sourceMappingURL=Cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Cache.js","sourceRoot":"","sources":["../src/Cache.ts"],"names":[],"mappings":";;;AAAA,gDAA+D;AAC/D,+BAAuB;AACvB,uCAAmC;AAEnC,uCAAkC;AAElC,MAAa,KAAM,SAAQ,cAAI;IAM7B,YAAY,EACV,UAAU,EACV,IAAI,EAIL;QACC,KAAK,CAAC;YACJ,KAAK,EAAE,UAAU;gBACf,CAAC,CAAC,IAAI,eAAS,CAAC,UAAU,CAAC;gBAC3B,CAAC,CAAC,IAAI,kBAAO,CAAC;oBACV,GAAG,EAAE,IAAI;oBACT,MAAM,EAAE,KAAK;oBACb,GAAG,EAAE,CAAC;oBACN,MAAM,EAAE,CAAC;iBACV,CAAC;YACN,GAAG,IAAI;SACR,CAAC,CAAA;QAEF,IAAI,CAAC,OAAO,GAAG,IAAI,kBAAO,CAAC;YACzB,GAAG,EAAE,IAAI;YACT,MAAM,EAAE,KAAK;YACb,GAAG,EAAE,CAAC;YACN,MAAM,EAAE,CAAC;SACV,CAAC,CAAA;QAEF,IAAI,CAAC,WAAW,GAAG,IAAI,cAAI,CAAC;YAC1B,KAAK,EAAE,IAAI,CAAC,OAAO;YACnB,SAAS,EAAE,IAAI,EAAE,SAAS,IAAI,EAAE;SACjC,CAAC,CAAA;QAEF,IAAI,CAAC,GAAG,GAAG,IAAI,EAAE,SAAS,IAAI,EAAE,CAAA;QAChC,IAAI,CAAC,aAAa,GAAG,IAAI,EAAE,aAAa,IAAI,KAAK,CAAA;IACnD,CAAC;IAEO,aAAa,CAAC,MA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