Skip to content

Commit

Permalink
lib: more cache fixes
Browse files Browse the repository at this point in the history
Closes nodejs#3806

Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
  • Loading branch information
flakey5 committed Nov 8, 2024
1 parent fe1f132 commit a2e32bb
Show file tree
Hide file tree
Showing 12 changed files with 449 additions and 462 deletions.
47 changes: 15 additions & 32 deletions docs/docs/api/CacheStore.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

A Cache Store is responsible for storing and retrieving cached responses.
It is also responsible for deciding which specific response to use based off of
a response's `Vary` header (if present).
a response's `Vary` header (if present). It is expected to be compliant with
[RFC-9111](https://www.rfc-editor.org/rfc/rfc9111.html).

## Pre-built Cache Stores

Expand All @@ -21,27 +22,32 @@ The store must implement the following functions:

### Getter: `isFull`

This tells the cache interceptor if the store is full or not. If this is true,
Optional. This tells the cache interceptor if the store is full or not. If this is true,
the cache interceptor will not attempt to cache the response.

### Function: `createReadStream`
### Function: `getValueByKey`

Parameters:

* **req** `Dispatcher.RequestOptions` - Incoming request

Returns: `CacheStoreReadable | Promise<CacheStoreReadable | undefined> | undefined` - If the request is cached, a readable for the body is returned. Otherwise, `undefined` is returned.
Returns: `GetValueByKeyResult | Promise<GetValueByKeyResult | undefined> | undefined` - If the request is cached, a readable for the body is returned. Otherwise, `undefined` is returned.

Response properties:

* **response** `CachedResponse` - The cached response data.
* **body** `Readable | undefined` - The response's body.

### Function: `createWriteStream`

Parameters:

* **req** `Dispatcher.RequestOptions` - Incoming request
* **value** `CacheStoreValue` - Response to store
* **value** `CachedResponse` - Response to store

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.
Returns: `Writable | 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.

## `CacheStoreValue`
## `CachedResponse`

This is an interface containing the majority of a response's data (minus the body).

Expand All @@ -55,15 +61,11 @@ This is an interface containing the majority of a response's data (minus the bod

### Property `rawHeaders`

`(Buffer | Buffer[])[]` - The response's headers.

### Property `rawTrailers`

`string[] | undefined` - The response's trailers.
`Buffer[]` - The response's headers.

### Property `vary`

`Record<string, string> | undefined` - The headers defined by the response's `Vary` header
`Record<string, string | string[]> | undefined` - The headers defined by the response's `Vary` header
and their respective values for later comparison

For example, for a response like
Expand Down Expand Up @@ -95,22 +97,3 @@ This would be
is either the same sa staleAt or the `max-stale` caching directive.

The store must not return a response after the time defined in this property.

## `CacheStoreReadable`

This extends Node's [`Readable`](https://nodejs.org/api/stream.html#class-streamreadable)
and defines extra properties relevant to the cache interceptor.

### Getter: `value`

The response's [`CacheStoreValue`](#cachestorevalue)

## `CacheStoreWriteable`

This extends Node's [`Writable`](https://nodejs.org/api/stream.html#class-streamwritable)
and defines extra properties relevant to the cache interceptor.

### Setter: `rawTrailers`

If the response has trailers, the cache interceptor will pass them to the cache
interceptor through this method.
Loading

0 comments on commit a2e32bb

Please # to comment.