@nocobase/cache
Advanced tools
Comparing version 1.6.0-beta.3 to 1.6.0-beta.4
@@ -9,3 +9,2 @@ /** | ||
*/ | ||
import { RedisStore } from 'cache-manager-redis-yet'; | ||
import { BloomFilter } from '.'; | ||
@@ -19,3 +18,3 @@ import { Cache } from '../cache'; | ||
constructor(cache: Cache); | ||
getStore(): RedisStore<import("redis").RedisClientType>; | ||
private get store(); | ||
reserve(key: string, errorRate: number, capacity: number): Promise<void>; | ||
@@ -22,0 +21,0 @@ add(key: string, value: string): Promise<void>; |
@@ -39,20 +39,23 @@ /** | ||
} | ||
getStore() { | ||
get store() { | ||
return this.cache.store.store; | ||
} | ||
async reserve(key, errorRate, capacity) { | ||
const store = this.getStore(); | ||
await store.client.bf.reserve(key, errorRate, capacity); | ||
try { | ||
await this.store.client.bf.reserve(key, errorRate, capacity); | ||
} catch (error) { | ||
if (error.message.includes("ERR item exists")) { | ||
return; | ||
} | ||
throw error; | ||
} | ||
} | ||
async add(key, value) { | ||
const store = this.getStore(); | ||
await store.client.bf.add(key, value); | ||
await this.store.client.bf.add(key, value); | ||
} | ||
async mAdd(key, values) { | ||
const store = this.getStore(); | ||
await store.client.bf.mAdd(key, values); | ||
await this.store.client.bf.mAdd(key, values); | ||
} | ||
async exists(key, value) { | ||
const store = this.getStore(); | ||
return await store.client.bf.exists(key, value); | ||
return this.store.client.bf.exists(key, value); | ||
} | ||
@@ -59,0 +62,0 @@ }; |
@@ -12,2 +12,4 @@ /** | ||
import { BloomFilter } from './bloom-filter'; | ||
import { Counter } from './counter'; | ||
import { LockManager } from '@nocobase/lock-manager'; | ||
type StoreOptions = { | ||
@@ -23,5 +25,7 @@ store?: 'memory' | FactoryStore<Store, any>; | ||
}; | ||
prefix: string; | ||
}>; | ||
export declare class CacheManager { | ||
defaultStore: string; | ||
prefix?: string; | ||
private stores; | ||
@@ -57,3 +61,11 @@ /** | ||
}): Promise<BloomFilter>; | ||
/** | ||
* @experimental | ||
*/ | ||
createCounter(options: { | ||
name: string; | ||
prefix?: string; | ||
store?: string; | ||
}, lockManager?: LockManager): Promise<Counter>; | ||
} | ||
export {}; |
@@ -50,4 +50,6 @@ /** | ||
var import_redis_bloom_filter = require("./bloom-filter/redis-bloom-filter"); | ||
var import_counter = require("./counter"); | ||
const _CacheManager = class _CacheManager { | ||
defaultStore; | ||
prefix; | ||
stores = /* @__PURE__ */ new Map(); | ||
@@ -84,4 +86,5 @@ /** | ||
const cacheOptions = (0, import_deepmerge.default)(defaultOptions, options || {}); | ||
const { defaultStore = "memory", stores } = cacheOptions; | ||
const { defaultStore = "memory", stores, prefix } = cacheOptions; | ||
this.defaultStore = defaultStore; | ||
this.prefix = prefix; | ||
for (const [name, store] of Object.entries(stores)) { | ||
@@ -114,3 +117,5 @@ const { store: s, ...globalConfig } = store; | ||
async createCache(options) { | ||
const { name, prefix, store = this.defaultStore, ...config } = options; | ||
const { name, store = this.defaultStore, ...config } = options; | ||
let { prefix } = options; | ||
prefix = this.prefix ? prefix ? `${this.prefix}:${prefix}` : this.prefix : prefix; | ||
if (!import_lodash.default.isEmpty(config) || store === "memory") { | ||
@@ -170,2 +175,27 @@ const newStore = await this.createStore({ name, storeType: store, ...config }); | ||
} | ||
/** | ||
* @experimental | ||
*/ | ||
async createCounter(options, lockManager) { | ||
const { store = this.defaultStore, name, prefix } = options || {}; | ||
let cache; | ||
if (store !== "memory") { | ||
try { | ||
cache = this.getCache(name); | ||
} catch (error) { | ||
cache = await this.createCache({ name, store, prefix }); | ||
} | ||
} | ||
switch (store) { | ||
case "memory": | ||
return new import_counter.MemoryCounter(); | ||
case "redis": | ||
return new import_counter.RedisCounter(cache); | ||
default: | ||
if (!lockManager) { | ||
throw new Error(`Counter store [${store}] is not supported`); | ||
} | ||
return new import_counter.LockCounter(cache, lockManager); | ||
} | ||
} | ||
}; | ||
@@ -172,0 +202,0 @@ __name(_CacheManager, "CacheManager"); |
@@ -12,1 +12,2 @@ /** | ||
export * from './bloom-filter'; | ||
export * from './counter'; |
@@ -29,2 +29,3 @@ /** | ||
__reExport(src_exports, require("./bloom-filter"), module.exports); | ||
__reExport(src_exports, require("./counter"), module.exports); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -34,3 +35,4 @@ 0 && (module.exports = { | ||
...require("./cache"), | ||
...require("./bloom-filter") | ||
...require("./bloom-filter"), | ||
...require("./counter") | ||
}); |
{ | ||
"name": "@nocobase/cache", | ||
"version": "1.6.0-beta.3", | ||
"version": "1.6.0-beta.4", | ||
"description": "", | ||
@@ -9,2 +9,3 @@ "license": "AGPL-3.0", | ||
"dependencies": { | ||
"@nocobase/lock-manager": "1.6.0-alpha.6", | ||
"bloom-filters": "^3.0.1", | ||
@@ -22,3 +23,3 @@ "cache-manager": "^5.2.4", | ||
}, | ||
"gitHead": "19a659c49d7eedd3d57e46f9df2e8540f55c99b7" | ||
"gitHead": "4419f433716dadf34886b261d9abe20e74551044" | ||
} |
71909
23
1060
4
+ Added@nocobase/lock-manager@1.6.0-alpha.6(transitive)