diff --git a/.changeset/dirty-eels-poke.md b/.changeset/dirty-eels-poke.md new file mode 100644 index 0000000000..4986716dd9 --- /dev/null +++ b/.changeset/dirty-eels-poke.md @@ -0,0 +1,14 @@ +--- +'@backstage/backend-common': patch +'@backstage/create-app': patch +--- + +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 +``` 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/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; - } -} 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: