Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
Eric Peterson
2021-05-09 18:08:57 +02:00
parent d48b375a81
commit 03ae33c064
4 changed files with 27 additions and 15 deletions
+6 -3
View File
@@ -46,7 +46,11 @@ export interface CacheClient {
* optional TTL may also be provided, otherwise it defaults to the TTL that
* was provided when the client was instantiated.
*/
set(key: string, value: JsonValue, options?: CacheSetOptions): Promise<boolean>;
set(
key: string,
value: JsonValue,
options?: CacheSetOptions,
): Promise<boolean>;
/**
* Removes the given key from the cache store. Resolves true if the key
@@ -77,7 +81,7 @@ export class DefaultCacheClient implements CacheClient {
opts: CacheSetOptions = {},
): Promise<boolean> {
const k = this.getNormalizedKey(key);
return await this.client.set(k, value, opts.ttl)
return await this.client.set(k, value, opts.ttl);
}
async delete(key: string): Promise<boolean> {
@@ -99,5 +103,4 @@ export class DefaultCacheClient implements CacheClient {
return createHash('md5').update(candidateKey).digest('base64');
}
}
+10 -6
View File
@@ -53,7 +53,9 @@ describe('CacheManager', () => {
CacheManager.fromConfig(config);
expect(getOptionalString.mock.calls[0][0]).toEqual('backend.cache.store');
expect(getOptionalString.mock.calls[1][0]).toEqual('backend.cache.connection');
expect(getOptionalString.mock.calls[1][0]).toEqual(
'backend.cache.connection',
);
});
it('does not require the backend.cache key', () => {
@@ -92,7 +94,7 @@ describe('CacheManager', () => {
manager.forPlugin(plugin2Id).getClient({ defaultTtl: expectedTtl });
const client = DefaultCacheClient as jest.Mock;
const cache = Keyv as unknown as jest.Mock;
const cache = (Keyv as unknown) as jest.Mock;
expect(cache).toHaveBeenCalledTimes(2);
expect(client).toHaveBeenCalledTimes(2);
@@ -112,7 +114,7 @@ describe('CacheManager', () => {
const expectedNamespace = 'test-plugin';
manager.forPlugin(expectedNamespace).getClient();
const cache = Keyv as unknown as jest.Mock;
const cache = (Keyv as unknown) as jest.Mock;
const mockCalls = cache.mock.calls.splice(-1);
const callArgs = mockCalls[0];
expect(callArgs[0].store).toBeInstanceOf(NoStore);
@@ -122,9 +124,11 @@ describe('CacheManager', () => {
const manager = CacheManager.fromConfig(defaultConfig());
const expectedTtl = 3600;
const expectedNamespace = 'test-plugin';
manager.forPlugin(expectedNamespace).getClient({ defaultTtl: expectedTtl });
manager
.forPlugin(expectedNamespace)
.getClient({ defaultTtl: expectedTtl });
const cache = Keyv as unknown as jest.Mock;
const cache = (Keyv as unknown) as jest.Mock;
const mockCalls = cache.mock.calls.splice(-1);
const callArgs = mockCalls[0];
expect(callArgs[0]).toMatchObject({
@@ -148,7 +152,7 @@ describe('CacheManager', () => {
const expectedTtl = 3600;
manager.forPlugin('test').getClient({ defaultTtl: expectedTtl });
const cache = Keyv as unknown as jest.Mock;
const cache = (Keyv as unknown) as jest.Mock;
const mockCacheCalls = cache.mock.calls.splice(-1);
expect(mockCacheCalls[0][0]).toMatchObject({
ttl: expectedTtl,
+11 -4
View File
@@ -51,7 +51,8 @@ export class CacheManager {
// If no `backend.cache` config is provided, instantiate the CacheManager
// with a "NoStore" cache client.
const store = config.getOptionalString('backend.cache.store') || 'none';
const connectionString = config.getOptionalString('backend.cache.connection') || '';
const connectionString =
config.getOptionalString('backend.cache.connection') || '';
return new CacheManager(store, connectionString);
}
@@ -83,7 +84,10 @@ export class CacheManager {
return this.storeFactories[this.store].call(this, pluginId, ttl);
}
private getMemcacheClient(pluginId: string, defaultTtl: number | undefined): Keyv {
private getMemcacheClient(
pluginId: string,
defaultTtl: number | undefined,
): Keyv {
return new Keyv({
namespace: pluginId,
ttl: defaultTtl,
@@ -91,7 +95,10 @@ export class CacheManager {
});
}
private getMemoryClient(pluginId: string, defaultTtl: number | undefined): Keyv {
private getMemoryClient(
pluginId: string,
defaultTtl: number | undefined,
): Keyv {
return new Keyv({
namespace: pluginId,
ttl: defaultTtl,
@@ -102,6 +109,6 @@ export class CacheManager {
return new Keyv({
namespace: pluginId,
store: new NoStore(),
})
});
}
}
-2
View File
@@ -19,7 +19,6 @@
* used when no cache store is configured in a Backstage backend instance.
*/
export class NoStore extends Map<string, any> {
clear(): void {
return;
}
@@ -39,5 +38,4 @@ export class NoStore extends Map<string, any> {
set(_key: string, _value: any): this {
return this;
}
}