用于缓存http接口内容的gin高性能中间件

花了2天时间写了一个用于缓存http接口内容的gin高性能中间件,感觉应该很实用

Github传送门

  • 得益于简单singleflight解决缓存击穿问题,优于官方 gin-contrib/cache
  • 支持redis(适合集群)及memory(适合单机)驱动
  • 支持自定key(默认route) 及参数k(默认requestPath)
  • 使用hash数据结构来解决同个接口的数据组批量维护的问题
  • 使用过期时间加删除缓存策略(不覆盖)
  • mode参数可选择缓存http状态码为2xx的回包

Quick start

go get github.com/janartist/api-cache
package main

import (
    apicache "github.com/janartist/api-cache"
    "github.com/gin-gonic/gin"
    "net/http/httptest"
    "github.com/janartist/api-cache/store"
    "testing"
)
func main()  {
    m := apicache.NewDefault(&store.RedisConf{
        Addr: "127.0.0.1:6379",
        Auth: "",
        DB:   0,
    })
    route(m)
}
func route(m *apicache.CacheManager) {
    app := gin.Default()
    app.GET("/test-cache-second", apicache.CacheFunc(m, apicache.Ttl(time.Second), apicache.Single(true)), func(c *gin.Context) {
        time.Sleep(time.Second)
        fmt.Print("[/test-cache-second] DB select ...\n")
        c.String(200, "test-cache-second-res")
    })
    if err := app.Run(":8080"); err != nil {
        panic(err)
    }
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!