Skip to content
Open
Show file tree
Hide file tree
Changes from all 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': minor
---

Initial Response Cache middleware implementation
85 changes: 85 additions & 0 deletions packages/response-cache/README.md
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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be invalidate.

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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be responseCache.


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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be onError: (key, c, error).

},
})
```

## Author

Rokas Muningis <https://github.com/muningis>

## License

MIT
15 changes: 15 additions & 0 deletions packages/response-cache/deno.json
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"]
}
}
56 changes: 56 additions & 0 deletions packages/response-cache/package.json
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"
}
}
Loading
Loading