@nocobase/cache 1.2.13-alpha → 1.3.0-alpha.20240710084543
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/package.json +2 -2
- package/lib/bloom-filter/index.d.ts +0 -17
- package/lib/bloom-filter/index.js +0 -24
- package/lib/bloom-filter/memory-bloom-filter.d.ts +0 -21
- package/lib/bloom-filter/memory-bloom-filter.js +0 -70
- package/lib/bloom-filter/redis-bloom-filter.d.ts +0 -23
- package/lib/bloom-filter/redis-bloom-filter.js +0 -64
- package/lib/cache-manager.d.ts +0 -56
- package/lib/cache-manager.js +0 -169
- package/lib/cache.d.ts +0 -38
- package/lib/cache.js +0 -118
- package/lib/index.d.ts +0 -11
- package/lib/index.js +0 -34
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/cache",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0-alpha.20240710084543",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "AGPL-3.0",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -18,5 +18,5 @@
|
|
|
18
18
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
19
19
|
"directory": "packages/cache"
|
|
20
20
|
},
|
|
21
|
-
"gitHead": "
|
|
21
|
+
"gitHead": "07a8b596fc64a9779a194cb9b0dc2ca7570ed9d4"
|
|
22
22
|
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* @experimental
|
|
11
|
-
*/
|
|
12
|
-
export interface BloomFilter {
|
|
13
|
-
reserve(key: string, errorRate: number, capacity: number): Promise<void>;
|
|
14
|
-
add(key: string, val: string): Promise<void>;
|
|
15
|
-
mAdd(key: string, vals: string[]): Promise<void>;
|
|
16
|
-
exists(key: string, val: string): Promise<boolean>;
|
|
17
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
var __defProp = Object.defineProperty;
|
|
11
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
-
var __copyProps = (to, from, except, desc) => {
|
|
15
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
-
for (let key of __getOwnPropNames(from))
|
|
17
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
-
}
|
|
20
|
-
return to;
|
|
21
|
-
};
|
|
22
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
|
-
var bloom_filter_exports = {};
|
|
24
|
-
module.exports = __toCommonJS(bloom_filter_exports);
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
import { BloomFilter as IBloomFilter } from '.';
|
|
10
|
-
import { Cache } from '../cache';
|
|
11
|
-
/**
|
|
12
|
-
* @experimental
|
|
13
|
-
*/
|
|
14
|
-
export declare class MemoryBloomFilter implements IBloomFilter {
|
|
15
|
-
cache: Cache;
|
|
16
|
-
constructor(cache: Cache);
|
|
17
|
-
reserve(key: string, errorRate: number, capacity: number): Promise<void>;
|
|
18
|
-
add(key: string, value: string): Promise<void>;
|
|
19
|
-
mAdd(key: string, values: string[]): Promise<void>;
|
|
20
|
-
exists(key: string, value: string): Promise<boolean>;
|
|
21
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
var __defProp = Object.defineProperty;
|
|
11
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
-
var __export = (target, all) => {
|
|
16
|
-
for (var name in all)
|
|
17
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
-
};
|
|
19
|
-
var __copyProps = (to, from, except, desc) => {
|
|
20
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
-
for (let key of __getOwnPropNames(from))
|
|
22
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
-
}
|
|
25
|
-
return to;
|
|
26
|
-
};
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var memory_bloom_filter_exports = {};
|
|
29
|
-
__export(memory_bloom_filter_exports, {
|
|
30
|
-
MemoryBloomFilter: () => MemoryBloomFilter
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(memory_bloom_filter_exports);
|
|
33
|
-
var import_bloom_filters = require("bloom-filters");
|
|
34
|
-
const _MemoryBloomFilter = class _MemoryBloomFilter {
|
|
35
|
-
cache;
|
|
36
|
-
constructor(cache) {
|
|
37
|
-
this.cache = cache;
|
|
38
|
-
}
|
|
39
|
-
async reserve(key, errorRate, capacity) {
|
|
40
|
-
const filter = import_bloom_filters.BloomFilter.create(capacity, errorRate);
|
|
41
|
-
await this.cache.set(key, filter);
|
|
42
|
-
}
|
|
43
|
-
async add(key, value) {
|
|
44
|
-
const filter = await this.cache.get(key);
|
|
45
|
-
if (!filter) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
filter.add(value);
|
|
49
|
-
}
|
|
50
|
-
async mAdd(key, values) {
|
|
51
|
-
const filter = await this.cache.get(key);
|
|
52
|
-
if (!filter) {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
values.forEach((value) => filter.add(value));
|
|
56
|
-
}
|
|
57
|
-
async exists(key, value) {
|
|
58
|
-
const filter = await this.cache.get(key);
|
|
59
|
-
if (!filter) {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
return filter.has(value);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
__name(_MemoryBloomFilter, "MemoryBloomFilter");
|
|
66
|
-
let MemoryBloomFilter = _MemoryBloomFilter;
|
|
67
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
68
|
-
0 && (module.exports = {
|
|
69
|
-
MemoryBloomFilter
|
|
70
|
-
});
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
import { RedisStore } from 'cache-manager-redis-yet';
|
|
10
|
-
import { BloomFilter } from '.';
|
|
11
|
-
import { Cache } from '../cache';
|
|
12
|
-
/**
|
|
13
|
-
* @experimental
|
|
14
|
-
*/
|
|
15
|
-
export declare class RedisBloomFilter implements BloomFilter {
|
|
16
|
-
cache: Cache;
|
|
17
|
-
constructor(cache: Cache);
|
|
18
|
-
getStore(): RedisStore<import("redis").RedisClientType>;
|
|
19
|
-
reserve(key: string, errorRate: number, capacity: number): Promise<void>;
|
|
20
|
-
add(key: string, value: string): Promise<void>;
|
|
21
|
-
mAdd(key: string, values: string[]): Promise<void>;
|
|
22
|
-
exists(key: string, value: string): Promise<boolean>;
|
|
23
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
var __defProp = Object.defineProperty;
|
|
11
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
-
var __export = (target, all) => {
|
|
16
|
-
for (var name in all)
|
|
17
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
-
};
|
|
19
|
-
var __copyProps = (to, from, except, desc) => {
|
|
20
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
-
for (let key of __getOwnPropNames(from))
|
|
22
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
-
}
|
|
25
|
-
return to;
|
|
26
|
-
};
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var redis_bloom_filter_exports = {};
|
|
29
|
-
__export(redis_bloom_filter_exports, {
|
|
30
|
-
RedisBloomFilter: () => RedisBloomFilter
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(redis_bloom_filter_exports);
|
|
33
|
-
/* istanbul ignore file -- @preserve */
|
|
34
|
-
const _RedisBloomFilter = class _RedisBloomFilter {
|
|
35
|
-
cache;
|
|
36
|
-
constructor(cache) {
|
|
37
|
-
this.cache = cache;
|
|
38
|
-
}
|
|
39
|
-
getStore() {
|
|
40
|
-
return this.cache.store.store;
|
|
41
|
-
}
|
|
42
|
-
async reserve(key, errorRate, capacity) {
|
|
43
|
-
const store = this.getStore();
|
|
44
|
-
await store.client.bf.reserve(key, errorRate, capacity);
|
|
45
|
-
}
|
|
46
|
-
async add(key, value) {
|
|
47
|
-
const store = this.getStore();
|
|
48
|
-
await store.client.bf.add(key, value);
|
|
49
|
-
}
|
|
50
|
-
async mAdd(key, values) {
|
|
51
|
-
const store = this.getStore();
|
|
52
|
-
await store.client.bf.mAdd(key, values);
|
|
53
|
-
}
|
|
54
|
-
async exists(key, value) {
|
|
55
|
-
const store = this.getStore();
|
|
56
|
-
return await store.client.bf.exists(key, value);
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
__name(_RedisBloomFilter, "RedisBloomFilter");
|
|
60
|
-
let RedisBloomFilter = _RedisBloomFilter;
|
|
61
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
62
|
-
0 && (module.exports = {
|
|
63
|
-
RedisBloomFilter
|
|
64
|
-
});
|
package/lib/cache-manager.d.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
import { FactoryStore, Store } from 'cache-manager';
|
|
10
|
-
import { Cache } from './cache';
|
|
11
|
-
import { BloomFilter } from './bloom-filter';
|
|
12
|
-
type StoreOptions = {
|
|
13
|
-
store?: 'memory' | FactoryStore<Store, any>;
|
|
14
|
-
close?: (store: Store) => Promise<void>;
|
|
15
|
-
[key: string]: any;
|
|
16
|
-
};
|
|
17
|
-
export type CacheManagerOptions = Partial<{
|
|
18
|
-
defaultStore: string;
|
|
19
|
-
stores: {
|
|
20
|
-
[storeType: string]: StoreOptions;
|
|
21
|
-
};
|
|
22
|
-
}>;
|
|
23
|
-
export declare class CacheManager {
|
|
24
|
-
defaultStore: string;
|
|
25
|
-
private stores;
|
|
26
|
-
/**
|
|
27
|
-
* @internal
|
|
28
|
-
*/
|
|
29
|
-
storeTypes: Map<string, StoreOptions>;
|
|
30
|
-
/**
|
|
31
|
-
* @internal
|
|
32
|
-
*/
|
|
33
|
-
caches: Map<string, Cache>;
|
|
34
|
-
constructor(options?: CacheManagerOptions);
|
|
35
|
-
private createStore;
|
|
36
|
-
registerStore(options: {
|
|
37
|
-
name: string;
|
|
38
|
-
} & StoreOptions): void;
|
|
39
|
-
private newCache;
|
|
40
|
-
createCache(options: {
|
|
41
|
-
name: string;
|
|
42
|
-
prefix?: string;
|
|
43
|
-
store?: string;
|
|
44
|
-
[key: string]: any;
|
|
45
|
-
}): Promise<Cache>;
|
|
46
|
-
getCache(name: string): Cache;
|
|
47
|
-
flushAll(): Promise<void>;
|
|
48
|
-
close(): Promise<void>;
|
|
49
|
-
/**
|
|
50
|
-
* @experimental
|
|
51
|
-
*/
|
|
52
|
-
createBloomFilter(options?: {
|
|
53
|
-
store?: string;
|
|
54
|
-
}): Promise<BloomFilter>;
|
|
55
|
-
}
|
|
56
|
-
export {};
|
package/lib/cache-manager.js
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
var __create = Object.create;
|
|
11
|
-
var __defProp = Object.defineProperty;
|
|
12
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
17
|
-
var __export = (target, all) => {
|
|
18
|
-
for (var name in all)
|
|
19
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
-
};
|
|
21
|
-
var __copyProps = (to, from, except, desc) => {
|
|
22
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
-
for (let key of __getOwnPropNames(from))
|
|
24
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
-
}
|
|
27
|
-
return to;
|
|
28
|
-
};
|
|
29
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
30
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
31
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
32
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
33
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
34
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
35
|
-
mod
|
|
36
|
-
));
|
|
37
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
-
var cache_manager_exports = {};
|
|
39
|
-
__export(cache_manager_exports, {
|
|
40
|
-
CacheManager: () => CacheManager
|
|
41
|
-
});
|
|
42
|
-
module.exports = __toCommonJS(cache_manager_exports);
|
|
43
|
-
var import_cache_manager = require("cache-manager");
|
|
44
|
-
var import_cache = require("./cache");
|
|
45
|
-
var import_lodash = __toESM(require("lodash"));
|
|
46
|
-
var import_cache_manager_redis_yet = require("cache-manager-redis-yet");
|
|
47
|
-
var import_deepmerge = __toESM(require("deepmerge"));
|
|
48
|
-
var import_memory_bloom_filter = require("./bloom-filter/memory-bloom-filter");
|
|
49
|
-
var import_redis_bloom_filter = require("./bloom-filter/redis-bloom-filter");
|
|
50
|
-
const _CacheManager = class _CacheManager {
|
|
51
|
-
defaultStore;
|
|
52
|
-
stores = /* @__PURE__ */ new Map();
|
|
53
|
-
/**
|
|
54
|
-
* @internal
|
|
55
|
-
*/
|
|
56
|
-
storeTypes = /* @__PURE__ */ new Map();
|
|
57
|
-
/**
|
|
58
|
-
* @internal
|
|
59
|
-
*/
|
|
60
|
-
caches = /* @__PURE__ */ new Map();
|
|
61
|
-
constructor(options) {
|
|
62
|
-
const defaultOptions = {
|
|
63
|
-
defaultStore: "memory",
|
|
64
|
-
stores: {
|
|
65
|
-
memory: {
|
|
66
|
-
store: "memory",
|
|
67
|
-
// global config
|
|
68
|
-
max: 2e3
|
|
69
|
-
},
|
|
70
|
-
redis: {
|
|
71
|
-
store: import_cache_manager_redis_yet.redisStore,
|
|
72
|
-
close: async (redis) => {
|
|
73
|
-
await redis.client.quit();
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
const cacheOptions = (0, import_deepmerge.default)(defaultOptions, options || {});
|
|
79
|
-
const { defaultStore = "memory", stores } = cacheOptions;
|
|
80
|
-
this.defaultStore = defaultStore;
|
|
81
|
-
for (const [name, store] of Object.entries(stores)) {
|
|
82
|
-
const { store: s, ...globalConfig } = store;
|
|
83
|
-
this.registerStore({ name, store: s, ...globalConfig });
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
async createStore(options) {
|
|
87
|
-
const { name, storeType: type, ...config } = options;
|
|
88
|
-
const storeType = this.storeTypes.get(type);
|
|
89
|
-
if (!storeType) {
|
|
90
|
-
throw new Error(`Create cache failed, store type [${type}] is unavailable or not registered`);
|
|
91
|
-
}
|
|
92
|
-
const { store: s, close, ...globalConfig } = storeType;
|
|
93
|
-
const store = await (0, import_cache_manager.caching)(s, { ...globalConfig, ...config });
|
|
94
|
-
this.stores.set(name, { close, store });
|
|
95
|
-
return store;
|
|
96
|
-
}
|
|
97
|
-
registerStore(options) {
|
|
98
|
-
const { name, ...rest } = options;
|
|
99
|
-
this.storeTypes.set(name, rest);
|
|
100
|
-
}
|
|
101
|
-
newCache(options) {
|
|
102
|
-
const { name, prefix, store } = options;
|
|
103
|
-
const cache = new import_cache.Cache({ name, prefix, store });
|
|
104
|
-
this.caches.set(name, cache);
|
|
105
|
-
return cache;
|
|
106
|
-
}
|
|
107
|
-
async createCache(options) {
|
|
108
|
-
const { name, prefix, store = this.defaultStore, ...config } = options;
|
|
109
|
-
if (!import_lodash.default.isEmpty(config)) {
|
|
110
|
-
const newStore = await this.createStore({ name, storeType: store, ...config });
|
|
111
|
-
return this.newCache({ name, prefix, store: newStore });
|
|
112
|
-
}
|
|
113
|
-
const s = this.stores.get(store);
|
|
114
|
-
if (!s) {
|
|
115
|
-
const defaultStore = await this.createStore({ name: store, storeType: store });
|
|
116
|
-
return this.newCache({ name, prefix, store: defaultStore });
|
|
117
|
-
}
|
|
118
|
-
return this.newCache({ name, prefix, store: s.store });
|
|
119
|
-
}
|
|
120
|
-
getCache(name) {
|
|
121
|
-
const cache = this.caches.get(name);
|
|
122
|
-
if (!cache) {
|
|
123
|
-
throw new Error(`Get cache failed, ${name} is not found`);
|
|
124
|
-
}
|
|
125
|
-
return cache;
|
|
126
|
-
}
|
|
127
|
-
async flushAll() {
|
|
128
|
-
const promises = [];
|
|
129
|
-
for (const cache of this.caches.values()) {
|
|
130
|
-
promises.push(cache.reset());
|
|
131
|
-
}
|
|
132
|
-
await Promise.all(promises);
|
|
133
|
-
}
|
|
134
|
-
async close() {
|
|
135
|
-
const promises = [];
|
|
136
|
-
for (const s of this.stores.values()) {
|
|
137
|
-
const { close, store } = s;
|
|
138
|
-
close && promises.push(close(store.store));
|
|
139
|
-
}
|
|
140
|
-
await Promise.all(promises);
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* @experimental
|
|
144
|
-
*/
|
|
145
|
-
async createBloomFilter(options) {
|
|
146
|
-
const name = "bloom-filter";
|
|
147
|
-
const { store = this.defaultStore } = options || {};
|
|
148
|
-
let cache;
|
|
149
|
-
try {
|
|
150
|
-
cache = this.getCache(name);
|
|
151
|
-
} catch (error) {
|
|
152
|
-
cache = await this.createCache({ name, store });
|
|
153
|
-
}
|
|
154
|
-
switch (store) {
|
|
155
|
-
case "memory":
|
|
156
|
-
return new import_memory_bloom_filter.MemoryBloomFilter(cache);
|
|
157
|
-
case "redis":
|
|
158
|
-
return new import_redis_bloom_filter.RedisBloomFilter(cache);
|
|
159
|
-
default:
|
|
160
|
-
throw new Error(`BloomFilter store [${store}] is not supported`);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
__name(_CacheManager, "CacheManager");
|
|
165
|
-
let CacheManager = _CacheManager;
|
|
166
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
167
|
-
0 && (module.exports = {
|
|
168
|
-
CacheManager
|
|
169
|
-
});
|
package/lib/cache.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
import { Cache as BasicCache, Milliseconds } from 'cache-manager';
|
|
10
|
-
export declare class Cache {
|
|
11
|
-
name: string;
|
|
12
|
-
prefix?: string;
|
|
13
|
-
store: BasicCache;
|
|
14
|
-
constructor({ name, prefix, store }: {
|
|
15
|
-
name: string;
|
|
16
|
-
store: BasicCache;
|
|
17
|
-
prefix?: string;
|
|
18
|
-
});
|
|
19
|
-
key(key: string): string;
|
|
20
|
-
set(key: string, value: unknown, ttl?: Milliseconds): Promise<void>;
|
|
21
|
-
get<T>(key: string): Promise<T>;
|
|
22
|
-
del(key: string): Promise<void>;
|
|
23
|
-
reset(): Promise<void>;
|
|
24
|
-
wrap<T>(key: string, fn: () => Promise<T>, ttl?: Milliseconds): Promise<T>;
|
|
25
|
-
wrapWithCondition<T>(key: string, fn: () => T | Promise<T>, options?: {
|
|
26
|
-
useCache?: boolean;
|
|
27
|
-
isCacheable?: (val: unknown) => boolean | Promise<boolean>;
|
|
28
|
-
ttl?: Milliseconds;
|
|
29
|
-
}): Promise<T>;
|
|
30
|
-
mset(args: [string, unknown][], ttl?: Milliseconds): Promise<void>;
|
|
31
|
-
mget(...args: string[]): Promise<unknown[]>;
|
|
32
|
-
mdel(...args: string[]): Promise<void>;
|
|
33
|
-
keys(pattern?: string): Promise<string[]>;
|
|
34
|
-
ttl(key: string): Promise<number>;
|
|
35
|
-
setValueInObject(key: string, objectKey: string, value: unknown): Promise<void>;
|
|
36
|
-
getValueInObject(key: string, objectKey: string): Promise<any>;
|
|
37
|
-
delValueInObject(key: string, objectKey: string): Promise<void>;
|
|
38
|
-
}
|
package/lib/cache.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
var __defProp = Object.defineProperty;
|
|
11
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
-
var __export = (target, all) => {
|
|
16
|
-
for (var name in all)
|
|
17
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
-
};
|
|
19
|
-
var __copyProps = (to, from, except, desc) => {
|
|
20
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
-
for (let key of __getOwnPropNames(from))
|
|
22
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
-
}
|
|
25
|
-
return to;
|
|
26
|
-
};
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var cache_exports = {};
|
|
29
|
-
__export(cache_exports, {
|
|
30
|
-
Cache: () => Cache
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(cache_exports);
|
|
33
|
-
const _Cache = class _Cache {
|
|
34
|
-
name;
|
|
35
|
-
prefix;
|
|
36
|
-
store;
|
|
37
|
-
constructor({ name, prefix, store }) {
|
|
38
|
-
this.name = name;
|
|
39
|
-
this.prefix = prefix;
|
|
40
|
-
this.store = store;
|
|
41
|
-
}
|
|
42
|
-
key(key) {
|
|
43
|
-
return this.prefix ? `${this.prefix}:${key}` : key;
|
|
44
|
-
}
|
|
45
|
-
async set(key, value, ttl) {
|
|
46
|
-
await this.store.set(this.key(key), value, ttl);
|
|
47
|
-
}
|
|
48
|
-
async get(key) {
|
|
49
|
-
return await this.store.get(this.key(key));
|
|
50
|
-
}
|
|
51
|
-
async del(key) {
|
|
52
|
-
await this.store.del(this.key(key));
|
|
53
|
-
}
|
|
54
|
-
async reset() {
|
|
55
|
-
await this.store.reset();
|
|
56
|
-
}
|
|
57
|
-
async wrap(key, fn, ttl) {
|
|
58
|
-
return await this.store.wrap(this.key(key), fn, ttl);
|
|
59
|
-
}
|
|
60
|
-
async wrapWithCondition(key, fn, options) {
|
|
61
|
-
const { useCache, isCacheable, ttl } = options || {};
|
|
62
|
-
if (useCache === false) {
|
|
63
|
-
return await fn();
|
|
64
|
-
}
|
|
65
|
-
const value = await this.get(key);
|
|
66
|
-
if (value) {
|
|
67
|
-
return value;
|
|
68
|
-
}
|
|
69
|
-
const result = await fn();
|
|
70
|
-
const cacheable = isCacheable ? await isCacheable(result) : result;
|
|
71
|
-
if (!cacheable) {
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
await this.set(key, result, ttl);
|
|
75
|
-
return result;
|
|
76
|
-
}
|
|
77
|
-
async mset(args, ttl) {
|
|
78
|
-
await this.store.store.mset(
|
|
79
|
-
args.map(([key, value]) => [this.key(key), value]),
|
|
80
|
-
ttl
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
async mget(...args) {
|
|
84
|
-
args = args.map((key) => this.key(key));
|
|
85
|
-
return await this.store.store.mget(...args);
|
|
86
|
-
}
|
|
87
|
-
async mdel(...args) {
|
|
88
|
-
args = args.map((key) => this.key(key));
|
|
89
|
-
await this.store.store.mdel(...args);
|
|
90
|
-
}
|
|
91
|
-
async keys(pattern) {
|
|
92
|
-
const keys = await this.store.store.keys(pattern);
|
|
93
|
-
return keys.map((key) => key.replace(`${this.name}:`, ""));
|
|
94
|
-
}
|
|
95
|
-
async ttl(key) {
|
|
96
|
-
return await this.store.store.ttl(this.key(key));
|
|
97
|
-
}
|
|
98
|
-
async setValueInObject(key, objectKey, value) {
|
|
99
|
-
const object = await this.get(key) || {};
|
|
100
|
-
object[objectKey] = value;
|
|
101
|
-
await this.set(key, object);
|
|
102
|
-
}
|
|
103
|
-
async getValueInObject(key, objectKey) {
|
|
104
|
-
const object = await this.get(key) || {};
|
|
105
|
-
return object[objectKey];
|
|
106
|
-
}
|
|
107
|
-
async delValueInObject(key, objectKey) {
|
|
108
|
-
const object = await this.get(key) || {};
|
|
109
|
-
delete object[objectKey];
|
|
110
|
-
await this.set(key, object);
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
__name(_Cache, "Cache");
|
|
114
|
-
let Cache = _Cache;
|
|
115
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
116
|
-
0 && (module.exports = {
|
|
117
|
-
Cache
|
|
118
|
-
});
|
package/lib/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
export * from './cache-manager';
|
|
10
|
-
export * from './cache';
|
|
11
|
-
export * from './bloom-filter';
|
package/lib/index.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
var __defProp = Object.defineProperty;
|
|
11
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
-
var __copyProps = (to, from, except, desc) => {
|
|
15
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
-
for (let key of __getOwnPropNames(from))
|
|
17
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
-
}
|
|
20
|
-
return to;
|
|
21
|
-
};
|
|
22
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
23
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
-
var src_exports = {};
|
|
25
|
-
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
__reExport(src_exports, require("./cache-manager"), module.exports);
|
|
27
|
-
__reExport(src_exports, require("./cache"), module.exports);
|
|
28
|
-
__reExport(src_exports, require("./bloom-filter"), module.exports);
|
|
29
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
-
0 && (module.exports = {
|
|
31
|
-
...require("./cache-manager"),
|
|
32
|
-
...require("./cache"),
|
|
33
|
-
...require("./bloom-filter")
|
|
34
|
-
});
|