(name: string, config: string)
| 37 | } |
| 38 | |
| 39 | function createInternal(name: string, config: string): Cache { |
| 40 | if (!config) { |
| 41 | return new NullCache(name); |
| 42 | } |
| 43 | const parts = config.split(';'); |
| 44 | if (parts.length > 1) { |
| 45 | return new MultiCache(name, ...parts.map(part => createInternal(name, part))); |
| 46 | } |
| 47 | const match = config.match(/^([^(]+)\(([^)]+)\)$/); |
| 48 | if (!match) throw new Error(`Unable to parse '${config}'`); |
| 49 | const params = match[2].split(','); |
| 50 | switch (match[1]) { |
| 51 | case 'InMemory': { |
| 52 | if (params.length !== 1) throw new Error(`Bad params: ${config}`); |
| 53 | return new InMemoryCache(name, paramInt(config, params[0])); |
| 54 | } |
| 55 | |
| 56 | case 'OnDisk': { |
| 57 | if (params.length !== 2) throw new Error(`Bad params: ${config}`); |
| 58 | return new OnDiskCache(name, params[0], paramInt(config, params[1])); |
| 59 | } |
| 60 | |
| 61 | case 'S3': { |
| 62 | if (params.length !== 3) throw new Error(`Bad params: ${config}`); |
| 63 | return new S3Cache(name, params[0], params[1], params[2]); |
| 64 | } |
| 65 | |
| 66 | default: { |
| 67 | throw new Error(`Unrecognised cache type '${match[1]}'`); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Added some type casting to make cache-tests a little more typesafe |
| 73 | export function createCacheFromConfig<TCache extends Cache = Cache>(name: string, config: string): TCache { |
no test coverage detected