Skip to content

Commit ee20ba7

Browse files
committed
refactor: Move cache key prefix into CMCache
* Makes adding prefix cache provider agnostic * Prepares for #115
1 parent f71933b commit ee20ba7

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

src/Bot/ResourcesManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export class BotResourcesManager {
147147
} else if(res !== undefined && res.cache.equalProvider(candidateProvider)) {
148148
opts.cache = res.cache;
149149
} else {
150-
opts.cache = new CMCache(createCacheManager(candidateProvider), candidateProvider, false, opts.ttl, this.logger, candidateProvider.prefix);
150+
opts.cache = new CMCache(createCacheManager(candidateProvider), candidateProvider, false, opts.ttl, this.logger);
151151
await runMigrations(opts.cache.cache, opts.cache.logger, candidateProvider.prefix);
152152
}
153153

src/Common/Cache/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const buildCacheOptionsFromProvider = (provider: CacheProvider | any): Ca
2323
}
2424
}
2525
export const createCacheManager = (options: CacheOptions): Cache => {
26-
const {store, max, ttl = 60, host = 'localhost', port, auth_pass, db, ...rest} = options;
26+
const {store, max, ttl = 60, host = 'localhost', port, auth_pass, db, prefix, ...rest} = options;
2727
switch (store) {
2828
case 'none':
2929
return cacheManager.caching({store: 'none', max, ttl});
@@ -52,11 +52,11 @@ export class CMCache {
5252
providerOptions: CacheOptions;
5353
logger!: Logger;
5454

55-
constructor(cache: Cache, providerOptions: CacheOptions, defaultCache: boolean, ttls: Partial<StrongTTLConfig>, logger: Logger, prefix?: string) {
55+
constructor(cache: Cache, providerOptions: CacheOptions, defaultCache: boolean, ttls: Partial<StrongTTLConfig>, logger: Logger) {
5656
this.cache = cache;
5757
this.providerOptions = providerOptions
58-
this.prefix = prefix;
5958
this.isDefaultCache = defaultCache;
59+
this.prefix = this.providerOptions.prefix ?? '';
6060

6161
this.setLogger(logger);
6262

@@ -176,22 +176,23 @@ export class CMCache {
176176
}
177177

178178
del(key: string): Promise<any> {
179-
return this.cache.del(key);
179+
return this.cache.del(`${this.prefix}${key}`);
180180
}
181181

182182
get<T>(key: string): Promise<T | undefined> {
183-
return this.cache.get(key);
183+
return this.cache.get(`${this.prefix}${key}`);
184184
}
185185

186186
reset(): Promise<void> {
187187
return this.cache.reset();
188188
}
189189

190190
set<T>(key: string, value: T, options?: CachingConfig): Promise<T> {
191-
return this.cache.set(key, value, options);
191+
return this.cache.set(`${this.prefix}${key}`, value, options);
192192
}
193193

194194
wrap<T>(...args: WrapArgsType<T>[]): Promise<T> {
195+
args[0] = `${this.prefix}${args[0]}`;
195196
return this.cache.wrap(...args);
196197
}
197198

src/Common/interfaces.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,11 @@ export interface CacheOptions {
791791
* */
792792
max?: number
793793

794+
/**
795+
* A prefix to add to all keys
796+
* */
797+
prefix?: string
798+
794799
[key:string]: any
795800
}
796801

src/Schema/App.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,10 @@
11811181
],
11821182
"type": "number"
11831183
},
1184+
"prefix": {
1185+
"description": "A prefix to add to all keys",
1186+
"type": "string"
1187+
},
11841188
"store": {
11851189
"$ref": "#/definitions/CacheProvider"
11861190
},

src/Schema/OperatorConfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,10 @@
474474
],
475475
"type": "number"
476476
},
477+
"prefix": {
478+
"description": "A prefix to add to all keys",
479+
"type": "string"
480+
},
477481
"store": {
478482
"$ref": "#/definitions/CacheProvider"
479483
},

0 commit comments

Comments
 (0)