@graphql-mesh/cache-inmemory-lru 0.6.18 → 0.7.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/cjs/index.js +52 -0
- package/cjs/package.json +1 -0
- package/esm/index.js +49 -0
- package/package.json +27 -16
- package/typings/index.d.cts +17 -0
- package/typings/index.d.ts +17 -0
- package/index.d.ts +0 -10
- package/index.js +0 -37
- package/index.mjs +0 -35
package/cjs/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("@graphql-mesh/utils");
|
|
4
|
+
const disposablestack_1 = require("@whatwg-node/disposablestack");
|
|
5
|
+
class InMemoryLRUCache {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.timeouts = new Set();
|
|
8
|
+
this.lru = (0, utils_1.createLruCache)(options?.max, options?.ttl);
|
|
9
|
+
const subId = options?.pubsub?.subscribe?.('destroy', () => {
|
|
10
|
+
options?.pubsub?.unsubscribe(subId);
|
|
11
|
+
this[disposablestack_1.DisposableSymbols.dispose]();
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
get(key) {
|
|
15
|
+
return this.lru.get(key);
|
|
16
|
+
}
|
|
17
|
+
set(key, value, options) {
|
|
18
|
+
this.lru.set(key, value);
|
|
19
|
+
if (options?.ttl && options.ttl > 0) {
|
|
20
|
+
const timeout = setTimeout(() => {
|
|
21
|
+
this.timeouts.delete(timeout);
|
|
22
|
+
this.lru.delete(key);
|
|
23
|
+
}, options.ttl * 1000);
|
|
24
|
+
this.timeouts.add(timeout);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
delete(key) {
|
|
28
|
+
try {
|
|
29
|
+
this.lru.delete(key);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
getKeysByPrefix(prefix) {
|
|
37
|
+
const keysWithPrefix = [];
|
|
38
|
+
for (const key of this.lru.keys()) {
|
|
39
|
+
if (key.startsWith(prefix)) {
|
|
40
|
+
keysWithPrefix.push(key);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return keysWithPrefix;
|
|
44
|
+
}
|
|
45
|
+
[disposablestack_1.DisposableSymbols.dispose]() {
|
|
46
|
+
for (const timeout of this.timeouts) {
|
|
47
|
+
clearTimeout(timeout);
|
|
48
|
+
this.timeouts.delete(timeout);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.default = InMemoryLRUCache;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createLruCache } from '@graphql-mesh/utils';
|
|
2
|
+
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
3
|
+
export default class InMemoryLRUCache {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.timeouts = new Set();
|
|
6
|
+
this.lru = createLruCache(options?.max, options?.ttl);
|
|
7
|
+
const subId = options?.pubsub?.subscribe?.('destroy', () => {
|
|
8
|
+
options?.pubsub?.unsubscribe(subId);
|
|
9
|
+
this[DisposableSymbols.dispose]();
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
get(key) {
|
|
13
|
+
return this.lru.get(key);
|
|
14
|
+
}
|
|
15
|
+
set(key, value, options) {
|
|
16
|
+
this.lru.set(key, value);
|
|
17
|
+
if (options?.ttl && options.ttl > 0) {
|
|
18
|
+
const timeout = setTimeout(() => {
|
|
19
|
+
this.timeouts.delete(timeout);
|
|
20
|
+
this.lru.delete(key);
|
|
21
|
+
}, options.ttl * 1000);
|
|
22
|
+
this.timeouts.add(timeout);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
delete(key) {
|
|
26
|
+
try {
|
|
27
|
+
this.lru.delete(key);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
getKeysByPrefix(prefix) {
|
|
35
|
+
const keysWithPrefix = [];
|
|
36
|
+
for (const key of this.lru.keys()) {
|
|
37
|
+
if (key.startsWith(prefix)) {
|
|
38
|
+
keysWithPrefix.push(key);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return keysWithPrefix;
|
|
42
|
+
}
|
|
43
|
+
[DisposableSymbols.dispose]() {
|
|
44
|
+
for (const timeout of this.timeouts) {
|
|
45
|
+
clearTimeout(timeout);
|
|
46
|
+
this.timeouts.delete(timeout);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,36 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-mesh/cache-inmemory-lru",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"graphql": "*"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@graphql-mesh/types": "0.
|
|
10
|
-
"@graphql-mesh/utils": "0.
|
|
11
|
-
"
|
|
9
|
+
"@graphql-mesh/types": "^0.103.19",
|
|
10
|
+
"@graphql-mesh/utils": "^0.103.19",
|
|
11
|
+
"@whatwg-node/disposablestack": "^0.0.5",
|
|
12
|
+
"tslib": "^2.4.0"
|
|
12
13
|
},
|
|
13
14
|
"repository": {
|
|
14
15
|
"type": "git",
|
|
15
|
-
"url": "
|
|
16
|
+
"url": "ardatan/graphql-mesh",
|
|
16
17
|
"directory": "packages/cache/inmemory-lru"
|
|
17
18
|
},
|
|
18
19
|
"license": "MIT",
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=16.0.0"
|
|
22
|
+
},
|
|
23
|
+
"main": "cjs/index.js",
|
|
24
|
+
"module": "esm/index.js",
|
|
25
|
+
"typings": "typings/index.d.ts",
|
|
22
26
|
"typescript": {
|
|
23
|
-
"definition": "index.d.ts"
|
|
27
|
+
"definition": "typings/index.d.ts"
|
|
24
28
|
},
|
|
29
|
+
"type": "module",
|
|
25
30
|
"exports": {
|
|
26
31
|
".": {
|
|
27
|
-
"require":
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./typings/index.d.cts",
|
|
34
|
+
"default": "./cjs/index.js"
|
|
35
|
+
},
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./typings/index.d.ts",
|
|
38
|
+
"default": "./esm/index.js"
|
|
39
|
+
},
|
|
40
|
+
"default": {
|
|
41
|
+
"types": "./typings/index.d.ts",
|
|
42
|
+
"default": "./esm/index.js"
|
|
43
|
+
}
|
|
33
44
|
},
|
|
34
45
|
"./package.json": "./package.json"
|
|
35
46
|
}
|
|
36
|
-
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { KeyValueCache, KeyValueCacheSetOptions, MeshPubSub } from '@graphql-mesh/types';
|
|
2
|
+
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
3
|
+
export interface InMemoryLRUCacheOptions {
|
|
4
|
+
max?: number;
|
|
5
|
+
ttl?: number;
|
|
6
|
+
pubsub?: MeshPubSub;
|
|
7
|
+
}
|
|
8
|
+
export default class InMemoryLRUCache<V = any> implements KeyValueCache<V>, Disposable {
|
|
9
|
+
private lru;
|
|
10
|
+
private timeouts;
|
|
11
|
+
constructor(options?: InMemoryLRUCacheOptions);
|
|
12
|
+
get(key: string): any;
|
|
13
|
+
set(key: string, value: any, options?: KeyValueCacheSetOptions): void;
|
|
14
|
+
delete(key: string): boolean;
|
|
15
|
+
getKeysByPrefix(prefix: string): any[];
|
|
16
|
+
[DisposableSymbols.dispose](): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { KeyValueCache, KeyValueCacheSetOptions, MeshPubSub } from '@graphql-mesh/types';
|
|
2
|
+
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
3
|
+
export interface InMemoryLRUCacheOptions {
|
|
4
|
+
max?: number;
|
|
5
|
+
ttl?: number;
|
|
6
|
+
pubsub?: MeshPubSub;
|
|
7
|
+
}
|
|
8
|
+
export default class InMemoryLRUCache<V = any> implements KeyValueCache<V>, Disposable {
|
|
9
|
+
private lru;
|
|
10
|
+
private timeouts;
|
|
11
|
+
constructor(options?: InMemoryLRUCacheOptions);
|
|
12
|
+
get(key: string): any;
|
|
13
|
+
set(key: string, value: any, options?: KeyValueCacheSetOptions): void;
|
|
14
|
+
delete(key: string): boolean;
|
|
15
|
+
getKeysByPrefix(prefix: string): any[];
|
|
16
|
+
[DisposableSymbols.dispose](): void;
|
|
17
|
+
}
|
package/index.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { KeyValueCache, KeyValueCacheSetOptions, YamlConfig } from '@graphql-mesh/types';
|
|
2
|
-
export default class InMemoryLRUCache<V = any> implements KeyValueCache<V> {
|
|
3
|
-
private cacheIdentifier;
|
|
4
|
-
private lruCache;
|
|
5
|
-
constructor(options?: YamlConfig.InMemoryLRUConfig);
|
|
6
|
-
private nextTick;
|
|
7
|
-
get(key: string): Promise<V>;
|
|
8
|
-
set(key: string, value: V, options?: KeyValueCacheSetOptions): Promise<void>;
|
|
9
|
-
delete(key: string): Promise<void>;
|
|
10
|
-
}
|
package/index.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const utils = require('@graphql-mesh/utils');
|
|
4
|
-
|
|
5
|
-
class InMemoryLRUCache {
|
|
6
|
-
constructor(options) {
|
|
7
|
-
this.cacheIdentifier = Date.now();
|
|
8
|
-
this.lruCache = utils.createLruCache(options === null || options === void 0 ? void 0 : options.max, options === null || options === void 0 ? void 0 : options.ttl);
|
|
9
|
-
}
|
|
10
|
-
nextTick() {
|
|
11
|
-
// Make sure this is scheduled for next tick because LRU Cache is synchronous
|
|
12
|
-
// This helps for testing multiple Mesh instances pointing to the same cache
|
|
13
|
-
return new Promise(resolve => setTimeout(resolve));
|
|
14
|
-
}
|
|
15
|
-
async get(key) {
|
|
16
|
-
await this.nextTick();
|
|
17
|
-
const entry = this.lruCache.get(`${this.cacheIdentifier}-${key}`);
|
|
18
|
-
if ((entry === null || entry === void 0 ? void 0 : entry.expiresAt) && Date.now() > entry.expiresAt) {
|
|
19
|
-
this.lruCache.delete(key);
|
|
20
|
-
return undefined;
|
|
21
|
-
}
|
|
22
|
-
return entry === null || entry === void 0 ? void 0 : entry.value;
|
|
23
|
-
}
|
|
24
|
-
async set(key, value, options) {
|
|
25
|
-
await this.nextTick();
|
|
26
|
-
this.lruCache.set(`${this.cacheIdentifier}-${key}`, {
|
|
27
|
-
expiresAt: (options === null || options === void 0 ? void 0 : options.ttl) ? Date.now() + options.ttl * 1000 : Infinity,
|
|
28
|
-
value,
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
async delete(key) {
|
|
32
|
-
await this.nextTick();
|
|
33
|
-
this.lruCache.delete(`${this.cacheIdentifier}-${key}`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
module.exports = InMemoryLRUCache;
|
package/index.mjs
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { createLruCache } from '@graphql-mesh/utils';
|
|
2
|
-
|
|
3
|
-
class InMemoryLRUCache {
|
|
4
|
-
constructor(options) {
|
|
5
|
-
this.cacheIdentifier = Date.now();
|
|
6
|
-
this.lruCache = createLruCache(options === null || options === void 0 ? void 0 : options.max, options === null || options === void 0 ? void 0 : options.ttl);
|
|
7
|
-
}
|
|
8
|
-
nextTick() {
|
|
9
|
-
// Make sure this is scheduled for next tick because LRU Cache is synchronous
|
|
10
|
-
// This helps for testing multiple Mesh instances pointing to the same cache
|
|
11
|
-
return new Promise(resolve => setTimeout(resolve));
|
|
12
|
-
}
|
|
13
|
-
async get(key) {
|
|
14
|
-
await this.nextTick();
|
|
15
|
-
const entry = this.lruCache.get(`${this.cacheIdentifier}-${key}`);
|
|
16
|
-
if ((entry === null || entry === void 0 ? void 0 : entry.expiresAt) && Date.now() > entry.expiresAt) {
|
|
17
|
-
this.lruCache.delete(key);
|
|
18
|
-
return undefined;
|
|
19
|
-
}
|
|
20
|
-
return entry === null || entry === void 0 ? void 0 : entry.value;
|
|
21
|
-
}
|
|
22
|
-
async set(key, value, options) {
|
|
23
|
-
await this.nextTick();
|
|
24
|
-
this.lruCache.set(`${this.cacheIdentifier}-${key}`, {
|
|
25
|
-
expiresAt: (options === null || options === void 0 ? void 0 : options.ttl) ? Date.now() + options.ttl * 1000 : Infinity,
|
|
26
|
-
value,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
async delete(key) {
|
|
30
|
-
await this.nextTick();
|
|
31
|
-
this.lruCache.delete(`${this.cacheIdentifier}-${key}`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export default InMemoryLRUCache;
|