https://github.com/gin-contrib/cache
Gin middleware/handler to enable Cache
https://github.com/gin-contrib/cache
Last synced: 11 days ago
JSON representation
Gin middleware/handler to enable Cache
- Host: GitHub
- URL: https://github.com/gin-contrib/cache
- Owner: gin-contrib
- License: mit
- Created: 2016-11-14T16:25:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-05-10T00:47:07.000Z (about 1 year ago)
- Last Synced: 2024-05-10T01:41:16.914Z (about 1 year ago)
- Language: Go
- Homepage: https://gin-gonic.github.io/gin
- Size: 192 KB
- Stars: 351
- Watchers: 13
- Forks: 94
- Open Issues: 35
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-gin - gin-contrib/cache
README
# Cache middleware
[](https://github.com/gin-contrib/cache/actions/workflows/testing.yml)
[](https://codecov.io/gh/gin-contrib/cache)
[](https://goreportcard.com/report/github.com/gin-contrib/cache)
[](https://godoc.org/github.com/gin-contrib/cache)Gin middleware/handler to enable Cache.
- [Cache middleware](#cache-middleware)
- [Usage](#usage)
- [Start using it](#start-using-it)
- [InMemory Example](#inmemory-example)
- [Redis Example](#redis-example)## Usage
### Start using it
Download and install it:
```sh
go get github.com/gin-contrib/cache
```Import it in your code:
```go
import "github.com/gin-contrib/cache"
```### InMemory Example
See the [example](example/example.go)
```go
package mainimport (
"fmt"
"time""github.com/gin-contrib/cache"
"github.com/gin-contrib/cache/persistence"
"github.com/gin-gonic/gin"
)func main() {
r := gin.Default()store := persistence.NewInMemoryStore(time.Second)
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
})
// Cached Page
r.GET("/cache_ping", cache.CachePage(store, time.Minute, func(c *gin.Context) {
c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
}))// Listen and Server in 0.0.0.0:8080
r.Run(":8080")
}
```You can also use the `Delete` and `Flush` methods with the InMemory store:
```go
// Delete a specific cache entry by key
err := store.Delete("your-cache-key")
if err != nil {
// handle error
}// Flush all cache entries
err = store.Flush()
if err != nil {
// handle error
}
```### Redis Example
Here is a complete example using Redis as the cache backend with `NewRedisCacheWithURL`:
```go
package mainimport (
"fmt"
"time""github.com/gin-contrib/cache"
"github.com/gin-contrib/cache/persistence"
"github.com/gin-gonic/gin"
)func main() {
r := gin.Default()// Basic usage:
store := persistence.NewRedisCacheWithURL("redis://localhost:6379", time.Minute)// Advanced configuration with password and DB number:
// store := persistence.NewRedisCacheWithURL("redis://:password@localhost:6379/0", time.Minute)r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
})
// Cached Page
r.GET("/cache_ping", cache.CachePage(store, time.Minute, func(c *gin.Context) {
c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
}))// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
}
```