From 52d59981768057d7c64fc0d3e5b212c5000ee56c Mon Sep 17 00:00:00 2001 From: Tim Hansen Date: Wed, 17 May 2023 10:00:53 -0600 Subject: [PATCH] Default CacheClient to in-memory Signed-off-by: Tim Hansen --- .changeset/dirty-eels-poke.md | 5 +++ .../src/cache/CacheManager.test.ts | 13 ++++-- .../backend-common/src/cache/CacheManager.ts | 13 +----- packages/backend-common/src/cache/NoStore.ts | 43 ------------------- 4 files changed, 16 insertions(+), 58 deletions(-) create mode 100644 .changeset/dirty-eels-poke.md delete mode 100644 packages/backend-common/src/cache/NoStore.ts diff --git a/.changeset/dirty-eels-poke.md b/.changeset/dirty-eels-poke.md new file mode 100644 index 0000000000..e2e3fa5066 --- /dev/null +++ b/.changeset/dirty-eels-poke.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-common': patch +--- + +Changed the default backend CacheClient to an in-memory cache when not explicitly configured. diff --git a/packages/backend-common/src/cache/CacheManager.test.ts b/packages/backend-common/src/cache/CacheManager.test.ts index 3239be4d0e..4578d4fed9 100644 --- a/packages/backend-common/src/cache/CacheManager.test.ts +++ b/packages/backend-common/src/cache/CacheManager.test.ts @@ -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', () => { diff --git a/packages/backend-common/src/cache/CacheManager.ts b/packages/backend-common/src/cache/CacheManager.ts index 12ece42854..9101445b08 100644 --- a/packages/backend-common/src/cache/CacheManager.ts +++ b/packages/backend-common/src/cache/CacheManager.ts @@ -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 */ diff --git a/packages/backend-common/src/cache/NoStore.ts b/packages/backend-common/src/cache/NoStore.ts deleted file mode 100644 index 85f789d21e..0000000000 --- a/packages/backend-common/src/cache/NoStore.ts +++ /dev/null @@ -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 { - 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; - } -}