Merge pull request #9739 from magnusp/cachemanager-redis-client

backend-common: add Redis backed cache store
This commit is contained in:
Fredrik Adelöw
2022-02-23 15:43:32 +01:00
committed by GitHub
7 changed files with 135 additions and 6 deletions
+8
View File
@@ -134,6 +134,14 @@ export interface Config {
| {
store: 'memory';
}
| {
store: 'redis';
/**
* A redis connection string in the form `user:pass@host:port`.
* @secret
*/
connection: string;
}
| {
store: 'memcache';
/**
+1
View File
@@ -62,6 +62,7 @@
"jose": "^1.27.1",
"keyv": "^4.0.3",
"keyv-memcache": "^1.2.5",
"@keyv/redis": "^2.2.3",
"knex": "^1.0.2",
"lodash": "^4.17.21",
"logform": "^2.3.2",
+31
View File
@@ -18,6 +18,8 @@ import { ConfigReader } from '@backstage/config';
import Keyv from 'keyv';
/* @ts-expect-error */
import KeyvMemcache from 'keyv-memcache';
/* @ts-expect-error */
import KeyvRedis from '@keyv/redis';
import { DefaultCacheClient } from './CacheClient';
import { CacheManager } from './CacheManager';
import { NoStore } from './NoStore';
@@ -26,6 +28,8 @@ jest.createMockFromModule('keyv');
jest.mock('keyv');
jest.createMockFromModule('keyv-memcache');
jest.mock('keyv-memcache');
jest.createMockFromModule('@keyv/redis');
jest.mock('@keyv/redis');
jest.mock('./CacheClient', () => {
return {
DefaultCacheClient: jest.fn(),
@@ -190,6 +194,33 @@ describe('CacheManager', () => {
});
});
it('returns a Redis client when configured', () => {
const redisHostAndPort = '127.0.0.1:6379';
const expectedHost = `redis://${redisHostAndPort}`;
const manager = CacheManager.fromConfig(
new ConfigReader({
backend: {
cache: {
store: 'redis',
connection: redisHostAndPort,
},
},
}),
);
const expectedTtl = 3600;
manager.forPlugin('test').getClient({ defaultTtl: expectedTtl });
const cache = Keyv as unknown as jest.Mock;
const mockCacheCalls = cache.mock.calls.splice(-1);
expect(mockCacheCalls[0][0]).toMatchObject({
ttl: expectedTtl,
});
expect(mockCacheCalls[0][0].store).toBeInstanceOf(KeyvRedis);
const redis = KeyvRedis as jest.Mock;
const mockRedisCalls = redis.mock.calls.splice(-1);
expect(mockRedisCalls[0][0]).toEqual(expectedHost);
});
describe('connection errors', () => {
it('uses provided logger', () => {
// Set up and inject mock logger.
+14
View File
@@ -18,6 +18,8 @@ import { Config } from '@backstage/config';
import Keyv from 'keyv';
// @ts-expect-error
import KeyvMemcache from 'keyv-memcache';
// @ts-expect-error
import KeyvRedis from '@keyv/redis';
import { Logger } from 'winston';
import { getRootLogger } from '../logging';
import { DefaultCacheClient, CacheClient } from './CacheClient';
@@ -37,6 +39,7 @@ export class CacheManager {
* that return Keyv instances appropriate to the store.
*/
private readonly storeFactories = {
redis: this.getRedisClient,
memcache: this.getMemcacheClient,
memory: this.getMemoryClient,
none: this.getNoneClient,
@@ -123,6 +126,17 @@ export class CacheManager {
return this.storeFactories[this.store].call(this, pluginId, ttl);
}
private getRedisClient(
pluginId: string,
defaultTtl: number | undefined,
): Keyv {
return new Keyv({
namespace: pluginId,
ttl: defaultTtl,
store: new KeyvRedis(`redis://${this.connection}`),
});
}
private getMemcacheClient(
pluginId: string,
defaultTtl: number | undefined,