Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/khaki-tires-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/response-cache': major
---

Initial Response Cache middleware implementation
80 changes: 80 additions & 0 deletions packages/response-cache/README.md
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
48 changes: 48 additions & 0 deletions packages/response-cache/package.json
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"
}
}
Loading
Loading