From 52d59981768057d7c64fc0d3e5b212c5000ee56c Mon Sep 17 00:00:00 2001 From: Tim Hansen Date: Wed, 17 May 2023 10:00:53 -0600 Subject: [PATCH 1/4] 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; - } -} From 24e648f056c047cfa1de87e74d716d2e7a0f5d30 Mon Sep 17 00:00:00 2001 From: Tim Hansen Date: Wed, 17 May 2023 10:14:35 -0600 Subject: [PATCH 2/4] Remove cache configuration; no longer needed Signed-off-by: Tim Hansen --- .github/uffizzi/uffizzi.production.app-config.yaml | 2 -- app-config.yaml | 2 -- packages/create-app/templates/default-app/app-config.yaml.hbs | 2 -- 3 files changed, 6 deletions(-) diff --git a/.github/uffizzi/uffizzi.production.app-config.yaml b/.github/uffizzi/uffizzi.production.app-config.yaml index 9fcc6062c6..77b093e024 100644 --- a/.github/uffizzi/uffizzi.production.app-config.yaml +++ b/.github/uffizzi/uffizzi.production.app-config.yaml @@ -20,8 +20,6 @@ backend: port: ${POSTGRES_PORT} user: ${POSTGRES_USER} password: ${POSTGRES_PASSWORD} - cache: - store: memory cors: origin: ${UFFIZZI_URL} methods: [GET, HEAD, PATCH, POST, PUT, DELETE] diff --git a/app-config.yaml b/app-config.yaml index 7b1b28bb08..a4a332efdc 100644 --- a/app-config.yaml +++ b/app-config.yaml @@ -33,8 +33,6 @@ backend: database: client: better-sqlite3 connection: ':memory:' - cache: - store: memory cors: origin: http://localhost:3000 methods: [GET, HEAD, PATCH, POST, PUT, DELETE] diff --git a/packages/create-app/templates/default-app/app-config.yaml.hbs b/packages/create-app/templates/default-app/app-config.yaml.hbs index 3d216ec894..194549f079 100644 --- a/packages/create-app/templates/default-app/app-config.yaml.hbs +++ b/packages/create-app/templates/default-app/app-config.yaml.hbs @@ -30,8 +30,6 @@ backend: database: client: better-sqlite3 connection: ':memory:' - cache: - store: memory # workingDirectory: /tmp # Use this to configure a working directory for the scaffolder, defaults to the OS temp-dir integrations: From 23612c55e223e1e54e5d3c573effc06237739726 Mon Sep 17 00:00:00 2001 From: Tim Hansen Date: Wed, 17 May 2023 10:20:03 -0600 Subject: [PATCH 3/4] Update changelog Signed-off-by: Tim Hansen --- .changeset/dirty-eels-poke.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.changeset/dirty-eels-poke.md b/.changeset/dirty-eels-poke.md index e2e3fa5066..2caae0eb16 100644 --- a/.changeset/dirty-eels-poke.md +++ b/.changeset/dirty-eels-poke.md @@ -3,3 +3,11 @@ --- Changed the default backend CacheClient to an in-memory cache when not explicitly configured. + +Explicit configuration of an **in-memory cache** can be removed from `app-config.yaml`, as this is now the default: + +```diff +backend: +- cache: +- store: memory +``` From bf347b0747fe821633ee0cc97e82a0e246e143ea Mon Sep 17 00:00:00 2001 From: Tim Hansen Date: Fri, 16 Jun 2023 08:18:29 -0600 Subject: [PATCH 4/4] Add create-app to changeset Signed-off-by: Tim Hansen --- .changeset/dirty-eels-poke.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/dirty-eels-poke.md b/.changeset/dirty-eels-poke.md index 2caae0eb16..4986716dd9 100644 --- a/.changeset/dirty-eels-poke.md +++ b/.changeset/dirty-eels-poke.md @@ -1,5 +1,6 @@ --- '@backstage/backend-common': patch +'@backstage/create-app': patch --- Changed the default backend CacheClient to an in-memory cache when not explicitly configured.