@backstage/backend-defaults 0.2.19-next.0 → 0.3.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,38 @@
1
1
  # @backstage/backend-defaults
2
2
 
3
+ ## 0.3.0-next.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 0634fdc: Deprecated `dropDatabase`
8
+ - Updated dependencies
9
+ - @backstage/backend-plugin-api@0.6.19-next.2
10
+ - @backstage/backend-common@0.23.0-next.2
11
+ - @backstage/plugin-permission-node@0.7.30-next.2
12
+ - @backstage/backend-app-api@0.7.6-next.2
13
+ - @backstage/plugin-events-node@0.3.5-next.1
14
+ - @backstage/config-loader@1.8.0
15
+ - @backstage/backend-dev-utils@0.1.4
16
+ - @backstage/config@1.2.0
17
+ - @backstage/errors@1.2.4
18
+ - @backstage/types@1.1.1
19
+
20
+ ## 0.3.0-next.1
21
+
22
+ ### Minor Changes
23
+
24
+ - 02103be: Deprecated and moved over core services to `@backstage/backend-defaults`
25
+
26
+ ### Patch Changes
27
+
28
+ - Updated dependencies
29
+ - @backstage/backend-app-api@0.7.6-next.1
30
+ - @backstage/backend-plugin-api@0.6.19-next.1
31
+ - @backstage/plugin-permission-node@0.7.30-next.1
32
+ - @backstage/backend-common@0.23.0-next.1
33
+ - @backstage/config-loader@1.8.0
34
+ - @backstage/plugin-events-node@0.3.5-next.0
35
+
3
36
  ## 0.2.19-next.0
4
37
 
5
38
  ### Patch Changes
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/backend-defaults",
3
- "version": "0.2.19-next.0",
3
+ "version": "0.3.0-next.2",
4
4
  "main": "../dist/cache.cjs.js",
5
5
  "types": "../dist/cache.d.ts"
6
6
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/backend-defaults",
3
- "version": "0.2.19-next.0",
3
+ "version": "0.3.0-next.2",
4
4
  "main": "../dist/database.cjs.js",
5
5
  "types": "../dist/database.d.ts"
6
6
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/backend-defaults",
3
- "version": "0.2.19-next.0",
3
+ "version": "0.3.0-next.2",
4
4
  "main": "../dist/discovery.cjs.js",
5
5
  "types": "../dist/discovery.d.ts"
6
6
  }
package/dist/cache.cjs.js CHANGED
@@ -1,21 +1,193 @@
1
1
  'use strict';
2
2
 
3
- var backendCommon = require('@backstage/backend-common');
4
3
  var backendPluginApi = require('@backstage/backend-plugin-api');
4
+ var Keyv = require('keyv');
5
+ var crypto = require('crypto');
6
+
7
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
8
+
9
+ var Keyv__default = /*#__PURE__*/_interopDefaultCompat(Keyv);
10
+
11
+ class DefaultCacheClient {
12
+ #client;
13
+ #clientFactory;
14
+ #options;
15
+ constructor(client, clientFactory, options) {
16
+ this.#client = client;
17
+ this.#clientFactory = clientFactory;
18
+ this.#options = options;
19
+ }
20
+ async get(key) {
21
+ const k = this.getNormalizedKey(key);
22
+ const value = await this.#client.get(k);
23
+ return value;
24
+ }
25
+ async set(key, value, opts = {}) {
26
+ const k = this.getNormalizedKey(key);
27
+ await this.#client.set(k, value, opts.ttl);
28
+ }
29
+ async delete(key) {
30
+ const k = this.getNormalizedKey(key);
31
+ await this.#client.delete(k);
32
+ }
33
+ withOptions(options) {
34
+ const newOptions = { ...this.#options, ...options };
35
+ return new DefaultCacheClient(
36
+ this.#clientFactory(newOptions),
37
+ this.#clientFactory,
38
+ newOptions
39
+ );
40
+ }
41
+ /**
42
+ * Ensures keys are well-formed for any/all cache stores.
43
+ */
44
+ getNormalizedKey(candidateKey) {
45
+ const wellFormedKey = Buffer.from(candidateKey).toString("base64");
46
+ if (wellFormedKey.length < 200) {
47
+ return wellFormedKey;
48
+ }
49
+ return crypto.createHash("sha256").update(candidateKey).digest("base64");
50
+ }
51
+ }
52
+
53
+ class CacheManager {
54
+ /**
55
+ * Keys represent supported `backend.cache.store` values, mapped to factories
56
+ * that return Keyv instances appropriate to the store.
57
+ */
58
+ storeFactories = {
59
+ redis: this.createRedisStoreFactory(),
60
+ memcache: this.createMemcacheStoreFactory(),
61
+ memory: this.createMemoryStoreFactory()
62
+ };
63
+ logger;
64
+ store;
65
+ connection;
66
+ useRedisSets;
67
+ errorHandler;
68
+ defaultTtl;
69
+ /**
70
+ * Creates a new {@link CacheManager} instance by reading from the `backend`
71
+ * config section, specifically the `.cache` key.
72
+ *
73
+ * @param config - The loaded application configuration.
74
+ */
75
+ static fromConfig(config, options = {}) {
76
+ const store = config.getOptionalString("backend.cache.store") || "memory";
77
+ const defaultTtl = config.getOptionalNumber("backend.cache.defaultTtl");
78
+ const connectionString = config.getOptionalString("backend.cache.connection") || "";
79
+ const useRedisSets = config.getOptionalBoolean("backend.cache.useRedisSets") ?? true;
80
+ const logger = options.logger?.child({
81
+ type: "cacheManager"
82
+ });
83
+ return new CacheManager(
84
+ store,
85
+ connectionString,
86
+ useRedisSets,
87
+ options.onError,
88
+ logger,
89
+ defaultTtl
90
+ );
91
+ }
92
+ /** @internal */
93
+ constructor(store, connectionString, useRedisSets, errorHandler, logger, defaultTtl) {
94
+ if (!this.storeFactories.hasOwnProperty(store)) {
95
+ throw new Error(`Unknown cache store: ${store}`);
96
+ }
97
+ this.logger = logger;
98
+ this.store = store;
99
+ this.connection = connectionString;
100
+ this.useRedisSets = useRedisSets;
101
+ this.errorHandler = errorHandler;
102
+ this.defaultTtl = defaultTtl;
103
+ }
104
+ /**
105
+ * Generates a PluginCacheManager for consumption by plugins.
106
+ *
107
+ * @param pluginId - The plugin that the cache manager should be created for.
108
+ * Plugin names should be unique.
109
+ */
110
+ forPlugin(pluginId) {
111
+ return {
112
+ getClient: (defaultOptions = {}) => {
113
+ const clientFactory = (options) => {
114
+ const concreteClient = this.getClientWithTtl(
115
+ pluginId,
116
+ options.defaultTtl ?? this.defaultTtl
117
+ );
118
+ concreteClient.on("error", (err) => {
119
+ this.logger?.error("Failed to create cache client", err);
120
+ if (typeof this.errorHandler === "function") {
121
+ this.errorHandler(err);
122
+ }
123
+ });
124
+ return concreteClient;
125
+ };
126
+ return new DefaultCacheClient(
127
+ clientFactory(defaultOptions),
128
+ clientFactory,
129
+ defaultOptions
130
+ );
131
+ }
132
+ };
133
+ }
134
+ getClientWithTtl(pluginId, ttl) {
135
+ return this.storeFactories[this.store](pluginId, ttl);
136
+ }
137
+ createRedisStoreFactory() {
138
+ const KeyvRedis = require("@keyv/redis");
139
+ let store;
140
+ return (pluginId, defaultTtl) => {
141
+ if (!store) {
142
+ store = new KeyvRedis(this.connection);
143
+ }
144
+ return new Keyv__default.default({
145
+ namespace: pluginId,
146
+ ttl: defaultTtl,
147
+ store,
148
+ useRedisSets: this.useRedisSets
149
+ });
150
+ };
151
+ }
152
+ createMemcacheStoreFactory() {
153
+ const KeyvMemcache = require("@keyv/memcache");
154
+ let store;
155
+ return (pluginId, defaultTtl) => {
156
+ if (!store) {
157
+ store = new KeyvMemcache(this.connection);
158
+ }
159
+ return new Keyv__default.default({
160
+ namespace: pluginId,
161
+ ttl: defaultTtl,
162
+ store
163
+ });
164
+ };
165
+ }
166
+ createMemoryStoreFactory() {
167
+ const store = /* @__PURE__ */ new Map();
168
+ return (pluginId, defaultTtl) => new Keyv__default.default({
169
+ namespace: pluginId,
170
+ ttl: defaultTtl,
171
+ store
172
+ });
173
+ }
174
+ }
5
175
 
6
176
  const cacheServiceFactory = backendPluginApi.createServiceFactory({
7
177
  service: backendPluginApi.coreServices.cache,
8
178
  deps: {
9
179
  config: backendPluginApi.coreServices.rootConfig,
10
- plugin: backendPluginApi.coreServices.pluginMetadata
180
+ plugin: backendPluginApi.coreServices.pluginMetadata,
181
+ logger: backendPluginApi.coreServices.rootLogger
11
182
  },
12
- async createRootContext({ config }) {
13
- return backendCommon.CacheManager.fromConfig(config);
183
+ async createRootContext({ config, logger }) {
184
+ return CacheManager.fromConfig(config, { logger });
14
185
  },
15
186
  async factory({ plugin }, manager) {
16
187
  return manager.forPlugin(plugin.getId()).getClient();
17
188
  }
18
189
  });
19
190
 
191
+ exports.CacheManager = CacheManager;
20
192
  exports.cacheServiceFactory = cacheServiceFactory;
21
193
  //# sourceMappingURL=cache.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache.cjs.js","sources":["../src/entrypoints/cache/cacheServiceFactory.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { CacheManager } from '@backstage/backend-common';\nimport {\n coreServices,\n createServiceFactory,\n} from '@backstage/backend-plugin-api';\n\n/**\n * @public\n */\nexport const cacheServiceFactory = createServiceFactory({\n service: coreServices.cache,\n deps: {\n config: coreServices.rootConfig,\n plugin: coreServices.pluginMetadata,\n },\n async createRootContext({ config }) {\n return CacheManager.fromConfig(config);\n },\n async factory({ plugin }, manager) {\n return manager.forPlugin(plugin.getId()).getClient();\n },\n});\n"],"names":["createServiceFactory","coreServices","CacheManager"],"mappings":";;;;;AAyBO,MAAM,sBAAsBA,qCAAqB,CAAA;AAAA,EACtD,SAASC,6BAAa,CAAA,KAAA;AAAA,EACtB,IAAM,EAAA;AAAA,IACJ,QAAQA,6BAAa,CAAA,UAAA;AAAA,IACrB,QAAQA,6BAAa,CAAA,cAAA;AAAA,GACvB;AAAA,EACA,MAAM,iBAAA,CAAkB,EAAE,MAAA,EAAU,EAAA;AAClC,IAAO,OAAAC,0BAAA,CAAa,WAAW,MAAM,CAAA,CAAA;AAAA,GACvC;AAAA,EACA,MAAM,OAAA,CAAQ,EAAE,MAAA,IAAU,OAAS,EAAA;AACjC,IAAA,OAAO,QAAQ,SAAU,CAAA,MAAA,CAAO,KAAM,EAAC,EAAE,SAAU,EAAA,CAAA;AAAA,GACrD;AACF,CAAC;;;;"}
1
+ {"version":3,"file":"cache.cjs.js","sources":["../src/entrypoints/cache/CacheClient.ts","../src/entrypoints/cache/CacheManager.ts","../src/entrypoints/cache/cacheServiceFactory.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n CacheService,\n CacheServiceOptions,\n CacheServiceSetOptions,\n} from '@backstage/backend-plugin-api';\nimport { JsonValue } from '@backstage/types';\nimport { createHash } from 'crypto';\nimport Keyv from 'keyv';\n\nexport type CacheClientFactory = (options: CacheServiceOptions) => Keyv;\n\n/**\n * A basic, concrete implementation of the CacheService, suitable for almost\n * all uses in Backstage.\n */\nexport class DefaultCacheClient implements CacheService {\n #client: Keyv;\n #clientFactory: CacheClientFactory;\n #options: CacheServiceOptions;\n\n constructor(\n client: Keyv,\n clientFactory: CacheClientFactory,\n options: CacheServiceOptions,\n ) {\n this.#client = client;\n this.#clientFactory = clientFactory;\n this.#options = options;\n }\n\n async get<TValue extends JsonValue>(\n key: string,\n ): Promise<TValue | undefined> {\n const k = this.getNormalizedKey(key);\n const value = await this.#client.get(k);\n return value as TValue | undefined;\n }\n\n async set(\n key: string,\n value: JsonValue,\n opts: CacheServiceSetOptions = {},\n ): Promise<void> {\n const k = this.getNormalizedKey(key);\n await this.#client.set(k, value, opts.ttl);\n }\n\n async delete(key: string): Promise<void> {\n const k = this.getNormalizedKey(key);\n await this.#client.delete(k);\n }\n\n withOptions(options: CacheServiceOptions): CacheService {\n const newOptions = { ...this.#options, ...options };\n return new DefaultCacheClient(\n this.#clientFactory(newOptions),\n this.#clientFactory,\n newOptions,\n );\n }\n\n /**\n * Ensures keys are well-formed for any/all cache stores.\n */\n private getNormalizedKey(candidateKey: string): string {\n // Remove potentially invalid characters.\n const wellFormedKey = Buffer.from(candidateKey).toString('base64');\n\n // Memcache in particular doesn't do well with keys > 250 bytes.\n // Padded because a plugin ID is also prepended to the key.\n if (wellFormedKey.length < 200) {\n return wellFormedKey;\n }\n\n return createHash('sha256').update(candidateKey).digest('base64');\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n CacheService,\n CacheServiceOptions,\n LoggerService,\n} from '@backstage/backend-plugin-api';\nimport { Config } from '@backstage/config';\nimport Keyv from 'keyv';\nimport { DefaultCacheClient } from './CacheClient';\nimport { CacheManagerOptions } from './types';\n\ntype StoreFactory = (pluginId: string, defaultTtl: number | undefined) => Keyv;\n\n/*\n * TODO(freben): This class intentionally inlines the CacheManagerOptions and\n * PluginCacheManager types, to not break the api reports in backend-common\n * which re-exports it. When backend-common is deprecated, we can stop inlining\n * those types.\n */\n\n/**\n * Implements a Cache Manager which will automatically create new cache clients\n * for plugins when requested. All requested cache clients are created with the\n * connection details provided.\n *\n * @public\n */\nexport class CacheManager {\n /**\n * Keys represent supported `backend.cache.store` values, mapped to factories\n * that return Keyv instances appropriate to the store.\n */\n private readonly storeFactories = {\n redis: this.createRedisStoreFactory(),\n memcache: this.createMemcacheStoreFactory(),\n memory: this.createMemoryStoreFactory(),\n };\n\n private readonly logger?: LoggerService;\n private readonly store: keyof CacheManager['storeFactories'];\n private readonly connection: string;\n private readonly useRedisSets: boolean;\n private readonly errorHandler: CacheManagerOptions['onError'];\n private readonly defaultTtl?: number;\n\n /**\n * Creates a new {@link CacheManager} instance by reading from the `backend`\n * config section, specifically the `.cache` key.\n *\n * @param config - The loaded application configuration.\n */\n static fromConfig(\n config: Config,\n options: {\n /**\n * An optional logger for use by the PluginCacheManager.\n */\n logger?: LoggerService;\n\n /**\n * An optional handler for connection errors emitted from the underlying data\n * store.\n */\n onError?: (err: Error) => void;\n } = {},\n ): CacheManager {\n // If no `backend.cache` config is provided, instantiate the CacheManager\n // with an in-memory cache client.\n const store = config.getOptionalString('backend.cache.store') || 'memory';\n const defaultTtl = config.getOptionalNumber('backend.cache.defaultTtl');\n const connectionString =\n config.getOptionalString('backend.cache.connection') || '';\n const useRedisSets =\n config.getOptionalBoolean('backend.cache.useRedisSets') ?? true;\n const logger = options.logger?.child({\n type: 'cacheManager',\n });\n return new CacheManager(\n store,\n connectionString,\n useRedisSets,\n options.onError,\n logger,\n defaultTtl,\n );\n }\n\n /** @internal */\n constructor(\n store: string,\n connectionString: string,\n useRedisSets: boolean,\n errorHandler: CacheManagerOptions['onError'],\n logger?: LoggerService,\n defaultTtl?: number,\n ) {\n if (!this.storeFactories.hasOwnProperty(store)) {\n throw new Error(`Unknown cache store: ${store}`);\n }\n this.logger = logger;\n this.store = store as keyof CacheManager['storeFactories'];\n this.connection = connectionString;\n this.useRedisSets = useRedisSets;\n this.errorHandler = errorHandler;\n this.defaultTtl = defaultTtl;\n }\n\n /**\n * Generates a PluginCacheManager for consumption by plugins.\n *\n * @param pluginId - The plugin that the cache manager should be created for.\n * Plugin names should be unique.\n */\n forPlugin(pluginId: string): {\n getClient(options?: CacheServiceOptions): CacheService;\n } {\n return {\n getClient: (defaultOptions = {}) => {\n const clientFactory = (options: CacheServiceOptions) => {\n const concreteClient = this.getClientWithTtl(\n pluginId,\n options.defaultTtl ?? this.defaultTtl,\n );\n\n // Always provide an error handler to avoid stopping the process.\n concreteClient.on('error', (err: Error) => {\n // In all cases, just log the error.\n this.logger?.error('Failed to create cache client', err);\n\n // Invoke any custom error handler if provided.\n if (typeof this.errorHandler === 'function') {\n this.errorHandler(err);\n }\n });\n\n return concreteClient;\n };\n\n return new DefaultCacheClient(\n clientFactory(defaultOptions),\n clientFactory,\n defaultOptions,\n );\n },\n };\n }\n\n private getClientWithTtl(pluginId: string, ttl: number | undefined): Keyv {\n return this.storeFactories[this.store](pluginId, ttl);\n }\n\n private createRedisStoreFactory(): StoreFactory {\n const KeyvRedis = require('@keyv/redis');\n let store: typeof KeyvRedis | undefined;\n return (pluginId, defaultTtl) => {\n if (!store) {\n store = new KeyvRedis(this.connection);\n }\n return new Keyv({\n namespace: pluginId,\n ttl: defaultTtl,\n store,\n useRedisSets: this.useRedisSets,\n });\n };\n }\n\n private createMemcacheStoreFactory(): StoreFactory {\n const KeyvMemcache = require('@keyv/memcache');\n let store: typeof KeyvMemcache | undefined;\n return (pluginId, defaultTtl) => {\n if (!store) {\n store = new KeyvMemcache(this.connection);\n }\n return new Keyv({\n namespace: pluginId,\n ttl: defaultTtl,\n store,\n });\n };\n }\n\n private createMemoryStoreFactory(): StoreFactory {\n const store = new Map();\n return (pluginId, defaultTtl) =>\n new Keyv({\n namespace: pluginId,\n ttl: defaultTtl,\n store,\n });\n }\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createServiceFactory,\n} from '@backstage/backend-plugin-api';\nimport { CacheManager } from './CacheManager';\n\n/**\n * @public\n */\nexport const cacheServiceFactory = createServiceFactory({\n service: coreServices.cache,\n deps: {\n config: coreServices.rootConfig,\n plugin: coreServices.pluginMetadata,\n logger: coreServices.rootLogger,\n },\n async createRootContext({ config, logger }) {\n return CacheManager.fromConfig(config, { logger });\n },\n async factory({ plugin }, manager) {\n return manager.forPlugin(plugin.getId()).getClient();\n },\n});\n"],"names":["createHash","Keyv","createServiceFactory","coreServices"],"mappings":";;;;;;;;;;AA+BO,MAAM,kBAA2C,CAAA;AAAA,EACtD,OAAA,CAAA;AAAA,EACA,cAAA,CAAA;AAAA,EACA,QAAA,CAAA;AAAA,EAEA,WAAA,CACE,MACA,EAAA,aAAA,EACA,OACA,EAAA;AACA,IAAA,IAAA,CAAK,OAAU,GAAA,MAAA,CAAA;AACf,IAAA,IAAA,CAAK,cAAiB,GAAA,aAAA,CAAA;AACtB,IAAA,IAAA,CAAK,QAAW,GAAA,OAAA,CAAA;AAAA,GAClB;AAAA,EAEA,MAAM,IACJ,GAC6B,EAAA;AAC7B,IAAM,MAAA,CAAA,GAAI,IAAK,CAAA,gBAAA,CAAiB,GAAG,CAAA,CAAA;AACnC,IAAA,MAAM,KAAQ,GAAA,MAAM,IAAK,CAAA,OAAA,CAAQ,IAAI,CAAC,CAAA,CAAA;AACtC,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAAA,EAEA,MAAM,GACJ,CAAA,GAAA,EACA,KACA,EAAA,IAAA,GAA+B,EAChB,EAAA;AACf,IAAM,MAAA,CAAA,GAAI,IAAK,CAAA,gBAAA,CAAiB,GAAG,CAAA,CAAA;AACnC,IAAA,MAAM,KAAK,OAAQ,CAAA,GAAA,CAAI,CAAG,EAAA,KAAA,EAAO,KAAK,GAAG,CAAA,CAAA;AAAA,GAC3C;AAAA,EAEA,MAAM,OAAO,GAA4B,EAAA;AACvC,IAAM,MAAA,CAAA,GAAI,IAAK,CAAA,gBAAA,CAAiB,GAAG,CAAA,CAAA;AACnC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,MAAA,IAAU,OAAS,EAAA;AACjC,IAAA,OAAO,QAAQ,SAAU,CAAA,MAAA,CAAO,KAAM,EAAC,EAAE,SAAU,EAAA,CAAA;AAAA,GACrD;AACF,CAAC;;;;;"}
package/dist/cache.d.ts CHANGED
@@ -1,9 +1,84 @@
1
1
  import * as _backstage_backend_plugin_api from '@backstage/backend-plugin-api';
2
- import * as _backstage_backend_common from '@backstage/backend-common';
2
+ import { LoggerService, CacheServiceOptions, CacheService } from '@backstage/backend-plugin-api';
3
+ import { Config } from '@backstage/config';
3
4
 
4
5
  /**
5
6
  * @public
6
7
  */
7
- declare const cacheServiceFactory: () => _backstage_backend_plugin_api.ServiceFactory<_backstage_backend_common.CacheClient, "plugin">;
8
+ declare const cacheServiceFactory: () => _backstage_backend_plugin_api.ServiceFactory<_backstage_backend_plugin_api.CacheService, "plugin">;
8
9
 
9
- export { cacheServiceFactory };
10
+ /**
11
+ * Implements a Cache Manager which will automatically create new cache clients
12
+ * for plugins when requested. All requested cache clients are created with the
13
+ * connection details provided.
14
+ *
15
+ * @public
16
+ */
17
+ declare class CacheManager {
18
+ /**
19
+ * Keys represent supported `backend.cache.store` values, mapped to factories
20
+ * that return Keyv instances appropriate to the store.
21
+ */
22
+ private readonly storeFactories;
23
+ private readonly logger?;
24
+ private readonly store;
25
+ private readonly connection;
26
+ private readonly useRedisSets;
27
+ private readonly errorHandler;
28
+ private readonly defaultTtl?;
29
+ /**
30
+ * Creates a new {@link CacheManager} instance by reading from the `backend`
31
+ * config section, specifically the `.cache` key.
32
+ *
33
+ * @param config - The loaded application configuration.
34
+ */
35
+ static fromConfig(config: Config, options?: {
36
+ /**
37
+ * An optional logger for use by the PluginCacheManager.
38
+ */
39
+ logger?: LoggerService;
40
+ /**
41
+ * An optional handler for connection errors emitted from the underlying data
42
+ * store.
43
+ */
44
+ onError?: (err: Error) => void;
45
+ }): CacheManager;
46
+ /**
47
+ * Generates a PluginCacheManager for consumption by plugins.
48
+ *
49
+ * @param pluginId - The plugin that the cache manager should be created for.
50
+ * Plugin names should be unique.
51
+ */
52
+ forPlugin(pluginId: string): {
53
+ getClient(options?: CacheServiceOptions): CacheService;
54
+ };
55
+ private getClientWithTtl;
56
+ private createRedisStoreFactory;
57
+ private createMemcacheStoreFactory;
58
+ private createMemoryStoreFactory;
59
+ }
60
+
61
+ /**
62
+ * Options given when constructing a {@link CacheManager}.
63
+ *
64
+ * @public
65
+ */
66
+ type CacheManagerOptions = {
67
+ /**
68
+ * An optional logger for use by the PluginCacheManager.
69
+ */
70
+ logger?: LoggerService;
71
+ /**
72
+ * An optional handler for connection errors emitted from the underlying data
73
+ * store.
74
+ */
75
+ onError?: (err: Error) => void;
76
+ };
77
+ /**
78
+ * @public
79
+ */
80
+ interface PluginCacheManager {
81
+ getClient(options?: CacheServiceOptions): CacheService;
82
+ }
83
+
84
+ export { CacheManager, type CacheManagerOptions, type PluginCacheManager, cacheServiceFactory };