Default CacheClient to in-memory

Signed-off-by: Tim Hansen <timbonicush@spotify.com>
This commit is contained in:
Tim Hansen
2023-05-17 10:00:53 -06:00
parent cf9b4ce59d
commit 52d5998176
4 changed files with 16 additions and 58 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': patch
---
Changed the default backend CacheClient to an in-memory cache when not explicitly configured.
+9 -4
View File
@@ -20,7 +20,6 @@ import KeyvMemcache from '@keyv/memcache';
import KeyvRedis from '@keyv/redis';
import { DefaultCacheClient } from './CacheClient';
import { CacheManager } from './CacheManager';
import { NoStore } from './NoStore';
jest.createMockFromModule('keyv');
jest.mock('keyv');
@@ -119,17 +118,23 @@ describe('CacheManager', () => {
});
describe('CacheManager.forPlugin stores', () => {
it('returns none client when no cache is configured', () => {
it('returns memory client when no cache is configured', () => {
const manager = CacheManager.fromConfig(
new ConfigReader({ backend: {} }),
);
const expectedTtl = 3600;
const expectedNamespace = 'test-plugin';
manager.forPlugin(expectedNamespace).getClient();
manager
.forPlugin(expectedNamespace)
.getClient({ defaultTtl: expectedTtl });
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);
expect(callArgs[0]).toMatchObject({
ttl: expectedTtl,
namespace: expectedNamespace,
});
});
it('returns memory client when explicitly configured', () => {
+2 -11
View File
@@ -25,7 +25,6 @@ import {
} from '@backstage/backend-plugin-api';
import { getRootLogger } from '../logging';
import { DefaultCacheClient } from './CacheClient';
import { NoStore } from './NoStore';
import { CacheManagerOptions, PluginCacheManager } from './types';
/**
@@ -44,7 +43,6 @@ export class CacheManager {
redis: this.getRedisClient,
memcache: this.getMemcacheClient,
memory: this.getMemoryClient,
none: this.getNoneClient,
};
/**
@@ -70,8 +68,8 @@ export class CacheManager {
options: CacheManagerOptions = {},
): CacheManager {
// If no `backend.cache` config is provided, instantiate the CacheManager
// with a "NoStore" cache client.
const store = config.getOptionalString('backend.cache.store') || 'none';
// with an in-memory cache client.
const store = config.getOptionalString('backend.cache.store') || 'memory';
const connectionString =
config.getOptionalString('backend.cache.connection') || '';
const logger = (options.logger || getRootLogger()).child({
@@ -169,13 +167,6 @@ export class CacheManager {
store: this.memoryStore,
});
}
private getNoneClient(pluginId: string): Keyv {
return new Keyv({
namespace: pluginId,
store: new NoStore(),
});
}
}
/** @public */
-43
View File
@@ -1,43 +0,0 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Store } from 'keyv';
/**
* Storage class compatible with Keyv which always results in a no-op. This is
* used when no cache store is configured in a Backstage backend instance.
*/
export class NoStore implements Store<string | undefined> {
clear(): void {
return;
}
delete(_key: string): boolean {
return false;
}
get(_key: string) {
return undefined;
}
has(_key: string): boolean {
return false;
}
set(_key: string, _value: any): this {
return this;
}
}