cache
You can read source code of this middleware on GitHubopen in new window and API documentation on .
The minimum requirement of Go is 1.16.
The open in new window works out-of-the-box with an optional and uses memory as the storage backend:
package main
import (
"net/http"
"time"
"github.com/flamego/cache"
"github.com/flamego/flamego"
)
func main() {
f := flamego.Classic()
f.Use(cache.Cacher())
f.Get("/set", func(r *http.Request, cache cache.Cache) error {
return cache.Set(r.Context(), "cooldown", true, time.Minute)
})
f.Get("/get", func(r *http.Request, cache cache.Cache) string {
v, err := cache.Get(r.Context(), "cooldown")
if err != nil && err != os.ErrNotExist {
return err.Error()
}
cooldown, ok := v.(bool)
if !ok || !cooldown {
return "It has been cooled"
}
return "Still hot"
})
f.Run()
}
File
The is the function to initialize a file storage backend, used together with cache.FileConfig
open in new window to customize the backend:
The is the function to initialize a PostgreSQL storage backend, used together with postgres.Config
open in new window to customize the backend:
package main
"net/http"
"os"
"time"
"github.com/flamego/cache/postgres"
"github.com/flamego/flamego"
)
func main() {
f := flamego.Classic()
dsn := os.ExpandEnv("postgres://$PGUSER:$PGPASSWORD@$PGHOST:$PGPORT/$PGDATABASE?sslmode=$PGSSLMODE")
f.Use(cache.Cacher(
cache.Options{
Initer: postgres.Initer(),
Config: postgres.Config{
DSN: dsn,
Table: "cache",
InitTable: true,
},
},
))
f.Get("/set", func(r *http.Request, cache cache.Cache) error {
return cache.Set(r.Context(), "cooldown", true, time.Minute)
})
f.Get("/get", func(r *http.Request, cache cache.Cache) string {
v, err := cache.Get(r.Context(), "cooldown")
if err != nil && err != os.ErrNotExist {
return err.Error()
}
cooldown, ok := v.(bool)
if !ok || !cooldown {
return "It has been cooled"
}
return "Still hot"
})
f.Run()
}
MySQL
The mysql.Initer
open in new window is the function to initialize a MySQL storage backend, used together with to customize the backend:
package main
import (
"os"
"time"
"github.com/flamego/cache"
"github.com/flamego/cache/redis"
"github.com/flamego/flamego"
)
func main() {
f := flamego.Classic()
f.Use(cache.Cacher(
cache.Options{
Initer: redis.Initer(),
Config: redis.Config{
Options: &redis.Options{
Addr: os.ExpandEnv("$REDIS_HOST:$REDIS_PORT"),
DB: 15,
},
},
},
))
f.Get("/set", func(r *http.Request, cache cache.Cache) error {
return cache.Set(r.Context(), "cooldown", true, time.Minute)
})
f.Get("/get", func(r *http.Request, cache cache.Cache) string {
v, err := cache.Get(r.Context(), "cooldown")
if err != nil && err != os.ErrNotExist {
return err.Error()
}
cooldown, ok := v.(bool)
if !ok || !cooldown {
return "It has been cooled"
}
return "Still hot"
})
f.Run()
}
MongoDB
The is the function to initialize a MongoDB storage backend, used together with mongo.Config
open in new window to customize the backend:
The default encoder and decoder of cache data use , and only limited types are supported for values. When you encounter errors like encode: gob: type not registered for interface: time.Duration
, you can use open in new window to register the type for encoding and decoding.
For example: