|
|
@ -0,0 +1,50 @@ |
|
|
|
package vote_record |
|
|
|
|
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"github.com/gogf/gf/v2/frame/g" |
|
|
|
v1 "practice_ArticleVote_Go/api/vote_record/v1" |
|
|
|
"practice_ArticleVote_Go/internal/consts" |
|
|
|
"practice_ArticleVote_Go/internal/dao" |
|
|
|
"practice_ArticleVote_Go/internal/utils" |
|
|
|
) |
|
|
|
|
|
|
|
func (l *VoteRecordLogic) GetAllArticleList(ctx context.Context, req *v1.GetAllArticleReq) (res *v1.GetAllArticleListRes, err error) { |
|
|
|
redis := g.Redis() |
|
|
|
cacheKey := fmt.Sprintf(consts.ARTICLE_LIST) |
|
|
|
fmt.Println(cacheKey) |
|
|
|
data, _ := redis.Get(ctx, cacheKey) |
|
|
|
fmt.Println("ARTICLE_LIST-data", data) |
|
|
|
if data != nil { |
|
|
|
var cached v1.GetAllArticleListRes |
|
|
|
err = json.Unmarshal([]byte(data.String()), &cached) |
|
|
|
if err == nil { |
|
|
|
return &cached, nil |
|
|
|
} |
|
|
|
} |
|
|
|
var articleList []*v1.GetAllArticleRes |
|
|
|
err = dao.Article. |
|
|
|
Ctx(ctx). |
|
|
|
Fields( |
|
|
|
dao.Article.Columns().Id, |
|
|
|
dao.Article.Columns().ArticleTitle, |
|
|
|
dao.Article.Columns().VoteStatus, |
|
|
|
dao.Article.Columns().CreateTime, |
|
|
|
). |
|
|
|
OrderDesc(dao.Article.Columns().CreateTime). |
|
|
|
Scan(&articleList) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
res = &v1.GetAllArticleListRes{ |
|
|
|
ArticleList: articleList, |
|
|
|
} |
|
|
|
buf, err := json.Marshal(res) |
|
|
|
if err == nil { |
|
|
|
ttl := utils.GetCacheTTL() |
|
|
|
_ = redis.SetEX(ctx, cacheKey, string(buf), int64(ttl)) |
|
|
|
} |
|
|
|
return res, nil |
|
|
|
} |