mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-14 04:07:46 +00:00
Weekend fun with further optimisations.
This commit is contained in:
+243
-26
@@ -1,36 +1,253 @@
|
||||
package traefikoidc
|
||||
|
||||
// Cache compatibility layer - maps old cache types to UnifiedCache
|
||||
import (
|
||||
"container/list"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Cache is now an alias for CacheAdapter wrapping UnifiedCache
|
||||
type Cache = CacheAdapter
|
||||
// Cache compatibility layer - maps old cache types to UniversalCache
|
||||
|
||||
// NewCache creates a UnifiedCache with default configuration
|
||||
// This maintains backward compatibility with existing code
|
||||
func NewCache() *CacheAdapter {
|
||||
config := DefaultUnifiedCacheConfig()
|
||||
unifiedCache := NewUnifiedCache(config)
|
||||
return NewCacheAdapter(unifiedCache)
|
||||
// NewCache creates a general purpose cache
|
||||
func NewCache() CacheInterface {
|
||||
config := UniversalCacheConfig{
|
||||
Type: CacheTypeGeneral,
|
||||
MaxSize: 1000,
|
||||
Logger: GetSingletonNoOpLogger(),
|
||||
}
|
||||
return &CacheInterfaceWrapper{
|
||||
cache: NewUniversalCache(config),
|
||||
}
|
||||
}
|
||||
|
||||
// OptimizedCache is now an alias for CacheAdapter wrapping UnifiedCache
|
||||
type OptimizedCache = CacheAdapter
|
||||
|
||||
// NewOptimizedCache creates a UnifiedCache with optimized configuration
|
||||
// This maintains backward compatibility with existing code
|
||||
func NewOptimizedCache() *CacheAdapter {
|
||||
config := DefaultUnifiedCacheConfig()
|
||||
config.EnableMemoryLimit = true
|
||||
config.Strategy = NewLRUStrategy(config.MaxSize)
|
||||
unifiedCache := NewUnifiedCache(config)
|
||||
return NewCacheAdapter(unifiedCache)
|
||||
// NewBoundedCache creates a bounded cache with specified max size
|
||||
func NewBoundedCache(maxSize int) CacheInterface {
|
||||
config := UniversalCacheConfig{
|
||||
Type: CacheTypeGeneral,
|
||||
MaxSize: maxSize,
|
||||
Logger: GetSingletonNoOpLogger(),
|
||||
}
|
||||
return &CacheInterfaceWrapper{
|
||||
cache: NewUniversalCache(config),
|
||||
}
|
||||
}
|
||||
|
||||
// OptimizedCacheConfig is an alias for UnifiedCacheConfig
|
||||
type OptimizedCacheConfig = UnifiedCacheConfig
|
||||
// BoundedCache is an alias for compatibility
|
||||
type BoundedCache = CacheInterfaceWrapper
|
||||
|
||||
// NewOptimizedCacheWithConfig creates a UnifiedCache with custom configuration
|
||||
func NewOptimizedCacheWithConfig(config OptimizedCacheConfig) *CacheAdapter {
|
||||
unifiedCache := NewUnifiedCache(config)
|
||||
return NewCacheAdapter(unifiedCache)
|
||||
// BoundedCacheAdapter is an alias for compatibility
|
||||
type BoundedCacheAdapter = CacheInterfaceWrapper
|
||||
|
||||
// UnifiedCache wraps UniversalCache for backward compatibility
|
||||
type UnifiedCache struct {
|
||||
*UniversalCache
|
||||
strategy CacheStrategy // For backward compatibility with tests
|
||||
}
|
||||
|
||||
// SetMaxSize sets the maximum cache size
|
||||
func (c *UnifiedCache) SetMaxSize(size int) {
|
||||
c.UniversalCache.SetMaxSize(size)
|
||||
}
|
||||
|
||||
// UnifiedCacheConfig is an alias for backward compatibility
|
||||
type UnifiedCacheConfig = UniversalCacheConfig
|
||||
|
||||
// DefaultUnifiedCacheConfig returns default config for backward compatibility
|
||||
func DefaultUnifiedCacheConfig() UniversalCacheConfig {
|
||||
return UniversalCacheConfig{
|
||||
Type: CacheTypeGeneral,
|
||||
MaxSize: 500,
|
||||
MaxMemoryBytes: 64 * 1024 * 1024,
|
||||
CleanupInterval: 2 * time.Minute,
|
||||
Logger: GetSingletonNoOpLogger(),
|
||||
}
|
||||
}
|
||||
|
||||
// NewUnifiedCache creates a universal cache for backward compatibility
|
||||
func NewUnifiedCache(config UniversalCacheConfig) *UnifiedCache {
|
||||
// Avoid circular reference by calling the real constructor
|
||||
cache := createUniversalCache(config)
|
||||
return &UnifiedCache{
|
||||
UniversalCache: cache,
|
||||
strategy: config.Strategy,
|
||||
}
|
||||
}
|
||||
|
||||
// CacheAdapter wraps UniversalCache for backward compatibility
|
||||
type CacheAdapter = CacheInterfaceWrapper
|
||||
|
||||
// NewCacheAdapter creates a cache adapter
|
||||
func NewCacheAdapter(cache interface{}) *CacheInterfaceWrapper {
|
||||
switch c := cache.(type) {
|
||||
case *UniversalCache:
|
||||
return &CacheInterfaceWrapper{cache: c}
|
||||
case *UnifiedCache:
|
||||
return &CacheInterfaceWrapper{cache: c.UniversalCache}
|
||||
default:
|
||||
// Try to convert to UniversalCache
|
||||
if uc, ok := cache.(*UniversalCache); ok {
|
||||
return &CacheInterfaceWrapper{cache: uc}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// OptimizedCache is an alias for backward compatibility
|
||||
type OptimizedCache = CacheInterfaceWrapper
|
||||
|
||||
// NewOptimizedCache creates an optimized cache
|
||||
func NewOptimizedCache() *CacheInterfaceWrapper {
|
||||
config := UniversalCacheConfig{
|
||||
Type: CacheTypeGeneral,
|
||||
MaxSize: 500,
|
||||
MaxMemoryBytes: 64 * 1024 * 1024,
|
||||
EnableMetrics: true,
|
||||
Logger: GetSingletonNoOpLogger(),
|
||||
}
|
||||
return &CacheInterfaceWrapper{
|
||||
cache: NewUniversalCache(config),
|
||||
}
|
||||
}
|
||||
|
||||
// LRUStrategy for backward compatibility
|
||||
type LRUStrategy struct {
|
||||
order *list.List
|
||||
elements map[string]*list.Element
|
||||
maxSize int
|
||||
}
|
||||
|
||||
func NewLRUStrategy(maxSize int) CacheStrategy {
|
||||
return &LRUStrategy{
|
||||
order: list.New(),
|
||||
elements: make(map[string]*list.Element),
|
||||
maxSize: maxSize,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *LRUStrategy) Name() string {
|
||||
return "LRU"
|
||||
}
|
||||
|
||||
func (s *LRUStrategy) ShouldEvict(item interface{}, now time.Time) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *LRUStrategy) OnAccess(key string, item interface{}) {}
|
||||
|
||||
func (s *LRUStrategy) OnRemove(key string) {}
|
||||
|
||||
func (s *LRUStrategy) EstimateSize(item interface{}) int64 {
|
||||
return 64
|
||||
}
|
||||
|
||||
func (s *LRUStrategy) GetEvictionCandidate() (key string, found bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// CacheStrategy interface for backward compatibility
|
||||
type CacheStrategy interface {
|
||||
Name() string
|
||||
ShouldEvict(item interface{}, now time.Time) bool
|
||||
OnAccess(key string, item interface{})
|
||||
OnRemove(key string)
|
||||
EstimateSize(item interface{}) int64
|
||||
GetEvictionCandidate() (key string, found bool)
|
||||
}
|
||||
|
||||
// CacheEntry for backward compatibility
|
||||
type CacheEntry struct {
|
||||
Key string
|
||||
Value interface{}
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// Cache is an alias for backward compatibility
|
||||
type Cache = CacheInterfaceWrapper
|
||||
|
||||
// OptimizedCacheConfig for backward compatibility
|
||||
type OptimizedCacheConfig = UniversalCacheConfig
|
||||
|
||||
// NewOptimizedCacheWithConfig creates cache with config
|
||||
func NewOptimizedCacheWithConfig(config OptimizedCacheConfig) *CacheInterfaceWrapper {
|
||||
return &CacheInterfaceWrapper{
|
||||
cache: NewUniversalCache(config),
|
||||
}
|
||||
}
|
||||
|
||||
// ListNode for backward compatibility
|
||||
type ListNode struct {
|
||||
Key string
|
||||
Value interface{}
|
||||
Next *ListNode
|
||||
Prev *ListNode
|
||||
}
|
||||
|
||||
// NewFixedMetadataCache creates a metadata cache with fixed configuration
|
||||
func NewFixedMetadataCache(args ...interface{}) *MetadataCache {
|
||||
// Accept variable arguments for backward compatibility
|
||||
// Expected args: maxSize, maxMemoryMB, logger
|
||||
logger := GetSingletonNoOpLogger()
|
||||
maxSize := 100 // default
|
||||
maxMemoryMB := int64(0) // default no limit
|
||||
|
||||
if len(args) > 0 {
|
||||
if size, ok := args[0].(int); ok {
|
||||
maxSize = size
|
||||
}
|
||||
}
|
||||
if len(args) > 1 {
|
||||
if memMB, ok := args[1].(int); ok {
|
||||
maxMemoryMB = int64(memMB) * 1024 * 1024 // Convert MB to bytes
|
||||
}
|
||||
}
|
||||
if len(args) > 2 {
|
||||
if l, ok := args[2].(*Logger); ok {
|
||||
logger = l
|
||||
}
|
||||
}
|
||||
|
||||
// Create a custom cache with the specified max size
|
||||
config := UniversalCacheConfig{
|
||||
Type: CacheTypeMetadata,
|
||||
MaxSize: maxSize,
|
||||
MaxMemoryBytes: maxMemoryMB,
|
||||
DefaultTTL: 1 * time.Hour,
|
||||
MetadataConfig: &MetadataCacheConfig{
|
||||
GracePeriod: 5 * time.Minute,
|
||||
ExtendedGracePeriod: 15 * time.Minute,
|
||||
MaxGracePeriod: 30 * time.Minute,
|
||||
SecurityCriticalMaxGracePeriod: 15 * time.Minute,
|
||||
},
|
||||
Logger: logger,
|
||||
}
|
||||
|
||||
cache := NewUniversalCache(config)
|
||||
return &MetadataCache{
|
||||
cache: cache,
|
||||
logger: logger,
|
||||
wg: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// DoublyLinkedList for backward compatibility
|
||||
type DoublyLinkedList struct {
|
||||
*list.List
|
||||
}
|
||||
|
||||
// NewDoublyLinkedList creates a new doubly linked list
|
||||
func NewDoublyLinkedList() *DoublyLinkedList {
|
||||
return &DoublyLinkedList{
|
||||
List: list.New(),
|
||||
}
|
||||
}
|
||||
|
||||
// PopFront removes and returns the front element
|
||||
func (l *DoublyLinkedList) PopFront() interface{} {
|
||||
if l.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
elem := l.Front()
|
||||
if elem != nil {
|
||||
return l.Remove(elem)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user