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:

    1. package main
    2. import (
    3. "net/http"
    4. "time"
    5. "github.com/flamego/cache"
    6. "github.com/flamego/flamego"
    7. )
    8. func main() {
    9. f := flamego.Classic()
    10. f.Use(cache.Cacher())
    11. f.Get("/set", func(r *http.Request, cache cache.Cache) error {
    12. return cache.Set(r.Context(), "cooldown", true, time.Minute)
    13. })
    14. f.Get("/get", func(r *http.Request, cache cache.Cache) string {
    15. v, err := cache.Get(r.Context(), "cooldown")
    16. if err != nil && err != os.ErrNotExist {
    17. return err.Error()
    18. }
    19. cooldown, ok := v.(bool)
    20. if !ok || !cooldown {
    21. return "It has been cooled"
    22. }
    23. return "Still hot"
    24. })
    25. f.Run()
    26. }

    File

    The is the function to initialize a file storage backend, used together with cache.FileConfigcache - 图6open in new window to customize the backend:

    The is the function to initialize a PostgreSQL storage backend, used together with postgres.Configcache - 图8open in new window to customize the backend:

    1. package main
    2. "net/http"
    3. "os"
    4. "time"
    5. "github.com/flamego/cache/postgres"
    6. "github.com/flamego/flamego"
    7. )
    8. func main() {
    9. f := flamego.Classic()
    10. dsn := os.ExpandEnv("postgres://$PGUSER:$PGPASSWORD@$PGHOST:$PGPORT/$PGDATABASE?sslmode=$PGSSLMODE")
    11. f.Use(cache.Cacher(
    12. cache.Options{
    13. Initer: postgres.Initer(),
    14. Config: postgres.Config{
    15. DSN: dsn,
    16. Table: "cache",
    17. InitTable: true,
    18. },
    19. },
    20. ))
    21. f.Get("/set", func(r *http.Request, cache cache.Cache) error {
    22. return cache.Set(r.Context(), "cooldown", true, time.Minute)
    23. })
    24. f.Get("/get", func(r *http.Request, cache cache.Cache) string {
    25. v, err := cache.Get(r.Context(), "cooldown")
    26. if err != nil && err != os.ErrNotExist {
    27. return err.Error()
    28. }
    29. cooldown, ok := v.(bool)
    30. if !ok || !cooldown {
    31. return "It has been cooled"
    32. }
    33. return "Still hot"
    34. })
    35. f.Run()
    36. }

    MySQL

    The mysql.Initeropen in new window is the function to initialize a MySQL storage backend, used together with to customize the backend:

    1. package main
    2. import (
    3. "os"
    4. "time"
    5. "github.com/flamego/cache"
    6. "github.com/flamego/cache/redis"
    7. "github.com/flamego/flamego"
    8. )
    9. func main() {
    10. f := flamego.Classic()
    11. f.Use(cache.Cacher(
    12. cache.Options{
    13. Initer: redis.Initer(),
    14. Config: redis.Config{
    15. Options: &redis.Options{
    16. Addr: os.ExpandEnv("$REDIS_HOST:$REDIS_PORT"),
    17. DB: 15,
    18. },
    19. },
    20. },
    21. ))
    22. f.Get("/set", func(r *http.Request, cache cache.Cache) error {
    23. return cache.Set(r.Context(), "cooldown", true, time.Minute)
    24. })
    25. f.Get("/get", func(r *http.Request, cache cache.Cache) string {
    26. v, err := cache.Get(r.Context(), "cooldown")
    27. if err != nil && err != os.ErrNotExist {
    28. return err.Error()
    29. }
    30. cooldown, ok := v.(bool)
    31. if !ok || !cooldown {
    32. return "It has been cooled"
    33. }
    34. return "Still hot"
    35. })
    36. f.Run()
    37. }

    MongoDB

    The is the function to initialize a MongoDB storage backend, used together with mongo.Configcache - 图14open 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 cache - 图16open in new window to register the type for encoding and decoding.

    For example: