feat(internal): 集成 Redis 缓存并更新相关逻辑- 在 config.yaml 中修改 Redis配置,使用 db 1- 在 go.mod 中添加 redis相关依赖
- 更新 hero
This commit is contained in:
@@ -6,7 +6,10 @@ import (
|
||||
"epic/internal/dao"
|
||||
"epic/internal/model/entity"
|
||||
"epic/internal/service"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Logic struct{}
|
||||
@@ -36,17 +39,16 @@ func (l *Logic) GetHeroByCode(ctx context.Context, code string) (*entity.EpicHer
|
||||
|
||||
// GetHeroList 查询所有英雄信息,并按创建时间倒序排列
|
||||
func (l *Logic) GetHeroList(ctx context.Context) ([]*v1.EpicHeroVO, error) {
|
||||
redis := g.Redis()
|
||||
cacheKey := "hero:list"
|
||||
// 推荐从配置文件加载
|
||||
redisClient := g.Redis()
|
||||
cache := gcache.New()
|
||||
cache.SetAdapter(gcache.NewAdapterRedis(redisClient))
|
||||
// Create redis cache adapter and set it to cache object.
|
||||
|
||||
// 1. 先查缓存
|
||||
value, err := redis.Get(ctx, cacheKey)
|
||||
if err == nil && !value.IsEmpty() {
|
||||
var voList []*v1.EpicHeroVO
|
||||
if err := value.Scan(&voList); err == nil {
|
||||
return voList, nil
|
||||
}
|
||||
}
|
||||
//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"))
|
||||
|
||||
var (
|
||||
doList []*entity.EpicHeroInfo // 数据库原始结构
|
||||
@@ -54,7 +56,7 @@ func (l *Logic) GetHeroList(ctx context.Context) ([]*v1.EpicHeroVO, error) {
|
||||
)
|
||||
|
||||
// 2. 缓存未命中,查数据库
|
||||
err = dao.EpicHeroInfo.Ctx(ctx).
|
||||
err := dao.EpicHeroInfo.Ctx(ctx).
|
||||
OrderDesc(dao.EpicHeroInfo.Columns().CreateTime). // 按创建时间倒序
|
||||
Scan(&doList)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user