-
Notifications
You must be signed in to change notification settings - Fork 290
RFC: Response cache #985
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
RFC: Response cache #985
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f02320b
feat(response cache): initial implementation
muningis a686aa0
feat(response cache): add complete response snapshot caching with hea…
muningis cc46026
docs(response cache): add changeset
muningis 785c5fb
feat(response cache): adjust according to PR comments
muningis e1ae821
chore: make it minor release instead of major
muningis 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@hono/response-cache': major | ||
| --- | ||
|
|
||
| Initial Response Cache middleware implementation | ||
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,80 @@ | ||
| # Response Cache for Hono | ||
| Response cache for [Hono](https://honojs.dev) with `Bring Your Own` cache store. | ||
|
|
||
| ## Usage | ||
| ### Basic with `in-memory` cache: | ||
| ```ts | ||
| import { Hono } from 'hono' | ||
| import { cacheMiddleware } from '@hono/response-cache' | ||
|
|
||
| const cacheStorage = new Map<string, string>() | ||
| const cache = cacheMiddleware({ | ||
| store: { | ||
| get: (key) => cacheStorage.get(key), | ||
| set: (key, value) => { | ||
| cacheStorage.set(key, value) | ||
| }, | ||
| delete: (key) => { | ||
| cacheStorage.delete(key) | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const app = new Hono() | ||
| app.use('*', cache) | ||
| ``` | ||
|
|
||
| ### Redis (and custom key function) | ||
| ```ts | ||
| import { Hono } from 'hono' | ||
| import { cacheMiddleware } from '@hono/response-cache' | ||
| import { createClient } from '@redis/client' | ||
|
|
||
| const redisClient = createClient({ | ||
| url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`, | ||
| }) | ||
|
|
||
| const store = { | ||
| get: async (key: string) => { | ||
| return (await redisClient.get(key)) ?? null | ||
| }, | ||
| set: async (key: string, value: string) => { | ||
| await redisClient.set(key, value) | ||
| return | ||
| }, | ||
| invalidate: async (key: string) => { | ||
| await redisClient.del(key) | ||
| return | ||
| }, | ||
| } | ||
|
|
||
| const cache = cacheMiddleware({ | ||
| store, | ||
| keyFn: (req, c) => `hono_res_cache_${c.req.path}`, | ||
| }) | ||
|
|
||
| app.use('*', cache) | ||
| ``` | ||
|
|
||
| ### Add logging | ||
| ```ts | ||
| import { cacheMiddleware } from '@hono/response-cache' | ||
|
|
||
| const cache = cacheMiddleware({ | ||
| store, | ||
| logging: { | ||
| enabled: true, | ||
| onHit: (key, c) => console.log(`Cache hit for ${key}`), | ||
| onMiss: (key, c) => console.log(`Cache miss for ${key}`), | ||
| onError: (key, c) => console.log(`Cache error for ${key}, error:`, error), | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| ## Author | ||
|
|
||
| Rokas Muningis <https://github.com/muningis> | ||
|
|
||
| ## License | ||
|
|
||
| MIT |
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,48 @@ | ||
| { | ||
| "name": "@hono/response-cache", | ||
| "version": "0.0.0", | ||
| "description": "Response cache for Hono", | ||
| "type": "module", | ||
| "main": "dist/index.cjs", | ||
| "module": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.js", | ||
| "require": "./dist/index.cjs" | ||
| } | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "scripts": { | ||
| "test": "tsc --noEmit && vitest --run", | ||
| "build": "tsc", | ||
| "publint": "publint", | ||
| "prerelease": "yarn build && yarn test", | ||
| "release": "yarn publish" | ||
| }, | ||
| "license": "MIT", | ||
| "publishConfig": { | ||
| "registry": "https://registry.npmjs.org", | ||
| "access": "public" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/honojs/middleware.git" | ||
| }, | ||
| "homepage": "https://github.com/honojs/middleware", | ||
| "peerDependencies": { | ||
| "hono": ">=4.7.0" | ||
| }, | ||
| "dependencies": { | ||
| "hono": "^4.10.1" | ||
| }, | ||
| "devDependencies": { | ||
| "publint": "^0.2.7", | ||
| "tsup": "^8.1.0", | ||
| "typescript": "^5.8.2", | ||
| "vitest": "^2.0.0" | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.