Browse Source

feat: 清除缓存

zhangxinlei1996 2 years ago
parent
commit
a6b26e74a6
2 changed files with 47 additions and 5 deletions
  1. 14 3
      rpc/internal/logic/delcheckredislogic.go
  2. 33 2
      service/power.go

+ 14 - 3
rpc/internal/logic/delcheckredislogic.go

@@ -3,8 +3,10 @@ package logic
 import (
 	"context"
 
+	"bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/entity"
 	"bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/rpc/internal/svc"
 	"bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/rpc/pb"
+	"bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/service"
 
 	"github.com/zeromicro/go-zero/core/logx"
 )
@@ -25,7 +27,16 @@ func NewDelCheckRedisLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Del
 
 // 清除权限缓存
 func (l *DelCheckRedisLogic) DelCheckRedis(in *pb.CheckReq) (*pb.Resp, error) {
-	// todo: add your logic here and delete this line
-
-	return &pb.Resp{}, nil
+	resp := &pb.Resp{}
+	power := service.NewPower(&entity.Conn{
+		Mysql:     entity.Mysql,
+		BaseMysql: entity.BaseMysql,
+		MgoJy:     entity.MgoJy,
+	})
+	if power.DelRedisPower(in.PositionId) {
+		resp.Status = 1
+	} else {
+		resp.Status = -1
+	}
+	return resp, nil
 }

+ 33 - 2
service/power.go

@@ -1,7 +1,9 @@
 package service
 
 import (
+	"encoding/json"
 	"fmt"
+	"math/rand"
 	"strings"
 	"time"
 
@@ -49,6 +51,12 @@ func (this *PowerService) Power(userid string, baseUserId, accountId, entId, pos
 	vip := &entity.Vip{}
 	member := &entity.Member{}
 	free := &entity.Free{}
+	userPower := entity.Power{}
+	if bytes, err := redis.GetBytes("newother", GetRedisName(positionId)); err == nil && bytes != nil {
+		if err := json.Unmarshal(*bytes, &userPower); err == nil {
+			return &userPower
+		}
+	}
 
 	//获取用户本身的注册时间和邮箱、这个与个人、企业无关
 	mgoUserFields := map[string]interface{}{
@@ -329,14 +337,21 @@ func (this *PowerService) Power(userid string, baseUserId, accountId, entId, pos
 			free.IsFree = false
 		}
 	}
-	return &entity.Power{
+	userPower = entity.Power{
 		Vip:      vip,
 		Member:   member,
 		Free:     free,
 		Ent:      ent,
 		Entniche: entniche,
 	}
-	return nil
+	//存储缓存
+	go func() {
+		if bytes, err := json.Marshal(userPower); err == nil && bytes != nil {
+			oneDayMore := 60*60*24 + rand.Intn(60*60)
+			_ = redis.PutBytes("newother", GetRedisName(positionId), &bytes, oneDayMore)
+		}
+	}()
+	return &userPower
 }
 
 //免费用户体验会员功能权限
@@ -374,3 +389,19 @@ func HasKey(a_items []map[string]interface{}) bool {
 	}
 	return false
 }
+
+//获取redis key
+func GetRedisName(positionId int64) string {
+	if positionId == 0 {
+		return ""
+	}
+	return fmt.Sprintf("user_power_info_%v", positionId)
+}
+
+//清除redis缓存
+func (this *PowerService) DelRedisPower(positionId int64) bool {
+	if positionId == 0 {
+		return false
+	}
+	return redis.Del("newother", GetRedisName(positionId))
+}