Refactor CacheManager.fromConfig

Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
Eric Peterson
2021-05-03 17:37:28 +02:00
parent cc60475127
commit b71c230beb
2 changed files with 35 additions and 16 deletions
+15 -1
View File
@@ -41,12 +41,17 @@ describe('CacheManager', () => {
describe('CacheManager.fromConfig', () => {
it('accesses the backend.cache key', () => {
const getOptionalConfig = jest.fn();
const getOptionalString = jest.fn();
const config = defaultConfig();
config.getOptionalConfig = getOptionalConfig;
config.getOptionalString = getOptionalString;
CacheManager.fromConfig(config);
expect(getOptionalConfig.mock.calls[0][0]).toEqual('backend.cache');
expect(getOptionalString.mock.calls[0][0]).toEqual('backend.cache.store');
expect(getOptionalConfig.mock.calls[0][0]).toEqual(
'backend.cache.connection',
);
});
it('does not require the backend.cache key', () => {
@@ -55,6 +60,15 @@ describe('CacheManager', () => {
CacheManager.fromConfig(config);
}).not.toThrowError();
});
it('throws on unknown cache store', () => {
const config = new ConfigReader({
backend: { cache: { store: 'notreal' } },
});
expect(() => {
CacheManager.fromConfig(config);
}).toThrowError();
});
});
describe('CacheManager.forPlugin', () => {
+20 -15
View File
@@ -30,7 +30,7 @@ import { PluginCacheManager } from './types';
*/
export class CacheManager {
/**
* Keys represented supported `backend.cache.store` values, mapped to
* Keys represents supported `backend.cache.store` values, mapped to
* factories that return cacheManager.Cache instances appropriate to the
* store.
*/
@@ -40,6 +40,9 @@ export class CacheManager {
none: this.getNoneClient,
};
private readonly store: keyof CacheManager['storeFactories'];
private readonly connection: Config;
/**
* Creates a new CacheManager instance by reading from the `backend` config
* section, specifically the `.cache` key.
@@ -49,12 +52,21 @@ export class CacheManager {
static fromConfig(config: Config): CacheManager {
// If no `backend.cache` config is provided, instantiate the CacheManager
// with empty config; allowing a "none" cache client will be returned.
return new CacheManager(
config.getOptionalConfig('backend.cache') || new ConfigReader(undefined),
);
const store = config.getOptionalString('backend.cache.store') || 'none';
const connectionConfig =
config.getOptionalConfig('backend.cache.connection') ||
new ConfigReader(undefined);
return new CacheManager(store, connectionConfig);
}
private constructor(private readonly config: Config) {}
private constructor(store: string, connectionConfig: Config) {
if (!this.storeFactories.hasOwnProperty(store)) {
throw new Error(`Unknown cache store: ${store}`);
}
this.store = store as keyof CacheManager['storeFactories'];
this.connection = connectionConfig;
}
/**
* Generates a CacheManagerInstance for consumption by plugins.
@@ -75,19 +87,12 @@ export class CacheManager {
}
private getClientWithTtl(ttl: number): cacheManager.Cache {
const store = this.config.getOptionalString(
'store',
) as keyof CacheManager['storeFactories'];
if (this.storeFactories.hasOwnProperty(store)) {
return this.storeFactories[store].call(this, ttl);
}
return this.storeFactories.none.call(this, ttl);
return this.storeFactories[this.store].call(this, ttl);
}
private getMemcacheClient(defaultTtl: number): cacheManager.Cache {
const hosts = this.config.getStringArray('connection.hosts');
const netTimeout = this.config.getOptionalNumber('connection.netTimeout');
const hosts = this.connection.getStringArray('hosts');
const netTimeout = this.connection.getOptionalNumber('netTimeout');
return cacheManager.caching({
store: memcachedStore,
driver: Memcache,