This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
91 lines (72 loc) · 2.51 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// package fcache provides caching middleware for the Fiber web framework. The caching engine can be accessed through the Cache variable.
package fcache
import (
"github.com/gofiber/fiber/v2/utils"
"time"
"fmt"
"github.com/gofiber/fiber/v2"
gc "github.com/patrickmn/go-cache"
)
var (
Cache *gc.Cache
Config internalConfig
)
const (
AutoGenerateKey = ""
NoExpiration = gc.NoExpiration
)
func init() {
fmt.Println("fiber-cache is now deprecated, since Fiber v2.0.3+ now includes a bundled cache middleware.\nSee https://github.com/gofiber/fiber/tree/master/middleware/cache for more info.")
Config.DefaultTTL = 5 * time.Minute
Config.CleanupInterval = 10 * time.Minute
Cache = gc.New(Config.DefaultTTL, Config.CleanupInterval)
}
type CacheEntry struct {
Body []byte
StatusCode int
ContentType []byte
}
type internalConfig struct {
CleanupInterval time.Duration
DefaultTTL time.Duration
}
func createMiddleware(selectedKey string, ttl time.Duration) func(*fiber.Ctx) error {
return func(c *fiber.Ctx) error {
var key string
if selectedKey == AutoGenerateKey {
key = utils.ImmutableString(c.Path())
} else {
key = selectedKey
}
val, found := Cache.Get(key)
if found {
entry := val.(CacheEntry)
c.Response().SetBody(entry.Body)
c.Response().SetStatusCode(entry.StatusCode)
c.Response().Header.SetContentTypeBytes(entry.ContentType)
return nil
}
c.Locals("cacheKey", key)
err := c.Next()
if err == nil {
Cache.Set(key, CacheEntry{
Body: c.Response().Body(),
StatusCode: c.Response().StatusCode(),
ContentType: c.Response().Header.ContentType(),
}, ttl)
}
return err
}
}
// New returns a new instance of the caching middleware, with an automatically generated key and the default TTL.
func New() func(*fiber.Ctx) error {
return createMiddleware(AutoGenerateKey, Config.DefaultTTL)
}
// NewWithKey returns a new instance of the caching middleware with the default TTL and the option to set your own cache key. If this is an empty string or AutoGenerateKey, a key will be automatically generated.
func NewWithKey(key string) func(*fiber.Ctx) error {
return createMiddleware(key, Config.DefaultTTL)
}
// NewWithTTL returns a new instance of the caching middleware with the option to define your own cache key and your own TTL. If the cache key you set is an empty string or AutoGenerateKey, a key will be automatically generated.
func NewWithTTL(key string, ttl time.Duration) func(*fiber.Ctx) error {
return createMiddleware(key, ttl)
}