-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
57 lines (48 loc) · 1.32 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
package easycache
import (
"time"
"github.com/patrickmn/go-cache"
)
type EasyCache struct {
cache *cache.Cache
// TTL for each cache entry
TimeToLive int
// Interval of cache evictions
CleanUpInterval int
// Http Status code limit for which responses are cached. Defaults to 300
CacheIfStatusCodeLessThan int
// List of endpoints (Paths) to ignore
IgnoreEndpoints map[string]interface{}
// To dis/able logging
Logging bool
}
type CacheConfig struct {
TimeToLive int
CleanUpInterval int
CacheIfStatusCodeLessThan int
IgnoreEndpoints map[string]interface{}
Logging bool
}
func NewCache(conf CacheConfig) EasyCache {
if conf.TimeToLive == 0 {
conf.TimeToLive = 5
}
if conf.CleanUpInterval == 0 {
conf.CleanUpInterval = 10
}
if conf.CacheIfStatusCodeLessThan == 0 {
conf.CacheIfStatusCodeLessThan = 300
}
return EasyCache{
cache: cache.New(time.Minute*time.Duration(conf.TimeToLive),
time.Minute*time.Duration(conf.CleanUpInterval)),
TimeToLive: conf.TimeToLive,
CleanUpInterval: conf.CleanUpInterval,
CacheIfStatusCodeLessThan: conf.CacheIfStatusCodeLessThan,
IgnoreEndpoints: conf.IgnoreEndpoints,
Logging: conf.Logging,
}
}
func (ec *EasyCache) Cache() *cache.Cache {
return ec.cache
}