mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-24 20:18:13 +02:00
zero: merge pomerium/zero-sdk (#4848)
This commit is contained in:
parent
c4dd965f2d
commit
ea64902a73
54 changed files with 4800 additions and 170 deletions
55
pkg/zero/cluster/urlcache.go
Normal file
55
pkg/zero/cluster/urlcache.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package cluster
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// URLCache is a cache of URLs to download bundles from.
|
||||
type URLCache struct {
|
||||
mx sync.RWMutex
|
||||
cache map[string]DownloadCacheEntry
|
||||
}
|
||||
|
||||
// DownloadCacheEntry is a cache entry for a URL to download a bundle from.
|
||||
type DownloadCacheEntry struct {
|
||||
// URL is the URL to download the bundle from.
|
||||
URL url.URL
|
||||
// ExpiresAt is the time at which the URL expires.
|
||||
ExpiresAt time.Time
|
||||
// CaptureHeaders is a list of headers to capture from the response.
|
||||
CaptureHeaders []string
|
||||
}
|
||||
|
||||
// NewURLCache creates a new URL cache.
|
||||
func NewURLCache() *URLCache {
|
||||
return &URLCache{
|
||||
cache: make(map[string]DownloadCacheEntry),
|
||||
}
|
||||
}
|
||||
|
||||
// Get gets the cache entry for the given key, if it exists and has not expired.
|
||||
func (c *URLCache) Get(key string, minTTL time.Duration) (*DownloadCacheEntry, bool) {
|
||||
c.mx.RLock()
|
||||
defer c.mx.RUnlock()
|
||||
|
||||
entry, ok := c.cache[key]
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if time.Until(entry.ExpiresAt) < minTTL {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return &entry, true
|
||||
}
|
||||
|
||||
// Set sets the cache entry for the given key.
|
||||
func (c *URLCache) Set(key string, entry DownloadCacheEntry) {
|
||||
c.mx.Lock()
|
||||
defer c.mx.Unlock()
|
||||
|
||||
c.cache[key] = entry
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue