mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-12 04:04:26 +00:00
Ensure cleanups actually happen.
This commit is contained in:
@@ -37,6 +37,10 @@ type Cache struct {
|
|||||||
|
|
||||||
// maxSize is the maximum number of items allowed in the cache.
|
// maxSize is the maximum number of items allowed in the cache.
|
||||||
maxSize int
|
maxSize int
|
||||||
|
// autoCleanupInterval defines how often Cleanup is called automatically.
|
||||||
|
autoCleanupInterval time.Duration
|
||||||
|
// stopCleanup channel to terminate the auto cleanup goroutine.
|
||||||
|
stopCleanup chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultMaxSize is the default maximum number of items in the cache.
|
// DefaultMaxSize is the default maximum number of items in the cache.
|
||||||
@@ -44,12 +48,16 @@ const DefaultMaxSize = 500
|
|||||||
|
|
||||||
// NewCache creates a new empty cache instance that is ready for use.
|
// NewCache creates a new empty cache instance that is ready for use.
|
||||||
func NewCache() *Cache {
|
func NewCache() *Cache {
|
||||||
return &Cache{
|
c := &Cache{
|
||||||
items: make(map[string]CacheItem, DefaultMaxSize),
|
items: make(map[string]CacheItem, DefaultMaxSize),
|
||||||
order: list.New(),
|
order: list.New(),
|
||||||
elems: make(map[string]*list.Element, DefaultMaxSize),
|
elems: make(map[string]*list.Element, DefaultMaxSize),
|
||||||
maxSize: DefaultMaxSize,
|
maxSize: DefaultMaxSize,
|
||||||
|
autoCleanupInterval: 5 * time.Minute,
|
||||||
|
stopCleanup: make(chan struct{}),
|
||||||
}
|
}
|
||||||
|
go c.startAutoCleanup()
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set adds or updates an item in the cache with the specified expiration duration.
|
// Set adds or updates an item in the cache with the specified expiration duration.
|
||||||
@@ -167,3 +175,22 @@ func (c *Cache) removeItem(key string) {
|
|||||||
delete(c.elems, key)
|
delete(c.elems, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// startAutoCleanup initiates a goroutine that periodically cleans up expired cache items.
|
||||||
|
func (c *Cache) startAutoCleanup() {
|
||||||
|
ticker := time.NewTicker(c.autoCleanupInterval)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
c.Cleanup()
|
||||||
|
case <-c.stopCleanup:
|
||||||
|
ticker.Stop()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close terminates the auto cleanup goroutine.
|
||||||
|
func (c *Cache) Close() {
|
||||||
|
close(c.stopCleanup)
|
||||||
|
}
|
||||||
|
|||||||
+26
-3
@@ -7,16 +7,21 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MetadataCache provides thread-safe caching for OIDC provider metadata
|
|
||||||
type MetadataCache struct {
|
type MetadataCache struct {
|
||||||
metadata *ProviderMetadata
|
metadata *ProviderMetadata
|
||||||
expiresAt time.Time
|
expiresAt time.Time
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
|
autoCleanupInterval time.Duration
|
||||||
|
stopCleanup chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMetadataCache creates a new metadata cache instance
|
|
||||||
func NewMetadataCache() *MetadataCache {
|
func NewMetadataCache() *MetadataCache {
|
||||||
return &MetadataCache{}
|
c := &MetadataCache{
|
||||||
|
autoCleanupInterval: 5 * time.Minute,
|
||||||
|
stopCleanup: make(chan struct{}),
|
||||||
|
}
|
||||||
|
go c.startAutoCleanup()
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup removes expired metadata from the cache.
|
// Cleanup removes expired metadata from the cache.
|
||||||
@@ -69,5 +74,23 @@ if usageCount < 10 {
|
|||||||
c.expiresAt = time.Now().Add(2 * time.Hour)
|
c.expiresAt = time.Now().Add(2 * time.Hour)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// End of GetMetadata
|
||||||
return metadata, nil
|
return metadata, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *MetadataCache) startAutoCleanup() {
|
||||||
|
ticker := time.NewTicker(c.autoCleanupInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
c.Cleanup()
|
||||||
|
case <-c.stopCleanup:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *MetadataCache) Close() {
|
||||||
|
close(c.stopCleanup)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user