-
Notifications
You must be signed in to change notification settings - Fork 277
feat(response cache): initial implementation #1652
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
muningis
wants to merge
10
commits into
honojs:main
Choose a base branch
from
muningis:response-cache-2
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.
+719
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
511b57a
feat(response cache): initial implementation
muningis 16947df
feat(response cache): add complete response snapshot caching with hea…
muningis c225875
docs(response cache): add changeset
muningis cc263bc
feat(response cache): adjust according to PR comments
muningis 97b24e7
chore: make it minor release instead of major
muningis 5a80921
build: match project structure to other packages
muningis 408e78e
build: match some of dependencies
muningis e109afb
build: PR review changes
muningis 01e12e9
style: autofix
muningis 641960f
build: fix incorrect version for vitest
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': minor | ||
| --- | ||
|
|
||
| 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,85 @@ | ||
| # 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' | ||
|
Member
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. This should be |
||
|
|
||
| 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), | ||
|
Member
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. This should be |
||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| ## 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,15 @@ | ||
| { | ||
| "name": "@hono/response-cache", | ||
| "version": "0.0.0", | ||
| "license": "MIT", | ||
| "exports": { | ||
| ".": "./src/index.ts" | ||
| }, | ||
| "imports": { | ||
| "hono": "jsr:@hono/hono@^4.8.3" | ||
| }, | ||
| "publish": { | ||
| "include": ["deno.json", "README.md", "src/**/*.ts"], | ||
| "exclude": ["src/**/*.test.ts"] | ||
| } | ||
| } |
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,56 @@ | ||
| { | ||
| "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", | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "scripts": { | ||
| "build": "tsdown", | ||
| "format": "prettier --check . --ignore-path ../../.gitignore", | ||
| "lint": "eslint", | ||
| "typecheck": "tsc -b tsconfig.json", | ||
| "test": "vitest", | ||
| "version:jsr": "yarn version:set $npm_package_version" | ||
| }, | ||
| "exports": { | ||
| ".": { | ||
| "import": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| }, | ||
| "require": { | ||
| "types": "./dist/index.d.cts", | ||
| "default": "./dist/index.cjs" | ||
| } | ||
| } | ||
| }, | ||
| "license": "MIT", | ||
| "publishConfig": { | ||
| "registry": "https://registry.npmjs.org", | ||
| "access": "public", | ||
| "provenance": true | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/honojs/middleware.git", | ||
| "directory": "packages/response-cache" | ||
| }, | ||
| "homepage": "https://github.com/honojs/middleware", | ||
| "peerDependencies": { | ||
| "hono": ">=4.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "hono": "^4.11.1", | ||
| "tsdown": "^0.15.9", | ||
| "typescript": "^5.8.2", | ||
| "vitest": "^4.0.16" | ||
| }, | ||
| "engines": { | ||
| "node": ">=18" | ||
| } | ||
| } |
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.
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.
This should be
invalidate.