From 29d609ce0a63b516225083e180fb21a468a33496 Mon Sep 17 00:00:00 2001 From: Yavolte Date: Mon, 30 Jun 2025 11:11:34 +0800 Subject: [PATCH] fix cache bugs --- pkg/cache/redis/cache.go | 9 ++++++++- pkg/cache/redis/types.go | 13 +++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/cache/redis/cache.go b/pkg/cache/redis/cache.go index d7a0309..58b91a0 100644 --- a/pkg/cache/redis/cache.go +++ b/pkg/cache/redis/cache.go @@ -4,6 +4,8 @@ import ( "context" "encoding/json" "time" + + "git.nobla.cn/golang/aeus" ) type redisCache struct { @@ -57,7 +59,12 @@ func (c *redisCache) String() string { } func NewCache(opts ...Option) *redisCache { - return &redisCache{ + cache := &redisCache{ opts: newOptions(opts...), } + app := aeus.FromContext(cache.opts.context) + if app != nil { + cache.opts.prefix = app.Name() + ":" + cache.opts.prefix + } + return cache } diff --git a/pkg/cache/redis/types.go b/pkg/cache/redis/types.go index 32312e3..edaafcc 100644 --- a/pkg/cache/redis/types.go +++ b/pkg/cache/redis/types.go @@ -1,13 +1,16 @@ package redis import ( + "context" + "github.com/redis/go-redis/v9" ) type ( options struct { - client *redis.Client - prefix string + context context.Context + client *redis.Client + prefix string } Option func(*options) @@ -19,6 +22,12 @@ func WithClient(client *redis.Client) Option { } } +func WithContext(ctx context.Context) Option { + return func(o *options) { + o.context = ctx + } +} + func WithPrefix(prefix string) Option { return func(o *options) { o.prefix = prefix