-
Notifications
You must be signed in to change notification settings - Fork 408
/
csrftokencache.go
44 lines (34 loc) · 934 Bytes
/
csrftokencache.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
package client
import (
"time"
"github.com/kyma-project/kyma/components/central-application-gateway/internal/csrf"
cache "github.com/patrickmn/go-cache"
)
// Cache for CSRF data items
type TokenCache interface {
Get(itemID string) (resp *csrf.Response, found bool)
Add(itemID string, resp *csrf.Response)
Remove(itemID string)
}
// Creates a new TokenCache instance
func NewTokenCache() TokenCache {
return &tokenCache{
cache: cache.New(cache.NoExpiration, cache.NoExpiration),
}
}
type tokenCache struct {
cache *cache.Cache
}
func (tc *tokenCache) Get(itemID string) (resp *csrf.Response, found bool) {
res, found := tc.cache.Get(itemID)
if !found {
return nil, false
}
return res.(*csrf.Response), found
}
func (tc *tokenCache) Add(itemID string, resp *csrf.Response) {
tc.cache.Set(itemID, resp, time.Duration(-1)*time.Second)
}
func (tc *tokenCache) Remove(itemID string) {
tc.cache.Delete(itemID)
}