Add redis support for distributed caching (#83)

* Add redis support for distributed caching

* Move towards the self-provided Redis connection pool and RESP protocol implementation.
Official redis client library won't work with yaegi.

* fixup! Move towards the self-provided Redis connection pool and RESP protocol implementation. Official redis client library won't work with yaegi.

* fixup! fixup! Move towards the self-provided Redis connection pool and RESP protocol implementation. Official redis client library won't work with yaegi.

* fixup! fixup! fixup! Move towards the self-provided Redis connection pool and RESP protocol implementation. Official redis client library won't work with yaegi.

* fixup! fixup! fixup! fixup! Move towards the self-provided Redis connection pool and RESP protocol implementation. Official redis client library won't work with yaegi.

* fixup! fixup! fixup! fixup! fixup! Move towards the self-provided Redis connection pool and RESP protocol implementation. Official redis client library won't work with yaegi.

* ... and another all nighter.

* fixup! ... and another all nighter.

* fixup! fixup! ... and another all nighter.

* fixup! fixup! fixup! ... and another all nighter.

* Resolve issue #85 by adding ability to set custom claims in JWT tokens

* Remove redundant validation in auth middleware ( issue #89 )

* Add ability to set cookie prefix for session cookies ( #87 )

* fixup! Add ability to set cookie prefix for session cookies ( #87 )

* Add ability to set cookie max age - issue #91

* Potential fix for code scanning alert no. 10: Size computation for allocation may overflow

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* fixup! Merge main into 0.8.0-redis: resolve conflicts

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This commit is contained in:
2025-11-30 02:18:46 +00:00
committed by GitHub
co-authored by Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
parent 5fcbd54955
commit e64fc7f730
318 changed files with 100989 additions and 948 deletions
+29 -4
View File
@@ -10,6 +10,12 @@ import (
"time"
)
const (
// metadataCacheVersion is incremented when cache format changes
// This ensures old cached data is automatically ignored
metadataCacheVersion = "v2"
)
// MetadataCache wraps UniversalCache for metadata operations
type MetadataCache struct {
cache *UniversalCache
@@ -17,6 +23,11 @@ type MetadataCache struct {
wg *sync.WaitGroup
}
// versionedKey adds version prefix to cache keys
func (mc *MetadataCache) versionedKey(key string) string {
return metadataCacheVersion + ":" + key
}
// MetadataCacheEntry for compatibility
type MetadataCacheEntry struct {
}
@@ -55,12 +66,14 @@ func (mc *MetadataCache) Set(providerURL string, metadata *ProviderMetadata, ttl
return fmt.Errorf("failed to marshal metadata: %w", err)
}
return mc.cache.Set(providerURL, data, ttl)
// Use versioned key to prevent stale data issues
return mc.cache.Set(mc.versionedKey(providerURL), data, ttl)
}
// Get retrieves provider metadata from cache
func (mc *MetadataCache) Get(providerURL string) (*ProviderMetadata, bool) {
value, exists := mc.cache.Get(providerURL)
// Use versioned key to prevent stale data issues
value, exists := mc.cache.Get(mc.versionedKey(providerURL))
if !exists {
mc.logger.Debugf("MetadataCache: MISS for %s", providerURL)
return nil, false
@@ -78,9 +91,21 @@ func (mc *MetadataCache) Get(providerURL string) (*ProviderMetadata, bool) {
return nil, false
}
// Debug: log first 100 chars of cached data to diagnose unmarshal issues
dataPreview := string(data)
if len(dataPreview) > 100 {
dataPreview = dataPreview[:100]
}
mc.logger.Debugf("MetadataCache: Attempting to unmarshal for %s, data preview: %s", providerURL, dataPreview)
var metadata ProviderMetadata
if err := json.Unmarshal(data, &metadata); err != nil {
mc.logger.Errorf("MetadataCache: Failed to unmarshal metadata for %s: %v", providerURL, err)
// Graceful degradation: corrupt data is treated as cache miss
mc.logger.Errorf("MetadataCache: Corrupt data detected for %s: %v (preview: %s) - deleting and treating as miss", providerURL, err, dataPreview)
// Delete corrupt entry to prevent repeated errors (use versioned key)
mc.cache.Delete(mc.versionedKey(providerURL))
return nil, false
}
@@ -183,7 +208,7 @@ func (mc *MetadataCache) CleanupExpired() {
// Delete removes an entry from the cache
func (mc *MetadataCache) Delete(key string) {
mc.cache.Delete(key)
mc.cache.Delete(mc.versionedKey(key))
}
// Mutex returns the cache mutex for testing