-
Notifications
You must be signed in to change notification settings - Fork 9
docs: make target.loadedFromSource canonical, drop context mirror #565
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
Open
kriszyp
wants to merge
1
commit into
main
Choose a base branch
from
kris/docs-loadedfromsource-target-only
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -250,21 +250,23 @@ Harper automatically serializes concurrent requests for the same missing or stal | |||||
|
|
||||||
| #### Observing cache disposition | ||||||
|
|
||||||
| Each `get` on a caching table records whether the record came from the cache or from the source, in the `loadedFromSource` property of both the request context and the `RequestTarget`: | ||||||
| Whether a `get` on a caching table was served from cache or loaded from the source is a **per-`get` result**, recorded on the `RequestTarget` for that call as `target.loadedFromSource`. Because each `get()` has its own target, the value is precise to that one call and unaffected by any nested or subsequent gets — it is what Harper's REST handler reads to set cache response headers. | ||||||
|
|
||||||
| ```javascript | ||||||
| const context = {}; | ||||||
| const record = await MyCache.get(recordId, context); | ||||||
| console.log(context.loadedFromSource); // true = went to the source, false = served from cache | ||||||
| import { RequestTarget } from 'harper'; | ||||||
|
|
||||||
| const target = new RequestTarget(); | ||||||
| target.id = recordId; | ||||||
| const record = await MyCache.get(target); | ||||||
| console.log(target.loadedFromSource); // true = loaded from source, false = served from cache | ||||||
| ``` | ||||||
|
|
||||||
| Within a resource method, the same value is available on the active context via `getContext().loadedFromSource` after the `get` resolves. The flag settles as follows: | ||||||
| The flag settles as follows: | ||||||
|
|
||||||
| - `true` — the get went to the source: either it fetched the record, or the source errored and a stale cached record was served as a fallback (`staleIfError`). `true` means a source request was made, not necessarily that the returned data is fresh. | ||||||
| - `false` — the record was served from the cache: fresh hits, `onlyIfCached` requests, stale-while-revalidate responses (the source fetch continues in the background), and requests that waited on another request's in-flight fetch of the same record. This last case means a cache hit can still take as long as an upstream fetch. | ||||||
| - Each get on a caching table in the same context overwrites the value, so read it after the `get` you are measuring. | ||||||
|
|
||||||
| Note that `get()` returns a plain `RecordObject`, not a resource instance — the record itself does not carry cache disposition; read it from the context (or an explicitly passed `RequestTarget`). Prior to Harper 5.1.16, `context.loadedFromSource` was never assigned and the flag was only observable via an explicitly passed `RequestTarget`. | ||||||
| `get()` returns a plain `RecordObject`, not a resource instance, so the returned record does not itself carry cache disposition — read it from the `RequestTarget` you passed to the get. A `get` called with a plain id has no target to read back, so pass a `RequestTarget` when you need to observe disposition. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For better consistency, format the method reference
Suggested change
|
||||||
|
|
||||||
| #### Source `get` — controlling timestamp and expiration | ||||||
|
|
||||||
|
|
@@ -653,7 +655,6 @@ Returns the current context, which includes: | |||||
|
|
||||||
| - `user` — User object with username, role, and authorization information | ||||||
| - `transaction` — The current transaction | ||||||
| - `loadedFromSource` — For caching tables (5.1.16+), cache disposition of the most recent `get` in this context: `true` if it went to the source, `false` if served from cache (see [Observing cache disposition](#observing-cache-disposition)) | ||||||
|
|
||||||
| When triggered by HTTP, the context is the `Request` object with these additional properties: | ||||||
|
|
||||||
|
|
@@ -1159,7 +1160,6 @@ getContext is availabe as export from the `harper` module, or as a global variab | |||||
|
|
||||||
| - `user` — User object with username, role, and authorization information | ||||||
| - `transaction` — The current transaction | ||||||
| - `loadedFromSource` — For caching tables (5.1.16+), cache disposition of the most recent `get` in this context: `true` if it went to the source, `false` if served from cache (see [Observing cache disposition](#observing-cache-disposition)) | ||||||
|
|
||||||
| When triggered by HTTP, the context is the `Request` object with these additional properties: | ||||||
|
|
||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better consistency and readability, format class names like
RequestTargetand pluralized method references likegets with code backticks.