|
| 1 | +# Cache Store |
| 2 | + |
| 3 | +A Cache Store is responsible for storing and retrieving cached responses. |
| 4 | +It is also responsible for deciding which specific response to use based off of |
| 5 | +a response's `Vary` header (if present). |
| 6 | + |
| 7 | +## Pre-built Cache Stores |
| 8 | + |
| 9 | +### `MemoryCacheStore` |
| 10 | + |
| 11 | +The `MemoryCacheStore` stores the responses in-memory. |
| 12 | + |
| 13 | +**Options** |
| 14 | + |
| 15 | +- `maxEntries` - The maximum amount of responses to store. Default `Infinity`. |
| 16 | +- `maxEntrySize` - The maximum size in bytes that a response's body can be. If a response's body is greater than or equal to this, the response will not be cached. |
| 17 | + |
| 18 | +## Defining a Custom Cache Store |
| 19 | + |
| 20 | +The store must implement the following functions: |
| 21 | + |
| 22 | +### Getter: `isFull` |
| 23 | + |
| 24 | +This tells the cache interceptor if the store is full or not. If this is true, |
| 25 | +the cache interceptor will not attempt to cache the response. |
| 26 | + |
| 27 | +### Function: `createReadStream` |
| 28 | + |
| 29 | +Parameters: |
| 30 | + |
| 31 | +* **req** `Dispatcher.RequestOptions` - Incoming request |
| 32 | + |
| 33 | +Returns: `CacheStoreReadable | Promise<CacheStoreReadable | undefined> | undefined` - If the request is cached, a readable for the body is returned. Otherwise, `undefined` is returned. |
| 34 | + |
| 35 | +### Function: `createWriteStream` |
| 36 | + |
| 37 | +Parameters: |
| 38 | + |
| 39 | +* **req** `Dispatcher.RequestOptions` - Incoming request |
| 40 | +* **value** `CacheStoreValue` - Response to store |
| 41 | + |
| 42 | +Returns: `CacheStoreWriteable | undefined` - If the store is full, return `undefined`. Otherwise, return a writable so that the cache interceptor can stream the body and trailers to the store. |
| 43 | + |
| 44 | +## `CacheStoreValue` |
| 45 | + |
| 46 | +This is an interface containing the majority of a response's data (minus the body). |
| 47 | + |
| 48 | +### Property `statusCode` |
| 49 | + |
| 50 | +`number` - The response's HTTP status code. |
| 51 | + |
| 52 | +### Property `statusMessage` |
| 53 | + |
| 54 | +`string` - The response's HTTP status message. |
| 55 | + |
| 56 | +### Property `rawHeaders` |
| 57 | + |
| 58 | +`(Buffer | Buffer[])[]` - The response's headers. |
| 59 | + |
| 60 | +### Property `rawTrailers` |
| 61 | + |
| 62 | +`string[] | undefined` - The response's trailers. |
| 63 | + |
| 64 | +### Property `vary` |
| 65 | + |
| 66 | +`Record<string, string> | undefined` - The headers defined by the response's `Vary` header |
| 67 | +and their respective values for later comparison |
| 68 | + |
| 69 | +For example, for a response like |
| 70 | +``` |
| 71 | +Vary: content-encoding, accepts |
| 72 | +content-encoding: utf8 |
| 73 | +accepts: application/json |
| 74 | +``` |
| 75 | + |
| 76 | +This would be |
| 77 | +```js |
| 78 | +{ |
| 79 | + 'content-encoding': 'utf8', |
| 80 | + accepts: 'application/json' |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### Property `cachedAt` |
| 85 | + |
| 86 | +`number` - Time in millis that this value was cached. |
| 87 | + |
| 88 | +### Property `staleAt` |
| 89 | + |
| 90 | +`number` - Time in millis that this value is considered stale. |
| 91 | + |
| 92 | +### Property `deleteAt` |
| 93 | + |
| 94 | +`number` - Time in millis that this value is to be deleted from the cache. This |
| 95 | +is either the same sa staleAt or the `max-stale` caching directive. |
| 96 | + |
| 97 | +The store must not return a response after the time defined in this property. |
| 98 | + |
| 99 | +## `CacheStoreReadable` |
| 100 | + |
| 101 | +This extends Node's [`Readable`](https://nodejs.org/api/stream.html#class-streamreadable) |
| 102 | +and defines extra properties relevant to the cache interceptor. |
| 103 | + |
| 104 | +### Getter: `value` |
| 105 | + |
| 106 | +The response's [`CacheStoreValue`](#cachestorevalue) |
| 107 | + |
| 108 | +## `CacheStoreWriteable` |
| 109 | + |
| 110 | +This extends Node's [`Writable`](https://nodejs.org/api/stream.html#class-streamwritable) |
| 111 | +and defines extra properties relevant to the cache interceptor. |
| 112 | + |
| 113 | +### Setter: `rawTrailers` |
| 114 | + |
| 115 | +If the response has trailers, the cache interceptor will pass them to the cache |
| 116 | +interceptor through this method. |
0 commit comments