Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,67 @@ require (
github.com/oj-lab/go-webmods v0.1.4
github.com/redis/go-redis/v9 v9.12.1
github.com/spf13/viper v1.20.1
github.com/testcontainers/testcontainers-go v0.39.0
github.com/volcengine/ve-tos-golang-sdk/v2 v2.7.21
google.golang.org/grpc v1.74.2
google.golang.org/grpc v1.75.1
gorm.io/driver/mysql v1.6.0
gorm.io/driver/postgres v1.6.0
gorm.io/driver/sqlite v1.6.0
gorm.io/gorm v1.30.1
)

require (
dario.cat/mergo v1.0.2 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/errdefs/pkg v0.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/cpuguy83/dockercfg v0.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v28.3.3+incompatible // indirect
github.com/docker/go-connections v0.6.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/ebitengine/purego v0.8.4 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.10 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/go-archive v0.1.0 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.6.0 // indirect
github.com/moby/sys/user v0.4.0 // indirect
github.com/moby/sys/userns v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shirou/gopsutil/v4 v4.25.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/testify v1.11.1 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.38.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.38.0 // indirect
go.opentelemetry.io/otel/metric v1.38.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.38.0 // indirect
go.opentelemetry.io/otel/trace v1.38.0 // indirect
go.opentelemetry.io/proto/otlp v1.9.0 // indirect
)

require (
Expand Down Expand Up @@ -68,9 +118,9 @@ require (
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/net v0.43.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/text v0.28.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250811230008-5f3141c8851a // indirect
google.golang.org/protobuf v1.36.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250825161204-c5933d9347a5 // indirect
google.golang.org/protobuf v1.36.10 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
161 changes: 139 additions & 22 deletions go.sum

Large diffs are not rendered by default.

140 changes: 101 additions & 39 deletions redis_client/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"time"

"github.com/go-redis/cache/v9"
"github.com/redis/go-redis/v9"
)

var (
cacheRefreshEventChannel = "cacheRefreshEventChannel"
cacheClient *cache.Cache
cacheInstance *Cache
cacheInstanceOnce sync.Once
)
Expand All @@ -23,51 +23,113 @@ func SetCacheRefreshEventChannel(channel string) {

type Cache struct {
*cache.Cache
rdb redis.UniversalClient
refreshEventChannel string
}

func GetCache() *Cache {
cacheInstanceOnce.Do(func() {
cacheClient = cache.New(&cache.Options{
Redis: GetRDB(),
LocalCache: cache.NewTinyLFU(1000, time.Minute),
})
cacheInstance = &Cache{Cache: cacheClient}
// CacheConfig holds configuration for creating a new cache instance.
type CacheConfig struct {
// Redis client to use for the cache. Required.
Redis redis.UniversalClient
// RefreshEventChannel is the name of the pub/sub channel for cache invalidation events.
// Optional. Defaults to "cacheRefreshEventChannel".
RefreshEventChannel string
// LocalCacheSize is the maximum number of entries in the local cache.
// Optional. Defaults to 1000.
LocalCacheSize int
// LocalCacheTTL is the time-to-live for entries in the local cache.
// Optional. Defaults to 1 minute.
LocalCacheTTL time.Duration
}

// Subscribe cache refresh event
ctx := context.Background()
_, err := GetRDB().Set(ctx, cacheRefreshEventChannel, cacheRefreshEventChannel, 0).Result()
if err != nil {
panic(err)
}
go func() {
pubsub := GetRDB().Subscribe(ctx, cacheRefreshEventChannel)
defer func() {
err := pubsub.Close()
if err != nil {
slog.Error("Error closing pubsub", "error", err)
}
}()
slog.Info(
"Subscribed to cache refresh event channel", "channel", cacheRefreshEventChannel)
ch := pubsub.Channel()
for {
select {
case msg := <-ch:
if msg.Payload == cacheRefreshEventChannel {
slog.Info("Cache refresh event received", "key", msg.Payload)
cacheInstance.DeleteFromLocalCache(msg.Payload)
}
case <-ctx.Done():
return
}
// NewCache creates a new cache instance with the provided configuration.
// This function supports creating multiple cache instances with different Redis clients.
//
// Example:
//
// rdb := redis_client.NewRDB(redis_client.Config{
// Urls: []string{"localhost:6379"},
// Password: "",
// })
// cache := redis_client.NewCache(redis_client.CacheConfig{
// Redis: rdb,
// })
func NewCache(cfg CacheConfig) *Cache {
if cfg.Redis == nil {
panic("redis_client: Redis client is required for cache")
}

// Set defaults
refreshEventChannel := cfg.RefreshEventChannel
if refreshEventChannel == "" {
refreshEventChannel = "cacheRefreshEventChannel"
}
localCacheSize := cfg.LocalCacheSize
if localCacheSize == 0 {
localCacheSize = 1000
}
localCacheTTL := cfg.LocalCacheTTL
if localCacheTTL == 0 {
localCacheTTL = time.Minute
}

cacheClient := cache.New(&cache.Options{
Redis: cfg.Redis,
LocalCache: cache.NewTinyLFU(localCacheSize, localCacheTTL),
})
cacheInstance := &Cache{
Cache: cacheClient,
rdb: cfg.Redis,
refreshEventChannel: refreshEventChannel,
}

// Subscribe cache refresh event
ctx := context.Background()
_, err := cfg.Redis.Set(ctx, refreshEventChannel, refreshEventChannel, 0).Result()
if err != nil {
panic(err)
}
go func() {
pubsub := cfg.Redis.Subscribe(ctx, refreshEventChannel)
defer func() {
err := pubsub.Close()
if err != nil {
slog.Error("Error closing pubsub", "error", err)
}
}()
slog.Info(
"Subscribed to cache refresh event channel", "channel", refreshEventChannel)
ch := pubsub.Channel()
for {
select {
case msg := <-ch:
slog.Info("Cache refresh event received", "key", msg.Payload)
cacheInstance.DeleteFromLocalCache(msg.Payload)
case <-ctx.Done():
return
}
}
}()

return cacheInstance
}

// GetCache returns the singleton cache instance.
// The cache is initialized on first call using the configuration set by SetConfig.
// Subsequent calls return the same instance (thread-safe).
// Deprecated: Use NewCache instead for better control and to support multiple cache instances.
func GetCache() *Cache {
cacheInstanceOnce.Do(func() {
cacheInstance = NewCache(CacheConfig{
Redis: GetRDB(),
RefreshEventChannel: cacheRefreshEventChannel,
})
})
return cacheInstance
}

func publishCacheRefreshEvent(ctx context.Context, key string) error {
return GetRDB().Publish(ctx, cacheRefreshEventChannel, key).Err()
func (c *Cache) publishCacheRefreshEvent(ctx context.Context, key string) error {
return c.rdb.Publish(ctx, c.refreshEventChannel, key).Err()
}

func (c *Cache) Get(ctx context.Context, key string, value any) error {
Expand All @@ -83,12 +145,12 @@ func (c *Cache) Set(ctx context.Context, key string, value any, expiration time.
}); err != nil {
return err
}
return publishCacheRefreshEvent(ctx, key)
return c.publishCacheRefreshEvent(ctx, key)
}

func (c *Cache) Delete(ctx context.Context, key string) error {
if err := c.Cache.Delete(ctx, key); err != nil {
return err
}
return publishCacheRefreshEvent(ctx, key)
return c.publishCacheRefreshEvent(ctx, key)
}
Loading