forked from kyma-project/kyma
/
proxycache.go
50 lines (39 loc) · 1.13 KB
/
proxycache.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
package proxycache
import (
"net/http/httputil"
"time"
"github.com/patrickmn/go-cache"
)
const cleanupInterval = 60
type Proxy struct {
Proxy *httputil.ReverseProxy
OauthUrl string
ClientId string
ClientSecret string
}
type HTTPProxyCache interface {
Get(id string) (*Proxy, bool)
Add(id, oauthUrl, clientId, clientSecret string, proxy *httputil.ReverseProxy) *Proxy
}
type httpProxyCache struct {
skipVerify bool
proxyCache *cache.Cache
}
func NewProxyCache(skipVerify bool, proxyCacheTTL int) HTTPProxyCache {
return &httpProxyCache{
skipVerify: skipVerify,
proxyCache: cache.New(time.Duration(proxyCacheTTL)*time.Second, cleanupInterval*time.Second),
}
}
func (p *httpProxyCache) Get(id string) (*Proxy, bool) {
proxy, found := p.proxyCache.Get(id)
if !found {
return nil, false
}
return proxy.(*Proxy), found
}
func (p *httpProxyCache) Add(id, oauthUrl, clientId, clientSecret string, reverseProxy *httputil.ReverseProxy) *Proxy {
proxy := &Proxy{Proxy: reverseProxy, OauthUrl: oauthUrl, ClientId: clientId, ClientSecret: clientSecret}
p.proxyCache.Set(id, proxy, cache.DefaultExpiration)
return proxy
}