Skip to content
/ ginche Public

Ginche (gin(ca)che) is a simple gin-gonic middleware for caching HTTP responses

Notifications You must be signed in to change notification settings

chloyka/ginche

Repository files navigation

Badge Go Report Card Code coverage

Ginche

What is Ginche?

Ginche (gin(ca)che) is a simple gin-gonic middleware for caching HTTP responses in memory. It allows developers to cache Gin HTTP responses in memory, reducing the load on the server and improving response times.

Ideology of Ginche

Ginche was created for make this possible:

  1. Create fast caches with minimum overhead
  2. Define your own cache key generation rules
  3. Minimum effort for use in already finished projects
  4. Define your own exclusion rules

Benchmarks

Cache implementation (No middleware)

$ go test -bench . -benchmem

goos: darwin
goarch: arm64
pkg: github.com/chloyka/ginche

BenchmarkCache_Set-8     1318003               785.2 ns/op           261 B/op         10 allocs/op
BenchmarkCache_Get-8     1000000              1020 ns/op             314 B/op         12 allocs/op

Middleware

$ go test -bench . -benchmem

goos: darwin
goarch: arm64
pkg: github.com/chloyka/ginche

BenchmarkMiddleware-8            1000000              1725 ns/op            1584 B/op         16 allocs/op

Basic usage

package main

import (
    "github.com/chloyka/ginche"
    "github.com/gin-gonic/gin"
)

func main() {
    store := ginche.NewCache()
    r := gin.New()
    r.Use(ginche.Middleware(store, nil))
    r.GET("/ping", func(c *gin.Context) {
        c.String(200, "pong")
    })
}

Examples

See Full Examples

FAQ

Can i cache responses based on its request payload?

Yes! You can define your own cache key generation algos. Here is an Example

router.Use(ginche.Middleware(store, &ginche.Options{
    KeyFunc: func(ctx *gin.Context) string {
        if ctx.Request.Method == "POST" {
            // Use the full URL and the name field from the body as the cache key
            var body map[string]interface{}
            err := ctx.BindJSON(&body)
            if err != nil {
                // If there is an error, skip caching
                return ginche.SkipCacheKeyValue
            }
            return ctx.Request.URL.String() + body["name"].(string)
        } else {
            // Otherwise, skip caching
            return ginche.SkipCacheKeyValue
        }
    },
}))

TODO:

Implement Memcached storage

Feel free to Open issues, requesting features or contributing

About

Ginche (gin(ca)che) is a simple gin-gonic middleware for caching HTTP responses

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages