mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 05:56:38 +08:00
8d8814b416
- Replaced custom map-based cache in apps/oauth/cache.go with standard pkg/cache/ram framework. - Implemented Redis Pub/Sub invalidation channels for distributed token and user cache synchronization. - Created apps/oauth/cache_test.go to verify local cache operations and pub/sub broadcasts. refactor(cache): generic RAM cache with CoW and unified preheating Replaced L2 Redis cache and old cache package with process-local generic pkg/cache/ram. Implemented Copy-on-Write for reads, fine-grained locks per type for writes, and unified preheating in bootstrap. Changed cache invalidation to lazy-loading to resolve SQLite deadlocks during transactions.
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package config
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/Rain-kl/Wavelet/internal/model"
|
|
"github.com/Rain-kl/Wavelet/internal/repository"
|
|
"github.com/Rain-kl/Wavelet/internal/testhelper"
|
|
)
|
|
|
|
func TestListVisibleSystemConfigsUsesRedisCache(t *testing.T) {
|
|
dbConn, _, cleanup := testhelper.SetupTestEnvironment(t)
|
|
defer cleanup()
|
|
ctx := context.Background()
|
|
|
|
if err := repository.InvalidateVisibleSystemConfigsCache(ctx); err != nil {
|
|
t.Fatalf("InvalidateVisibleSystemConfigsCache() error = %v", err)
|
|
}
|
|
|
|
if _, err := repository.ListVisibleSystemConfigs(ctx); err != nil {
|
|
t.Fatalf("ListVisibleSystemConfigs() warm cache error = %v", err)
|
|
}
|
|
|
|
if err := dbConn.Create(&model.SystemConfig{
|
|
Key: "cache_probe_public_key",
|
|
Value: "cache_probe_public_value",
|
|
Type: "system",
|
|
Visibility: model.ConfigVisibilityVisible,
|
|
Description: "cache probe",
|
|
}).Error; err != nil {
|
|
t.Fatalf("Create(cache_probe_public_key) error = %v", err)
|
|
}
|
|
|
|
cached, err := repository.ListVisibleSystemConfigs(ctx)
|
|
if err != nil {
|
|
t.Fatalf("ListVisibleSystemConfigs() cached call error = %v", err)
|
|
}
|
|
for _, item := range cached {
|
|
if item.Key == "cache_probe_public_key" {
|
|
t.Fatal("cached visible config list should be stale before invalidation")
|
|
}
|
|
}
|
|
|
|
// Since system configs are now purely cached in process-local RAM (L1) and not written to Redis (L2),
|
|
// we do not verify the existence of the legacy Redis key here.
|
|
|
|
if err := repository.InvalidateVisibleSystemConfigsCache(ctx); err != nil {
|
|
t.Fatalf("InvalidateVisibleSystemConfigsCache() error = %v", err)
|
|
}
|
|
|
|
refreshed, err := repository.ListVisibleSystemConfigs(ctx)
|
|
if err != nil {
|
|
t.Fatalf("ListVisibleSystemConfigs() refreshed call error = %v", err)
|
|
}
|
|
|
|
var found bool
|
|
for _, item := range refreshed {
|
|
if item.Key == "cache_probe_public_key" {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("refreshed visible config list should include newly created public config")
|
|
}
|
|
}
|