@seepine/hono-cache
v0.0.1
Published
cache for hono
Downloads
9
Readme
hono-cache
基于 Hono 的中间件,支持内存和 Redis 缓存,以及多级组合。
安装
npm install @seepine/hono-cache使用方法
1. 注册中间件
import { Hono } from 'hono'
import { cacheMiddleware } from '@seepine/hono-cache'
const app = new Hono()
app.use(cacheMiddleware())2. 使用
// service/userService.ts
app.get('/userinfo', async c => {
const userinfo = await c.var.cache.get('key')
return c.json(userinfo)
})方法合集
get
c.var.cache.get('key')set
c.var.cache.set('key', 'value')
c.var.cache.set('key', 'value', '30s') // 过期时间getIfPresent
c.var.cache.getIfPresent(
'key',
() => {
return findById('userId')
},
'30s',
)