Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: http caching #3562

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open

feat: http caching #3562

wants to merge 38 commits into from

Conversation

flakey5
Copy link
Member

@flakey5 flakey5 commented Sep 7, 2024

Implements bare-bones opt-in http caching as per rfc9111. Bare-bones in this case means what's required by the spec and a few extra bits, with more coming in future prs.

Opening as a draft since there's still some more work to be done (mostly tests, but a bit more functionality-wise)

No request cache directives are supported at this time, this will come later.

Response caching directives supported:

  • public
  • private
  • s-maxage
  • max-age
  • Expires header
  • no-cache
  • no-store
  • stale-while-revalidate

This relates to...

Closes #3231
Closes #2760
Closes #2256
Closes #1146

Changes

Features

  • Opt-in http caching
  • In-memory default cache store

Bug Fixes

n/a

Breaking Changes and Deprecations

n/a

Status

lib/cache/lru-cache-store.js Outdated Show resolved Hide resolved
lib/cache/lru-cache-store.js Outdated Show resolved Hide resolved
lib/cache/lru-cache-store.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
@ronag
Copy link
Member

ronag commented Sep 7, 2024

Would it maybe be better to use jsdoc types rather than separate type definition files? @mcollina

index.js Outdated Show resolved Hide resolved
lib/cache/lru-cache-store.js Outdated Show resolved Hide resolved
lib/cache/lru-cache-store.js Outdated Show resolved Hide resolved
lib/cache/lru-cache-store.js Outdated Show resolved Hide resolved
lib/cache/lru-cache-store.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Show resolved Hide resolved
lib/interceptor/cache.js Outdated Show resolved Hide resolved
lib/interceptor/cache.js Outdated Show resolved Hide resolved
@mcollina
Copy link
Member

mcollina commented Sep 9, 2024

Would it maybe be better to use jsdoc types rather than separate type definition files? @mcollina

I think using type definitions are in line with the rest of the codebase, and therefore the correct implementation.

If we want to migrate to a jsdoc-generated world, let's discuss in another issue!

lib/handler/cache-handler.js Outdated Show resolved Hide resolved
lib/cache/lru-cache-store.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
@ronag
Copy link
Member

ronag commented Sep 11, 2024

@flakey5 please make sure to add me and @IsakT as co-authors (assuming you took some of our code in nxt-undici).

lib/cache/lru-cache-store.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
@flakey5 flakey5 force-pushed the flakey5/3231 branch 2 times, most recently from e07e3ec to 938fa7a Compare September 12, 2024 03:58
Implements bare-bones http caching as per rfc9111

Closes nodejs#3231
Closes nodejs#2760
Closes nodejs#2256
Closes nodejs#1146

Co-authored-by: Carlos Fuentes <me@metcoder.dev>

Co-authored-by: Robert Nagy <ronagy@icloud.com>

Co-authored-by: Isak Törnros <isak.tornros@hotmail.com>

Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
@flakey5 flakey5 marked this pull request as ready for review September 14, 2024 05:29
types/interceptors.d.ts Show resolved Hide resolved
test/cache-interceptor/cache-stores.js Outdated Show resolved Hide resolved
test/cache-interceptor/interceptor.js Outdated Show resolved Hide resolved
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
lib/cache/memory-cache-store.js Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
flakey5 and others added 2 commits September 15, 2024 10:55
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Co-authored-by: Robert Nagy <ronagy@icloud.com>
lib/cache/memory-cache-store.js Outdated Show resolved Hide resolved
lib/cache/memory-cache-store.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
lib/handler/cache-revalidation-handler.js Outdated Show resolved Hide resolved
lib/interceptor/cache.js Outdated Show resolved Hide resolved
test/cache-interceptor/interceptor.js Show resolved Hide resolved
flakey5 and others added 3 commits September 16, 2024 11:21
Co-authored-by: Carlos Fuentes <me@metcoder.dev>
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
lib/cache/memory-cache-store.js Outdated Show resolved Hide resolved
lib/cache/memory-cache-store.js Outdated Show resolved Hide resolved
Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

headers could be null or undefined

lib/cache/memory-cache-store.js Outdated Show resolved Hide resolved
Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ronag
Should we use fastTimers?

lib/cache/memory-cache-store.js Show resolved Hide resolved
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
@ronag
Copy link
Member

ronag commented Sep 24, 2024

TBH I think we should skip in-flight de-duplication in this first phase. @mcollina wdyt?

I would propose:

Step 1: in-memory cache
Step 2: sqlite cache
Step 3: in-flight de-deduplication

lib/handler/cache-handler.js Outdated Show resolved Hide resolved
lib/handler/cache-handler.js Outdated Show resolved Hide resolved
lib/interceptor/cache.js Show resolved Hide resolved
@mcollina
Copy link
Member

TBH I think we should skip in-flight de-duplication in this first phase. @mcollina wdyt?

I think this might be handled here already. Why do you want to remove it?

@flakey5
Copy link
Member Author

flakey5 commented Sep 24, 2024

I think this might be handled here already. Why do you want to remove it?

Re #3562 (comment)

Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
@flakey5
Copy link
Member Author

flakey5 commented Sep 24, 2024

I would propose:

Step 1: in-memory cache Step 2: sqlite cache Step 3: in-flight de-deduplication

To add to this, I'll also be opening another pr that adds support for cache control directives specified by the request (wip branch https://github.com/flakey5/undici/tree/flakey5/20240924/cli-cache-control)

Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
@flakey5
Copy link
Member Author

flakey5 commented Sep 25, 2024

Pushed a commit removing the request de-dupe code re #3562 (comment) and #3562 (comment). Can revert it if we do want to have it, but, I agree that it should be added afterwards in a pr dedicated to it. That way we can address the issues there without tying the rest of this up.

Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
lib/util/cache.js Show resolved Hide resolved
lib/util/cache.js Outdated Show resolved Hide resolved
test/cache-interceptor/cache-stores.js Show resolved Hide resolved
flakey5 and others added 3 commits September 25, 2024 16:49
Co-authored-by: Matteo Collina <matteo.collina@gmail.com>
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have opinions... Will try to make time over weeekend.

Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
@mcollina
Copy link
Member

mcollina commented Oct 1, 2024

@ronag, could you highlight the problems you are finding with this? I would like to land it and try to ship it asap.

@flakey5 flakey5 mentioned this pull request Oct 1, 2024
7 tasks
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, have some concerns with the API of the MemoryCacheStore, but we can revisit it in another PR.


value = {
readers: 0,
readLock: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you repeat/clarify why are using read locks for the stream?
This part I'm not fully getting it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The readLock is to stop a request from writing to a value if there's another request reading from it.

The writeLock is to stop a request from reading a value that's currently being written to

Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement HTTP caching fetch: caching Caching Question: Does undici.fetch support RFC 7234?
5 participants