forked from Kareem411/TriCache
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(fastify): first-class tricache/fastify package entry #8
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
Closed
Closed
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
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Fastify Plugin | ||
|
|
||
| > Package entry: `tricache/fastify` | ||
|
|
||
| First-class **Node.js** Fastify plugin backed by `CacheService`. This is the dedicated export requested for Fastify apps — the same implementation as [`tricache/http`](/integrations/http), not a second stack. | ||
|
|
||
| ```typescript | ||
| import Fastify from 'fastify'; | ||
| import { createFastifyPlugin, fastifyCachePlugin, fastifyCache } from 'tricache/fastify'; | ||
|
|
||
| const app = Fastify(); | ||
|
|
||
| await app.register(createFastifyPlugin({ | ||
| ttl: 300, | ||
| tags: ['api'], | ||
| })); | ||
| ``` | ||
|
|
||
| `fastifyCachePlugin` is the zero-arg alias (`createFastifyPlugin()`). Pass options at register time: | ||
|
|
||
| ```typescript | ||
| import { CacheService } from 'tricache'; | ||
| import { fastifyCachePlugin } from 'tricache/fastify'; | ||
|
|
||
| const cache = CacheService.create(); | ||
|
|
||
| await app.register(fastifyCachePlugin, { | ||
| cache, | ||
| ttl: 300, | ||
| swr: 60, | ||
| tags: ['api'], | ||
| headerWhitelist: ['accept-language'], | ||
| }); | ||
| ``` | ||
|
|
||
| Route-level `preHandler` (ttl/tags without a global plugin): | ||
|
|
||
| ```typescript | ||
| import { fastifyCache } from 'tricache/fastify'; | ||
|
|
||
| app.get('/api/catalog', { | ||
| preHandler: fastifyCache({ cache, ttl: 300, tags: ['catalog'] }), | ||
| }, async () => { | ||
| return await fetchCatalog(); | ||
| }); | ||
| ``` | ||
|
|
||
| `import { createFastifyPlugin, fastifyCachePlugin, fastifyCache } from 'tricache/http'` remains supported for back-compat. | ||
|
|
||
| --- | ||
|
|
||
| ## Behavior | ||
|
|
||
| * **Safe methods only**: `GET` and `HEAD` are cached; other methods pass through. | ||
| * **Weak ETags**: SHA-1 weak validators (`ETag: W/"…"`). | ||
| * **304 Not Modified**: matching `If-None-Match` short-circuits with an empty body. | ||
| * **Status gate**: non-2xx responses are never kept (4xx/5xx cannot poison a key). | ||
| * **Bypass**: `Cache-Control: no-cache` / `no-store` and a custom `skipCache` predicate skip the cache. | ||
| * **ttl / tags**: already covered by plugin options and `fastifyCache({ ttl, tags })` `preHandler` opts. | ||
|
|
||
| Route-level `config.cache` and `x-cache: HIT|MISS|STALE` response headers are **not** in this entry. Those can land as a follow-up on the same plugin — this package does not introduce a second Fastify implementation. | ||
|
|
||
| --- | ||
|
|
||
| ## Options | ||
|
|
||
| | Option | Type | Default | Description | | ||
| |---|---|---|---| | ||
| | `cache` | `CacheService` | singleton | TriCache instance. If omitted, lazily resolves `CacheService.create()` | | ||
| | `ttl` | `number` | `300` | Time-to-live in seconds | | ||
| | `swr` | `number` | `undefined` | Stale-While-Revalidate window in seconds | | ||
| | `etag` | `boolean` | `true` | Generate and evaluate weak ETags (`W/"…"`) | | ||
| | `keyGenerator` | `(req) => string` | method + URL + sorted query | Custom cache key from the Fastify request | | ||
| | `headerWhitelist` | `string[]` | `[]` | Request headers incorporated into the cache key | | ||
| | `skipCache` | `(req) => boolean` | `undefined` | Predicate returning true to bypass cache | | ||
| | `tags` | `string[] \| ((req) => string[])` | `[]` | Semantic tags for targeted `cache.invalidateTag()` | | ||
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| * tricache/fastify — first-class Node.js Fastify plugin entry. | ||
| * | ||
| * Re-exports the existing Fastify plugin from `src/http/fastify.ts` so apps can: | ||
| * | ||
| * import { createFastifyPlugin, fastifyCachePlugin, fastifyCache } from 'tricache/fastify'; | ||
| * | ||
| * `import { … } from 'tricache/http'` remains supported for back-compat. | ||
| * | ||
| * Plugin `options` and route `preHandler` already accept `ttl` / `tags` (and | ||
| * `swr`, `etag`, `skipCache`, `keyGenerator`, `headerWhitelist`). Route-level | ||
| * `config.cache` and `x-cache: HIT|MISS|STALE` headers are intentionally left | ||
| * for a follow-up so this entry does not fork a second Fastify stack. | ||
| */ | ||
|
|
||
| export { | ||
| createFastifyPlugin, | ||
| fastifyCachePlugin, | ||
| fastifyCache, | ||
| type FastifyCacheOptions, | ||
| type CachedFastifyResponse, | ||
| } from '../http/fastify.js'; |
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
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
Oops, something went wrong.
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.
swroptionWhen a user configures
swr, the Fastify implementation ignores it: both response-capture paths callactiveCache.set(...), which only stores the normal TTL and tags, rather than usingCacheService.get(..., { swr })or otherwise retaining a stale window. Consequently an entry expires afterttland becomes a normal miss instead of being served stale and revalidated, so this newly documented option gives users behavior different from what the table promises.Useful? React with 👍 / 👎.