| 51 | * A `Table` backed by a `Cache`. |
| 52 | */ |
| 53 | export class CacheTable implements Table { |
| 54 | cacheName: string; |
| 55 | |
| 56 | constructor( |
| 57 | readonly name: string, |
| 58 | private cache: NamedCache, |
| 59 | private adapter: Adapter, |
| 60 | private cacheQueryOptions?: CacheQueryOptions, |
| 61 | ) { |
| 62 | this.cacheName = this.cache.name; |
| 63 | } |
| 64 | |
| 65 | private request(key: string): Request { |
| 66 | return this.adapter.newRequest('/' + key); |
| 67 | } |
| 68 | |
| 69 | 'delete'(key: string): Promise<boolean> { |
| 70 | return this.cache.delete(this.request(key), this.cacheQueryOptions); |
| 71 | } |
| 72 | |
| 73 | keys(): Promise<string[]> { |
| 74 | return this.cache.keys().then((requests) => requests.map((req) => req.url.slice(1))); |
| 75 | } |
| 76 | |
| 77 | read(key: string): Promise<any> { |
| 78 | return this.cache.match(this.request(key), this.cacheQueryOptions).then((res) => { |
| 79 | if (res === undefined) { |
| 80 | return Promise.reject(new NotFound(this.name, key)); |
| 81 | } |
| 82 | return res.json(); |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | write(key: string, value: Object): Promise<void> { |
| 87 | return this.cache.put(this.request(key), this.adapter.newResponse(JSON.stringify(value))); |
| 88 | } |
| 89 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…