Skip to content

Commit

Permalink
add WithOnHitCache examples
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyahui committed Sep 2, 2022
1 parent b6151dd commit 19318c7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions examples/options/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
cache "github.com/chenyahui/gin-cache"
"github.com/chenyahui/gin-cache/persist"
"github.com/gin-gonic/gin"
"sync/atomic"
"time"
)

func main() {
app := gin.New()

memoryStore := persist.NewMemoryStore(1 * time.Minute)

var cacheHitCount int32
app.GET("/hello",
cache.CacheByRequestURI(
memoryStore,
2*time.Second,
cache.WithOnHitCache(func(c *gin.Context) {
atomic.AddInt32(&cacheHitCount, 1)
}),
),
func(c *gin.Context) {
c.String(200, "hello world")
},
)

app.GET("/get_hit_count", func(c *gin.Context) {
c.String(200, fmt.Sprintf("total hit count: %d", cacheHitCount))
})

if err := app.Run(":8080"); err != nil {
panic(err)
}
}

0 comments on commit 19318c7

Please sign in to comment.