English | 🇨🇳中文
A high performance gin middleware to cache http response. Compared to gin-contrib/cache. It has a huge performance improvement.
- Has a huge performance improvement compared to gin-contrib/cache.
- Cache http response in local memory or Redis.
- Offer a way to custom the cache strategy by per request.
- Use singleflight to avoid cache breakdown problem.
- Only Cache 2xx HTTP Response.
go get -u github.com/chenyahui/gin-cache
package main
import (
"time"
"github.com/chenyahui/gin-cache"
"github.com/chenyahui/gin-cache/persist"
"github.com/gin-gonic/gin"
)
func main() {
app := gin.New()
memoryStore := persist.NewMemoryStore(1 * time.Minute)
app.GET("/hello",
cache.CacheByRequestURI(memoryStore, 2*time.Second),
func(c *gin.Context) {
c.String(200, "hello world")
},
)
if err := app.Run(":8080"); err != nil {
panic(err)
}
}
package main
import (
"time"
"github.com/chenyahui/gin-cache"
"github.com/chenyahui/gin-cache/persist"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
)
func main() {
app := gin.New()
redisStore := persist.NewRedisStore(redis.NewClient(&redis.Options{
Network: "tcp",
Addr: "127.0.0.1:6379",
}))
app.GET("/hello",
cache.CacheByRequestURI(redisStore, 2*time.Second),
func(c *gin.Context) {
c.String(200, "hello world")
},
)
if err := app.Run(":8080"); err != nil {
panic(err)
}
}
wrk -c 500 -d 1m -t 5 http://127.0.0.1:8080/hello