refactor(cache): 抽离 Redis 缓存初始化逻辑
- 新增 utility/cache.go 文件,定义全局的 RedisCache 变量 - 修改 hero.go 中的缓存使用方式,使用新的 RedisCache 全局变量 - 更新 main.go,移除冗余的包引用
This commit is contained in:
@@ -6,9 +6,9 @@ import (
|
||||
"epic/internal/dao"
|
||||
"epic/internal/model/entity"
|
||||
"epic/internal/service"
|
||||
"epic/utility"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -39,16 +39,10 @@ func (l *Logic) GetHeroByCode(ctx context.Context, code string) (*entity.EpicHer
|
||||
|
||||
// GetHeroList 查询所有英雄信息,并按创建时间倒序排列
|
||||
func (l *Logic) GetHeroList(ctx context.Context) ([]*v1.EpicHeroVO, error) {
|
||||
// 推荐从配置文件加载
|
||||
redisClient := g.Redis()
|
||||
cache := gcache.New()
|
||||
cache.SetAdapter(gcache.NewAdapterRedis(redisClient))
|
||||
// Create redis cache adapter and set it to cache object.
|
||||
|
||||
//fmt.Println(cache.MustGet(ctx, "epic_artifact_map_key").String())
|
||||
cache.Set(ctx, "epic_artifact_map_key111", "545487878", 1000*time.Second)
|
||||
cache.Set(ctx, "epic_artifact_map_key222", "5454878787878878", 0)
|
||||
fmt.Println(cache.Get(ctx, "epic_artifact_map_key111"))
|
||||
utility.RedisCache.Set(ctx, "epic_artifact_map_key111", "122", 1000*time.Second)
|
||||
utility.RedisCache.Set(ctx, "epic_artifact_map_key222", "6565", 0)
|
||||
fmt.Println(utility.RedisCache.Get(ctx, "NAME"))
|
||||
|
||||
var (
|
||||
doList []*entity.EpicHeroInfo // 数据库原始结构
|
||||
|
||||
Reference in New Issue
Block a user