Przeglądaj źródła

缓存数据添加

maker 3 lat temu
rodzic
commit
b7bb8ebd23

+ 17 - 0
api/v1/typeManage/accountType.go

@@ -1,6 +1,8 @@
 package typeManage
 
 import (
+	"context"
+	"encoding/json"
 	"github.com/gin-gonic/gin"
 	"go.uber.org/zap"
 	"log-server/global"
@@ -9,6 +11,7 @@ import (
 	"log-server/model/typeManage"
 	typeManageRes "log-server/model/typeManage/response"
 	"log-server/utils"
+	"strconv"
 )
 
 type AccountTypeApi struct {
@@ -83,6 +86,20 @@ func (a *AccountTypeApi) GetAccountTypeById(c *gin.Context) {
 		response.FailWithMessage(err.Error(), c)
 		return
 	}
+	//查询redis
+	ctx := context.Background()
+	key := "typeManage:accountType"
+	//读取数据
+	bytes, _ := global.GVA_REDIS.HGet(ctx, key, strconv.Itoa(id.ID)).Bytes()
+	//存在记录,反序列化
+	if bytes != nil {
+		objects := &typeManage.AccountType{}
+		json.Unmarshal(bytes, objects)
+		//存在,返回
+		response.OkWithDetailed(typeManageRes.AccountTypeResponse{AccountType: *objects}, "获取成功", c)
+		return
+	}
+	//不存在,查询数据库
 	if accountType, err := accountTypeService.GetAccountTypeById(id.ID); err != nil{
 		global.GVA_LOG.Error("获取失败!", zap.Error(err))
 		response.FailWithMessage("获取失败", c)

+ 19 - 0
api/v1/typeManage/gameChannel.go

@@ -1,6 +1,8 @@
 package typeManage
 
 import (
+	"context"
+	"encoding/json"
 	"github.com/gin-gonic/gin"
 	"go.uber.org/zap"
 	"log-server/global"
@@ -9,6 +11,7 @@ import (
 	typeManageModel "log-server/model/typeManage"
 	typeManageRes "log-server/model/typeManage/response"
 	"log-server/utils"
+	"strconv"
 )
 
 type GameChannelApi struct {}
@@ -89,6 +92,21 @@ func (g *GameChannelApi) GetGameChannelById(c *gin.Context) {
 		response.FailWithMessage(err.Error(), c)
 		return
 	}
+	//查询redis
+	ctx := context.Background()
+	key := "typeManage:gameChannel"
+	//读取数据
+	redisChannel, _ := global.GVA_REDIS.HGet(ctx, key, strconv.Itoa(idInfo.ID)).Bytes()
+	if redisChannel != nil {
+		//反序列化
+		objects := &typeManageModel.GameChannel{}
+		json.Unmarshal(redisChannel, objects)
+		//存在, 返回
+		response.OkWithDetailed(typeManageRes.GameChannelResponse{GameChannel: *objects}, "获取成功", c)
+		return
+	}
+
+	//不存在,查询数据库
 	channel, err := gameService.GetGameChannelById(idInfo.ID);
 	if err != nil {
 		global.GVA_LOG.Error("获取失败!", zap.Error(err))
@@ -106,6 +124,7 @@ func (g *GameChannelApi) GameChannelList(c *gin.Context)  {
 		response.FailWithMessage(err.Error(), c)
 		return
 	}
+
 	if list, total, err := gameService.GetGameChannelInfoList(pageInfo.GameChannel, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc); err != nil {
 		global.GVA_LOG.Error("获取失败!", zap.Error(err))
 		response.FailWithMessage("获取失败", c)

+ 21 - 2
api/v1/typeManage/gameList.go

@@ -1,14 +1,17 @@
 package typeManage
 
 import (
+	"context"
+	"encoding/json"
+	"github.com/gin-gonic/gin"
+	"go.uber.org/zap"
 	"log-server/global"
 	"log-server/model/common/request"
 	"log-server/model/common/response"
 	"log-server/model/typeManage"
 	typeManageRes "log-server/model/typeManage/response"
 	"log-server/utils"
-	"github.com/gin-gonic/gin"
-	"go.uber.org/zap"
+	"strconv"
 )
 
 type GameListApi struct{}
@@ -86,6 +89,22 @@ func (g *GameListApi) GetGameListById(c *gin.Context) {
 		response.FailWithMessage(err.Error(), c)
 		return
 	}
+	//访问redis
+	ctx := context.Background()
+	key := "typeManage:gameList"
+	//读取数据
+	bytes, _ := global.GVA_REDIS.HGet(ctx, key, strconv.Itoa(idInfo.ID)).Bytes()
+	//有记录
+	if bytes != nil {
+		//反序列化
+		objects := &typeManage.GameList{}
+		json.Unmarshal(bytes, objects)
+		//返回信息
+		response.OkWithDetailed(typeManageRes.GameListResponse{GameList: *objects}, "获取成功", c)
+		return
+	}
+
+	//redis无记录,访问数据库
 	gameList, err := gameListService.GetGameListById(idInfo.ID)
 	if err != nil {
 		global.GVA_LOG.Error("获取失败!", zap.Error(err))

+ 19 - 2
api/v1/typeManage/loginType.go

@@ -1,14 +1,17 @@
 package typeManage
 
 import (
+	"context"
+	"encoding/json"
+	"github.com/gin-gonic/gin"
+	"go.uber.org/zap"
 	"log-server/global"
 	"log-server/model/common/request"
 	"log-server/model/common/response"
 	"log-server/model/typeManage"
 	typeManageRes "log-server/model/typeManage/response"
 	"log-server/utils"
-	"github.com/gin-gonic/gin"
-	"go.uber.org/zap"
+	"strconv"
 )
 
 type LoginTypeApi struct{}
@@ -82,6 +85,20 @@ func (l *LoginTypeApi) GetLoginTypeById(c *gin.Context) {
 		response.FailWithMessage(err.Error(), c)
 		return
 	}
+	//查询redis
+	ctx := context.Background()
+	key := "typeManage:loginType"
+	//读取缓存
+	bytes, _ := global.GVA_REDIS.HGet(ctx, key, strconv.Itoa(idInfo.ID)).Bytes()
+	//找到记录,反序列化
+	if bytes != nil {
+		objects := &typeManage.LoginType{}
+		json.Unmarshal(bytes, objects)
+		response.OkWithDetailed(&typeManageRes.LoginTypeResponse {LoginType:*objects}, "获取成功", c)
+		//返回
+		return
+	}
+	//没找到,查询数据库
 	loginType, err := loginTypeService.GetLoginTypeById(idInfo.ID)
 	if err != nil {
 		global.GVA_LOG.Error("获取失败!", zap.Error(err))

+ 16 - 0
api/v1/typeManage/responsiblePerson.go

@@ -1,6 +1,8 @@
 package typeManage
 
 import (
+	"context"
+	"encoding/json"
 	"github.com/gin-gonic/gin"
 	"go.uber.org/zap"
 	"log-server/global"
@@ -9,6 +11,7 @@ import (
 	"log-server/model/typeManage"
 	typeManageRes "log-server/model/typeManage/response"
 	"log-server/utils"
+	"strconv"
 )
 
 type ResponsiblePersonApi struct {}
@@ -85,6 +88,19 @@ func (r *ResponsiblePersonApi) GetResponsiblePersonById(c *gin.Context)  {
 		response.FailWithMessage(err.Error(), c)
 		return
 	}
+	//读取redis缓存
+	ctx := context.Background()
+	key := "typeManage:responsiblePerson"
+	bytes, _ := global.GVA_REDIS.HGet(ctx, key, strconv.Itoa(id.ID)).Bytes()
+	//存在记录,反序列化
+	if bytes != nil {
+		objects := &typeManage.ResponsiblePerson{}
+		json.Unmarshal(bytes, objects)
+		//返回
+		response.OkWithDetailed(typeManageRes.ResponsiblePersonResponse{ResponsiblePerson: *objects}, "获取成功", c)
+		return
+	}
+	//不存在,读取数据库
 	if person, err := responsiblePersonService.GetResponsiblePersonById(id.ID);err != nil{
 		global.GVA_LOG.Error("获取失败!", zap.Error(err))
 		response.FailWithMessage("获取失败", c)

+ 7 - 4
model/task/option.go

@@ -7,16 +7,19 @@ type GamePortOption struct {
 
 type LoginTypeOption struct {
 	Id int
-	LoginName string
+	LoginName string `json:"login_name"`
 }
 
 type AccountTypeOption struct {
 	Id int
-	TypeName string
+	TypeName string `json:"type_name"`
+}
+
+type ResponsiblePersonOption struct {
+	Name string `json:"name"`
 }
 
 type GameListOption struct {
-	Id int	`json:"game_id"`
 	GameName string	`json:"game_name"`
 }
 
@@ -26,5 +29,5 @@ type Option struct {
 	LoginTypeOptions []LoginTypeOption `json:"login_type"`
 	AccountTypeOptions []AccountTypeOption `json:"account_type"`
 	GameListOptions []GameListOption `json:"game_list"`
-	ResponsiblePersonOptions []string `json:"responsible_person_list"`
+	ResponsiblePersonOptions []ResponsiblePersonOption `json:"responsible_person_list"`
 }

+ 15 - 7
model/typeManage/gameChannel.go

@@ -1,11 +1,19 @@
 package typeManage
 
 type GameChannel struct {
-	Id              int64     `gorm:"column:id;type:bigint(20);primaryKey" json:"id"`
-	ChannelName     string    `gorm:"column:channel_name;type:varchar(255);not null" json:"channelName" binding:"required"`
-	ChannelDesc     string    `gorm:"column:channel_desc;type:varchar(255);not null" json:"channelDesc" binding:"required"`
-	DisplaySequence int64     `gorm:"column:display_sequence;type:smallint" json:"displaySequence" binding:"required"`
-	GameCounts      int64     `gorm:"column:game_counts;type:int" json:"gameCounts" binding:"required"`
-	CreateTime      LocalTime `gorm:"column:create_time;type:TIMESTAMP;" json:"createTime"`
-	UpdateTime      LocalTime `gorm:"column:update_time;type:TIMESTAMP;" json:"updateTime"`
+	Id              int64     `json:"id"`
+	ChannelName     string    `json:"channelName"`
+	ChannelDesc     string    `json:"channelDesc"`
+	DisplaySequence int64     `json:"displaySequence"`
+	GameCounts      int64     `json:"gameCounts"`
+	CreateTime      LocalTime `gorm:"column:create_time;"json:"createTime"`
+	UpdateTime      LocalTime `gorm:"column:update_time;"json:"updateTime"`
 }
+
+//type GameChannel1 struct {
+//	Id              int64     `json:"id"`
+//	ChannelName     string    `json:"channelName"`
+//	ChannelDesc     string    `json:"channelDesc"`
+//	DisplaySequence int64     `json:"displaySequence"`
+//	GameCounts      int64     `json:"gameCounts"`
+//}

+ 2 - 2
model/typeManage/gameList.go

@@ -6,6 +6,6 @@ type GameList struct {
 	GamePort  string    `gorm:"column:game_port;"json:"game_port"`
 	LoginType string    `gorm:"column:login_type;"json:"login_type"`
 	Remark    string    `gorm:"column:remark;"json:"remark"`
-	CreatedAt LocalTime `gorm:"column:create_time;type:TIMESTAMP;" json:"createdAt"`
-	UpdatedAt LocalTime `gorm:"column:update_time;type:TIMESTAMP;" json:"updatedAt"`
+	CreatedAt LocalTime `gorm:"column:create_time;type:TIMESTAMP;" json:"create_time"`
+	UpdatedAt LocalTime `gorm:"column:update_time;type:TIMESTAMP;" json:"update_time"`
 }

+ 2 - 2
model/typeManage/loginType.go

@@ -6,6 +6,6 @@ type LoginType struct {
 	LoginDesc       string    `gorm:"column:login_desc;" json:"login_desc"`
 	DisplaySequence int64     `gorm:"column:display_sequence;" json:"display_sequence"`
 	GameCounts      int64     `gorm:"column:game_counts;" json:"game_counts"`
-	CreatedAt       LocalTime `gorm:"column:create_time;" json:"createdAt"`
-	UpdatedAt       LocalTime `gorm:"column:update_time;" json:"updatedAt"`
+	CreatedAt       LocalTime `gorm:"column:create_time;" json:"create_time"`
+	UpdatedAt       LocalTime `gorm:"column:update_time;" json:"update_time"`
 }

+ 19 - 2
model/typeManage/option.go

@@ -1,6 +1,23 @@
 package typeManage
 
+type GamePortOption struct {
+	Id              int64     `json:"id"`
+	ChannelName     string    `json:"channelName"`
+}
+
+
+//type TestStruct struct {
+//	ID string `json:"id"`
+//	Info string `json:"info"`
+//}
+
+type LoginTypeOption struct {
+	Id              int64     `json:"id"`
+	LoginName       string    `json:"login_name"`
+	//LoginType []LoginType
+}
+
 type Option struct {
-	GamePortOptions []string `column:"channel_name;"json:"channel_name"`
-	LoginTypeOptions []string `column:"login_name;"json:"login_name"`
+	GamePortOptions []GamePortOption `column:"channel_name;"json:"channel"`
+	LoginTypeOptions []LoginTypeOption `column:"login_name;"json:"login"`
 }

+ 21 - 9
service/task/game_task.go

@@ -232,16 +232,28 @@ func (apiService *GameTask) GetGameTaskInfoList(api task.GameTask, info request.
 
 
 
-	//查找厂商名称,登录类型名称、账号类型名称
-	option := task.Option{}
-	global.GVA_DB.Table("game_channel").Select("id","channel_name").Find(&option.GamePortOptions)
-	global.GVA_DB.Table("login_type").Select("id", "login_name").Find(&option.LoginTypeOptions)
-	global.GVA_DB.Table("account_type").Select("id", "type_name").Find(&option.AccountTypeOptions)
-	global.GVA_DB.Table("game_list").Select("id", "game_name").Find(&option.GameListOptions)
-	global.GVA_DB.Table("responsible_person").Select("name").Where("state = 1").Find(&option.ResponsiblePersonOptions)
-
-	//fmt.Println(option)
 
+	option := task.Option{}
+	gamePortOption := GamePortOptionQuery()
+	loginTypeOption := LoginTypeOptionQuery()
+	accountTypeOption := AccountTypeOptionQuery()
+	responsiblePersonOption := ResponsiblePersonOptionQuery()
+	gameListOption := GameListOptionQuery()
+	if gamePortOption == nil || loginTypeOption == nil || accountTypeOption == nil || responsiblePersonOption == nil || gameListOption == nil{
+		//redis未找到
+		//通过数据库查找厂商名称,登录类型名称、账号类型名称
+		global.GVA_DB.Table("game_channel").Select("id","channel_name").Find(&option.GamePortOptions)
+		global.GVA_DB.Table("login_type").Select("id", "login_name").Find(&option.LoginTypeOptions)
+		global.GVA_DB.Table("account_type").Select("id", "type_name").Find(&option.AccountTypeOptions)
+		global.GVA_DB.Table("game_list").Select("game_name").Find(&option.GameListOptions)
+		global.GVA_DB.Table("responsible_person").Select("name").Where("state = 1").Find(&option.ResponsiblePersonOptions)
+	} else {
+		option.GamePortOptions = gamePortOption
+		option.LoginTypeOptions = loginTypeOption
+		option.AccountTypeOptions = accountTypeOption
+		option.ResponsiblePersonOptions = responsiblePersonOption
+		option.GameListOptions = gameListOption
+	}
 
 
 	if err != nil {

+ 129 - 0
service/task/redis_query_option.go

@@ -0,0 +1,129 @@
+package task
+
+import (
+	"context"
+	"encoding/json"
+	"fmt"
+	"log-server/global"
+	"log-server/model/task"
+)
+
+//定义游戏厂商结构体,以及返回游戏厂商结构体
+func GamePortOptionQuery() (channelOptionReturn []task.GamePortOption){
+	var channelOption []task.GamePortOption	//回传前端的信息结构体
+	var objectChannel task.GamePortOption	//用于反序列化的结构体
+	ctx := context.Background()	//redis上下文环境
+	key := "typeManage:gameChannel"	//查询的redis表
+	channelKeys, _ := global.GVA_REDIS.HKeys(ctx, key).Result()	//hash表中的所有key
+	return RedisQueryChannel(ctx, channelOption, objectChannel, key, channelKeys)
+}
+
+//定义登录方式结构体,以及返回登录方式结构体
+func LoginTypeOptionQuery() (loginOptionReturn []task.LoginTypeOption){
+	var loginOption []task.LoginTypeOption	//回传前端的信息结构体
+	var objectLogin task.LoginTypeOption	//用于反序列化的结构体
+	ctx := context.Background()	//redis上下文环境
+	key := "typeManage:loginType"	//查询的redis表
+	loginKeys, _ := global.GVA_REDIS.HKeys(ctx, key).Result()	//hash表中的所有key
+	return RedisQueryLogin(ctx, loginOption, objectLogin, key, loginKeys)
+}
+
+//定义账号类型结构体,以及返回账号类型结构体
+func AccountTypeOptionQuery() (accountOptionReturn []task.AccountTypeOption){
+	var accountOption []task.AccountTypeOption	//回传前端的信息结构体
+	var objectAccount task.AccountTypeOption	//用于反序列化的结构体
+	ctx := context.Background()	//redis上下文环境
+	key := "typeManage:accountType"	//查询的redis表
+	accountKeys, _ := global.GVA_REDIS.HKeys(ctx, key).Result()	//hash表中的所有key
+	return RedisQueryAccount(ctx, accountOption, objectAccount, key, accountKeys)
+}
+
+//定义负责人结构体,以及返回负责人结构体
+func ResponsiblePersonOptionQuery() (responsiblePersonOptionReturn []task.ResponsiblePersonOption){
+	var responsiblePersonOption []task.ResponsiblePersonOption	//回传前端的信息结构体
+	var objectResponsiblePerson task.ResponsiblePersonOption	//用于反序列化的结构体
+	ctx := context.Background()	//redis上下文环境
+	key := "typeManage:responsiblePerson"	//查询的redis表
+	responsiblePersonKeys, _ := global.GVA_REDIS.HKeys(ctx, key).Result()	//hash表中的所有key
+	return RedisQueryResponsiblePerson(ctx, responsiblePersonOption, objectResponsiblePerson, key, responsiblePersonKeys)
+}
+
+//定义游戏列表结构体,以及返回游戏列表结构体
+func GameListOptionQuery() (gameListOptionReturn []task.GameListOption){
+	var gameListOption []task.GameListOption	//回传前端的信息结构体
+	var objectGameList task.GameListOption	//用于反序列化的结构体
+	ctx := context.Background()	//redis上下文环境
+	key := "typeManage:gameList"	//查询redis表
+	gameListKeys, _ := global.GVA_REDIS.HKeys(ctx, key).Result()	//hash表中的所有key
+	return RedisQueryGameList(ctx, gameListOption, objectGameList, key, gameListKeys)
+}
+
+
+
+
+
+
+//查询游戏厂商具体数据
+func RedisQueryChannel(ctx context.Context, channelOption []task.GamePortOption, objectChannel task.GamePortOption, keyName string, keys []string) (channelOptionReturn []task.GamePortOption){
+	for k := range keys{
+		//遍历查找redis记录
+		bytes, _ := global.GVA_REDIS.HGet(ctx, keyName, keys[k]).Bytes()
+		json.Unmarshal(bytes, &objectChannel)
+		fmt.Println(objectChannel)
+		//将记录添加到数组
+		channelOption =  append(channelOption, objectChannel)
+	}
+	return channelOption
+}
+
+//查询登录方式具体数据
+func RedisQueryLogin(ctx context.Context, loginOption []task.LoginTypeOption, objectLogin task.LoginTypeOption, keyName string, keys []string) (loginOptionReturn []task.LoginTypeOption){
+	for k := range keys{
+		//遍历查找redis记录
+		bytes, _ := global.GVA_REDIS.HGet(ctx, keyName, keys[k]).Bytes()
+		json.Unmarshal(bytes, &objectLogin)
+		fmt.Println(objectLogin)
+		//将记录添加到数组
+		loginOption =  append(loginOption, objectLogin)
+	}
+	return loginOption
+}
+
+//查询账号类型具体数据
+func RedisQueryAccount(ctx context.Context, accountOption []task.AccountTypeOption, objectAccount task.AccountTypeOption, keyName string, keys []string) (accountOptionReturn []task.AccountTypeOption){
+	for k := range keys{
+		//遍历查找redis记录
+		bytes, _ := global.GVA_REDIS.HGet(ctx, keyName, keys[k]).Bytes()
+		json.Unmarshal(bytes, &objectAccount)
+		fmt.Println(objectAccount)
+		//将记录添加到数组
+		accountOption =  append(accountOption, objectAccount)
+	}
+	return accountOption
+}
+
+//查询负责人具体数据
+func RedisQueryResponsiblePerson(ctx context.Context, responsiblePersonOption []task.ResponsiblePersonOption, objectResponsiblePerson task.ResponsiblePersonOption, keyName string, keys []string) (responsiblePersonOptionReturn []task.ResponsiblePersonOption){
+	for k := range keys{
+		//遍历查找redis记录
+		bytes, _ := global.GVA_REDIS.HGet(ctx, keyName, keys[k]).Bytes()
+		json.Unmarshal(bytes, &objectResponsiblePerson)
+		fmt.Println(objectResponsiblePerson)
+		//将记录添加到数组
+		responsiblePersonOption =  append(responsiblePersonOption, objectResponsiblePerson)
+	}
+	return responsiblePersonOption
+}
+
+//查询游戏列表具体数据
+func RedisQueryGameList(ctx context.Context, gameListOption []task.GameListOption, objectGameList task.GameListOption, keyName string, keys []string) (gameListOptionReturn []task.GameListOption){
+	for k := range keys{
+		//遍历查找redis记录
+		bytes, _ := global.GVA_REDIS.HGet(ctx, keyName, keys[k]).Bytes()
+		json.Unmarshal(bytes, &objectGameList)
+		fmt.Println(objectGameList)
+		//将记录添加到数组
+		gameListOption =  append(gameListOption, objectGameList)
+	}
+	return gameListOption
+}

+ 52 - 3
service/typeManage/tm_accountType.go

@@ -1,12 +1,15 @@
 package typeManage
 
 import (
+	"context"
+	"encoding/json"
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
 	"log-server/global"
 	"log-server/model/common/request"
 	"log-server/model/typeManage"
+	"strconv"
 )
 
 type AccountTypeService struct {
@@ -20,7 +23,10 @@ func (a *AccountTypeService) CreateAccountType(accountType typeManage.AccountTyp
 	if !errors.Is(err, gorm.ErrRecordNotFound) {
 		return errors.New("已存在相同账号类型,请勿重复添加")
 	}
-	return global.GVA_DB.Table("account_type").Omit("create_time", "update_time").Create(&accountType).Error
+	//添加数据库记录
+	err = global.GVA_DB.Table("account_type").Omit("create_time", "update_time").Create(&accountType).Error
+	UpdateAccountRedis(accountType)
+	return
 }
 //单个删除账号类型
 func (a *AccountTypeService) DeleteAccountType(accountType typeManage.AccountType) (err error) {
@@ -32,11 +38,17 @@ func (a *AccountTypeService) DeleteAccountType(accountType typeManage.AccountTyp
 	if errors.Is(err, gorm.ErrRecordNotFound) {
 		return err
 	}
-	return global.GVA_DB.Table("account_type").Delete(&entity).Error
+	//删除记录
+	err = global.GVA_DB.Table("account_type").Delete(&entity).Error
+	//删除redis记录
+	DeleteAccountRedis(entity)
+	return
 }
 //批量删除账号类型
 func (a *AccountTypeService) DeleteAccountTypesByIds(ids request.IdsReq) (err error) {
 	err = global.GVA_DB.Table("account_type").Delete(&[]typeManage.AccountType{}, "id in ?", ids.Ids).Error
+	//删除redis记录
+	BulkDeletionAccount(ids.Ids)
 	return err
 }
 //更改账号类型
@@ -48,15 +60,22 @@ func (a *AccountTypeService) UpdateAccountType(accountType typeManage.AccountTyp
 	if !errors.Is(err, gorm.ErrRecordNotFound) {
 		return errors.New("已存在同名账号类型,无法更新")
 	}
-	return global.GVA_DB.Table("account_type").Updates(&accountType).Error
+	//进行数据更新
+	err = global.GVA_DB.Table("account_type").Updates(&accountType).Error
+	//更新redis记录
+	UpdateAccountRedis(accountType)
+	return err
 }
 
 
 //通过id获取单条数据
 func (a *AccountTypeService) GetAccountTypeById(id int) (accountType typeManage.AccountType, err error){
 	err = global.GVA_DB.Table("account_type").Where("id = ?", id).First(&accountType).Error
+	//更新redis
+	UpdateAccountRedis(accountType)
 	return
 }
+
 //获取所有账号类型列表
 func (a *AccountTypeService) AccountTypeList(accountType typeManage.AccountType, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error){
 	//accountType是传输过来的查询条件
@@ -121,4 +140,34 @@ func (a *AccountTypeService) AccountTypeList(accountType typeManage.AccountType,
 	}
 	//是因为只有切片能排序,model没办法排序;还是说为了将数组返回至前端才find扫描至切片呢?
 	return accountTypeList, total, err
+}
+
+//更改redis记录
+func UpdateAccountRedis(account typeManage.AccountType)  {
+	ctx := context.Background()
+	key := "typeManage:accountType"
+	field := strconv.FormatInt(account.Id, 10)
+	//序列化
+	datas, _ := json.Marshal(account)
+	global.GVA_REDIS.HSet(ctx, key, field, datas)
+}
+
+//删除redis记录
+func DeleteAccountRedis(account typeManage.AccountType)  {
+	//删除redis记录
+	ctx := context.Background()
+	key := "typeManage:accountType"
+	field := strconv.FormatInt(account.Id, 10)
+	global.GVA_REDIS.HDel(ctx, key, field)
+}
+
+//批量删除命令
+func BulkDeletionAccount(ids []int)  {
+	//批量删除redis
+	ctx := context.Background()
+	key := "typeManage:accountType"
+	for i := range ids{
+		delKey := strconv.Itoa(ids[i])
+		global.GVA_REDIS.HDel(ctx, key, delKey)
+	}
 }

+ 51 - 3
service/typeManage/tm_gameChannel.go

@@ -1,12 +1,15 @@
 package typeManage
 
 import (
+	"context"
+	"encoding/json"
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
 	"log-server/global"
 	"log-server/model/common/request"
 	"log-server/model/typeManage"
+	"strconv"
 )
 
 type GameChannelService struct {
@@ -19,7 +22,11 @@ func (g *GameChannelService) CreateGameChannel(channel typeManage.GameChannel) (
 	if !errors.Is(global.GVA_DB.Table("game_channel").Where("channel_name = ?",channel.ChannelName).First(&typeManage.GameChannel{}).Error, gorm.ErrRecordNotFound) {
 		return errors.New("已存在相同渠道,请勿重复添加")
 	}
-	return global.GVA_DB.Table("game_channel").Omit("create_time", "update_time").Create(&channel).Error
+	//添加数据库记录
+	err = global.GVA_DB.Table("game_channel").Omit("create_time", "update_time").Create(&channel).Error
+	//更新redis记录
+	UpdateChannelRedis(channel)
+	return
 }
 
 
@@ -30,7 +37,11 @@ func (g *GameChannelService) DeleteGameChannel(channel typeManage.GameChannel) (
 	if errors.Is(err, gorm.ErrRecordNotFound) {
 		return err
 	}
-	return global.GVA_DB.Table("game_channel").Delete(&entity).Error
+	//先删除数据库记录
+	err = global.GVA_DB.Table("game_channel").Delete(&entity).Error
+	//删除redis
+	DeleteChannelRedis(entity)
+	return
 }
 
 
@@ -38,6 +49,7 @@ func (g *GameChannelService) DeleteGameChannel(channel typeManage.GameChannel) (
 func (g *GameChannelService) DeleteGameChannelsByIds(ids request.IdsReq) (err error)  {
 
 	err = global.GVA_DB.Table("game_channel").Delete(&[]typeManage.GameChannel{}, "id in ?", ids.Ids).Error
+	BulkDeletionChannel(ids.Ids)
 	return err
 }
 
@@ -48,12 +60,18 @@ func (g *GameChannelService) UpdateGameChannel(channel typeManage.GameChannel) (
 	if !errors.Is(global.GVA_DB.Table("game_channel").Where("channel_name = ? and id != ?", channel.ChannelName, channel.Id).First(&typeManage.GameChannel{}).Error,gorm.ErrRecordNotFound) {
 		return errors.New("已存在相同渠道,无法更新")
 	}
-	return global.GVA_DB.Table("game_channel").Updates(&channel).Error
+	//进行数据更新
+	err = global.GVA_DB.Table("game_channel").Updates(&channel).Error
+	//更新redis记录
+	UpdateChannelRedis(channel)
+	return
 }
 
 //通过id查询
 func (g *GameChannelService) GetGameChannelById(id int) (channel typeManage.GameChannel, err error)  {
 	err = global.GVA_DB.Table("game_channel").Where("id = ?", id).First(&channel).Error
+	//更新redis
+	UpdateChannelRedis(channel)
 	return
 }
 
@@ -111,4 +129,34 @@ func (g *GameChannelService) GetGameChannelInfoList(channel typeManage.GameChann
 		}
 	}
 	return channelList, total, err
+}
+
+//更改redis记录
+func UpdateChannelRedis(channel typeManage.GameChannel)  {
+	ctx := context.Background()
+	key := "typeManage:gameChannel"
+	field := strconv.FormatInt(channel.Id, 10)
+	//序列化
+	datas, _ := json.Marshal(channel)
+	global.GVA_REDIS.HSet(ctx, key, field, datas)
+}
+
+//删除redis记录
+func DeleteChannelRedis(channel typeManage.GameChannel)  {
+	//删除redis记录
+	ctx := context.Background()
+	key := "typeManage:gameChannel"
+	field := strconv.FormatInt(channel.Id, 10)
+	global.GVA_REDIS.HDel(ctx, key, field)
+}
+
+//批量删除命令
+func BulkDeletionChannel(ids []int)  {
+	//批量删除redis
+	ctx := context.Background()
+	key := "typeManage:gameChannel"
+	for i := range ids{
+		delKey := strconv.Itoa(ids[i])
+		global.GVA_REDIS.HDel(ctx, key, delKey)
+	}
 }

+ 133 - 25
service/typeManage/tm_gameList.go

@@ -1,12 +1,15 @@
 package typeManage
 
 import (
+	"context"
+	"encoding/json"
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
 	"log-server/global"
 	"log-server/model/common/request"
 	"log-server/model/typeManage"
+	"strconv"
 )
 
 type GameListService struct{}
@@ -17,7 +20,11 @@ func (g *GameListService) CreateGameList(game typeManage.GameList) (err error)
 	if !errors.Is(global.GVA_DB.Table("game_list").Where("game_name = ? and game_port = ? and login_type = ?",game.GameName, game.GamePort, game.LoginType).First(&typeManage.GameChannel{}).Error, gorm.ErrRecordNotFound) {
 		return errors.New("已存在相同游戏,请勿重复添加")
 	}
-	return global.GVA_DB.Table("game_list").Omit("create_time", "update_time").Create(&game).Error
+	//添加数据库记录
+	err = global.GVA_DB.Table("game_list").Omit("create_time", "update_time").Create(&game).Error
+	//更新redis
+	UpdateGameRedis(game)
+	return err
 }
 
 //删除游戏列表
@@ -27,12 +34,18 @@ func (g *GameListService) DeleteGameList(game typeManage.GameList) (err error)
 	if errors.Is(err, gorm.ErrRecordNotFound) {
 		return err
 	}
-	return global.GVA_DB.Table("game_list").Delete(&entity).Error
+	//先执行删除操作,再删除redis缓存
+	err = global.GVA_DB.Table("game_list").Delete(&entity).Error
+	//删除redis缓存
+	DeleteGameRedis(entity)
+	return
 }
 
 //批量删除
 func (g *GameListService) DeleteGameListsByIds(ids request.IdsReq) (err error) {
 	err = global.GVA_DB.Table("game_list").Delete(&[]typeManage.GameList{}, "id in ?", ids.Ids).Error
+	//批量删除redis
+	BulkDeletionGame(ids.Ids)
 	return err
 }
 
@@ -45,7 +58,11 @@ func (g *GameListService) UpdateGameList(game typeManage.GameList) (err error) {
 	if !errors.Is(err, gorm.ErrRecordNotFound) {
 		return errors.New("已存在相同游戏,无法更新")
 	}
-	return global.GVA_DB.Table("game_list").Updates(&game).Error
+	//先执行更新操作,再删除redis缓存
+	err = global.GVA_DB.Table("game_list").Updates(&game).Error
+	//更新redis缓存
+	UpdateGameRedis(game)
+	return
 }
 
 
@@ -56,6 +73,8 @@ func (g *GameListService) UpdateGameList(game typeManage.GameList) (err error) {
 //单个查询
 func (g *GameListService) GetGameListById(id int) (gameList typeManage.GameList, err error)  {
 	err = global.GVA_DB.Table("game_list").Where("id = ?", id).First(&gameList).Error
+	//更改redis缓存
+	UpdateGameRedis(gameList)
 	return
 }
 
@@ -65,29 +84,44 @@ func (g *GameListService) GetGameList(game typeManage.SearchGameListParams, info
 	//计算limit和offset
 	limit := info.PageSize
 	offset := (info.Page - 1) * limit
-
-	//登录方式和游戏厂商获取
+	//通过redis获取登录方式和游戏厂商
 	option := typeManage.Option{}
-
-	//首先判断登录方式和游戏厂商存不存在,如果不存在,就不必find了
-	//判断是否有记录,如果有就查询,如果没有,报错
-	//有记录
-	//err = global.GVA_DB.Table("game_channel").Model(&typeManage.GameChannel{}).Count(&total).Error
-	//if err != nil {
-	//
-	//}
-	global.GVA_DB.Table("game_channel").Select("channel_name").Find(&option.GamePortOptions)
-
-	//判断是否有记录,如果有就查询,如果没有,报错
-	//有记录
-	global.GVA_DB.Table("login_type").Select("login_name").Find(&option.LoginTypeOptions)
-
-
-	//fmt.Println(option)
-
-
-
-
+	gamePortOption := GamePortOptionQuery()
+	loginTypeOption := LoginTypeOptionQuery()
+	if gamePortOption == nil || loginTypeOption == nil {
+		//首先判断登录方式和游戏厂商存不存在,如果不存在,就不必find了
+		//判断是否有记录,如果有就查询,如果没有,报错
+		//有记录
+		//err = global.GVA_DB.Table("game_channel").Model(&typeManage.GameChannel{}).Count(&total).Error
+		//if err != nil {
+		//
+		//}
+		global.GVA_DB.Table("game_channel").Select("channel_name").Find(&option.GamePortOptions)
+		//判断是否有记录,如果有就查询,如果没有,报错
+		//有记录
+		global.GVA_DB.Table("login_type").Select("login_name").Find(&option.LoginTypeOptions)
+		//fmt.Println(option)
+		//将option写入redis
+		//序列化option对象
+		//datas, _ := json.Marshal(option)
+		//global.GVA_REDIS.Set(ctx, key, datas, 30* time.Minute)
+	} else {
+		//找到redis记录
+		//先将map序列化,然后反序列化成结构体,最后传输
+		//var objectChannel typeManage.GamePortOption
+		//m := map[string]interface{}{
+		//	"id":"hahaha",
+		//	"info":"heheeh",
+		//}
+		//var object typeManage.TestStruct
+		//fmt.Println(m)
+		//bytes, _ := json.Marshal(m)
+		//err = json.Unmarshal(bytes, &object)
+		//fmt.Println(err)
+		//fmt.Println(object)
+		option.GamePortOptions = gamePortOption
+		option.LoginTypeOptions = loginTypeOption
+	}
 
 	//绑定操作model
 	db := global.GVA_DB.Table("game_list").Model(&typeManage.GameList{})
@@ -141,3 +175,77 @@ func (g *GameListService) GetGameList(game typeManage.SearchGameListParams, info
 		return gameList,option, total, err
 	}
 }
+
+//更改redis记录
+func UpdateGameRedis(game typeManage.GameList)  {
+	ctx := context.Background()
+	key := "typeManage:gameList"
+	field := strconv.FormatInt(game.Id, 10)
+	//序列化
+	datas, _ := json.Marshal(game)
+	global.GVA_REDIS.HSet(ctx, key, field, datas)
+}
+
+//删除redis记录
+func DeleteGameRedis(game typeManage.GameList)  {
+	//删除redis记录
+	ctx := context.Background()
+	key := "typeManage:gameList"
+	field := strconv.FormatInt(game.Id, 10)
+	global.GVA_REDIS.HDel(ctx, key, field)
+}
+
+//批量删除命令
+func BulkDeletionGame(ids []int)  {
+	//批量删除redis
+	ctx := context.Background()
+	key := "typeManage:gameList"
+	for i := range ids{
+		delKey := strconv.Itoa(ids[i])
+		global.GVA_REDIS.HDel(ctx, key, delKey)
+	}
+}
+
+//定义游戏厂商结构体,以及返回游戏厂商结构体
+func GamePortOptionQuery() (channelOptionReturn []typeManage.GamePortOption){
+	var channelOption []typeManage.GamePortOption	//回传前端的信息结构体
+	var objectChannel typeManage.GamePortOption	//用于反序列化的结构体
+	ctx := context.Background()	//redis上下文环境
+	key := "typeManage:gameChannel"	//查询的redis表
+	channelKeys, _ := global.GVA_REDIS.HKeys(ctx, key).Result()	//hash表中的所有key
+	return RedisQueryChannel(ctx, channelOption, objectChannel, key, channelKeys)
+}
+
+//定义登录方式结构体,以及返回登录方式结构体
+func LoginTypeOptionQuery() (loginOptionReturn []typeManage.LoginTypeOption){
+	var loginOption []typeManage.LoginTypeOption	//回传前端的信息结构体
+	var objectLogin typeManage.LoginTypeOption	//用于反序列化的结构体
+	ctx := context.Background()	//redis上下文环境
+	key := "typeManage:loginType"	//查询的redis表
+	loginKeys, _ := global.GVA_REDIS.HKeys(ctx, key).Result()	//hash表中的所有key
+	return RedisQueryLogin(ctx, loginOption, objectLogin, key, loginKeys)
+}
+
+//查询游戏厂商具体数据
+func RedisQueryChannel(ctx context.Context, channelOption []typeManage.GamePortOption, objectChannel typeManage.GamePortOption, keyName string, keys []string) (channelOptionReturn []typeManage.GamePortOption){
+	for k := range keys{
+		//fmt.Println(keys[k])
+		bytes, _ := global.GVA_REDIS.HGet(ctx, keyName, keys[k]).Bytes()
+		json.Unmarshal(bytes, &objectChannel)
+		fmt.Println(objectChannel)
+		channelOption =  append(channelOption, objectChannel)
+	}
+	return channelOption
+}
+
+//查询登录方式具体数据
+func RedisQueryLogin(ctx context.Context, loginOption []typeManage.LoginTypeOption, objectLogin typeManage.LoginTypeOption, keyName string, keys []string) (loginOptionReturn []typeManage.LoginTypeOption){
+	for k := range keys{
+		//fmt.Println(keys[k])
+		bytes, _ := global.GVA_REDIS.HGet(ctx, keyName, keys[k]).Bytes()
+		json.Unmarshal(bytes, &objectLogin)
+		fmt.Println(objectLogin)
+		loginOption =  append(loginOption, objectLogin)
+	}
+	return loginOption
+}

+ 52 - 3
service/typeManage/tm_loginType.go

@@ -1,12 +1,15 @@
 package typeManage
 
 import (
+	"context"
+	"encoding/json"
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
 	"log-server/global"
 	"log-server/model/common/request"
 	"log-server/model/typeManage"
+	"strconv"
 )
 
 type LoginTypeService struct {}
@@ -20,7 +23,11 @@ func (l *LoginTypeService) CreateLoginType(login typeManage.LoginType) (err erro
 		//找到同名登录名称
 		return errors.New("已存在相同登录方式,请勿重复添加")
 	}
-	return global.GVA_DB.Table("login_type").Omit("create_time", "update_time").Create(&login).Error
+	//添加数据库记录
+	err = global.GVA_DB.Table("login_type").Omit("create_time", "update_time").Create(&login).Error
+	//更新redis
+	UpdateLoginRedis(login)
+	return err
 }
 
 //单个删除
@@ -34,7 +41,11 @@ func (l *LoginTypeService) DeleteLoginType(login typeManage.LoginType) (err erro
 	if errors.Is(err, gorm.ErrRecordNotFound) {
 		return err
 	}
-	return global.GVA_DB.Table("login_type").Delete(&entity).Error
+	//先进行数据删除
+	err = global.GVA_DB.Table("login_type").Delete(&entity).Error
+	//删除redis
+	DeleteLoginRedis(entity)
+	return
 }
 
 
@@ -43,6 +54,8 @@ func (l *LoginTypeService) DeleteLoginType(login typeManage.LoginType) (err erro
 //批量删除
 func (l *LoginTypeService) DeleteLoginTypesByIds(ids request.IdsReq) (err error)  {
 	err = global.GVA_DB.Table("login_type").Delete(&[]typeManage.LoginType{}, "id in ?", ids.Ids).Error
+	//批量删除redis
+	BulkDeletionLogin(ids.Ids)
 	return err
 }
 
@@ -54,7 +67,11 @@ func (l *LoginTypeService) UpdateLoginType(login typeManage.LoginType) (err erro
 	if !errors.Is(err, gorm.ErrRecordNotFound) {
 		return errors.New("已存在同名登录方式,修改失败")
 	}
-	return global.GVA_DB.Table("login_type").Updates(&login).Error
+	//先进行数据更新
+	err = global.GVA_DB.Table("login_type").Updates(&login).Error
+	//更新redis
+	UpdateLoginRedis(login)
+	return
 }
 
 
@@ -63,6 +80,8 @@ func (l *LoginTypeService) UpdateLoginType(login typeManage.LoginType) (err erro
 //通过id查询单个登录类型
 func (l *LoginTypeService) GetLoginTypeById(id int) (loginType typeManage.LoginType, err error) {
 	err = global.GVA_DB.Table("login_type").Where("id = ?", id).First(&loginType).Error
+	//更新redis
+	UpdateLoginRedis(loginType)
 	return
 }
 
@@ -119,4 +138,34 @@ func (l *LoginTypeService) GetLoginType(loginType typeManage.LoginType, info req
 		}
 	}
 	return loginList, total, err
+}
+
+//更改redis记录
+func UpdateLoginRedis(login typeManage.LoginType)  {
+	ctx := context.Background()
+	key := "typeManage:loginType"
+	field := strconv.FormatInt(login.Id, 10)
+	//序列化
+	datas, _ := json.Marshal(login)
+	global.GVA_REDIS.HSet(ctx, key, field, datas)
+}
+
+//删除redis记录
+func DeleteLoginRedis(login typeManage.LoginType)  {
+	//删除redis记录
+	ctx := context.Background()
+	key := "typeManage:loginType"
+	field := strconv.FormatInt(login.Id, 10)
+	global.GVA_REDIS.HDel(ctx, key, field)
+}
+
+//批量删除命令
+func BulkDeletionLogin(ids []int)  {
+	//批量删除redis
+	ctx := context.Background()
+	key := "typeManage:loginType"
+	for i := range ids{
+		delKey := strconv.Itoa(ids[i])
+		global.GVA_REDIS.HDel(ctx, key, delKey)
+	}
 }

+ 67 - 4
service/typeManage/tm_responsiblePerson.go

@@ -1,6 +1,8 @@
 package typeManage
 
 import (
+	"context"
+	"encoding/json"
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
@@ -8,6 +10,7 @@ import (
 	"log-server/model/common/request"
 	"log-server/model/typeManage"
 	"math/rand"
+	"strconv"
 )
 
 func RandomString(n int) string {
@@ -72,8 +75,16 @@ func (r *ResponsiblePersonService) CreateResponsiblePerson(person typeManage.Res
 	//	person.Img = path
 	//
 	//}
-	//数据库插入
-	return global.GVA_DB.Table("responsible_person").Omit("create_time", "update_time").Create(&person).Error
+
+
+	//数据库插入记录
+	err = global.GVA_DB.Table("responsible_person").Omit("create_time", "update_time").Create(&person).Error
+	//更新redis信息
+	//state == 1,这条记录是启用状态,添加redis记录
+	if person.State == 1 {
+		UpdatePersonRedis(person)
+	}
+	return err
 }
 
 
@@ -84,12 +95,18 @@ func (r *ResponsiblePersonService) DeleteResponsiblePerson(person typeManage.Res
 	if errors.Is(err, gorm.ErrRecordNotFound) {
 		return errors.New("未找到记录,删除失败")
 	}
-	return global.GVA_DB.Table("responsible_person").Delete(&entity).Error
+	//先删除数据库记录
+	err = global.GVA_DB.Table("responsible_person").Delete(&entity).Error
+	//再删除redis缓存
+	DeletePersonRedis(entity)
+	return
 }
 
 //批量删除
 func (r *ResponsiblePersonService) DeleteResponsiblePersonsByIds(ids request.IdsReq) (err error) {
 	err = global.GVA_DB.Table("responsible_person").Delete(&[]typeManage.ResponsiblePerson{}, "id in ?", ids.Ids).Error
+	//批量删除redis记录
+	BulkDeletionPerson(ids.Ids)
 	return err
 }
 
@@ -100,13 +117,29 @@ func (r *ResponsiblePersonService) UpdateResponsiblePerson(person typeManage.Res
 	if !errors.Is(err, gorm.ErrRecordNotFound) {
 		return errors.New("已存在相同名字,无法更改")
 	}
-	return global.GVA_DB.Table("responsible_person").Updates(&person).Error
+	//更改数据库记录
+	err = global.GVA_DB.Table("responsible_person").Updates(&person).Error
+	//更新redis缓存
+	//state == 1,这条记录是启用状态,更新redis记录
+	if person.State == 1 {
+		UpdatePersonRedis(person)
+	}
+	//state == 2,这条记录是禁用状态,删除redis记录
+	if person.State == 2 {
+		DeletePersonRedis(person)
+	}
+	return
 }
 
 
 //通过id查询负责人
 func (r *ResponsiblePersonService) GetResponsiblePersonById(id int) (person typeManage.ResponsiblePerson, err error)  {
 	err = global.GVA_DB.Table("responsible_person").Where("id = ?", id).First(&person).Error
+	//更新redis缓存
+	//state == 1,这条记录是启用状态,更新redis记录
+	if person.State == 1 {
+		UpdatePersonRedis(person)
+	}
 	return
 }
 
@@ -155,3 +188,33 @@ func(r *ResponsiblePersonService) GetResponsiblePerson(person typeManage.Respons
 	}
 	return personList, total, err
 }
+
+//更改redis记录
+func UpdatePersonRedis(person typeManage.ResponsiblePerson)  {
+	ctx := context.Background()
+	key := "typeManage:responsiblePerson"
+	field := strconv.FormatInt(person.Id, 10)
+	//序列化
+	datas, _ := json.Marshal(person)
+	global.GVA_REDIS.HSet(ctx, key, field, datas)
+}
+
+//删除redis记录
+func DeletePersonRedis(person typeManage.ResponsiblePerson)  {
+	//删除redis记录
+	ctx := context.Background()
+	key := "typeManage:responsiblePerson"
+	field := strconv.FormatInt(person.Id, 10)
+	global.GVA_REDIS.HDel(ctx, key, field)
+}
+
+//批量删除命令
+func BulkDeletionPerson(ids []int)  {
+	//批量删除redis
+	ctx := context.Background()
+	key := "typeManage:responsiblePerson"
+	for i := range ids{
+		delKey := strconv.Itoa(ids[i])
+		global.GVA_REDIS.HDel(ctx, key, delKey)
+	}
+}