使用Redis中的Hash数据结构存储结构体实例_global

需要的结构体

例子(gin-vue-admin)(简化了一部分内容)

type Exam struct {
	global.GVA_MODEL
	ExamName              string    `json:"exam\_name" gorm:"index;not null;comment:考试名称"`
	ExamPrice             string    `json:"exam\_price" gorm:"not null;default:免费;comment:考试价格"`
	PassingGrade          string    `json:"passing\_grade" gorm:"not null;comment:及格分"`
	ExamDuration          string    `json:"exam\_duration" gorm:"not null;comment:考试时长"`
	NumberOfPoints        string    `json:"number\_of\_points" gorm:"comment:积分数量"`
	MinimumSubmissionTime string    `json:"minimum\_submission\_time" gorm:"comment:最低交卷时长"`
	NotesOnExam           string    `json:"notes\_on\_exam" gorm:"comment:考前注意事项"`
	Password              string    `json:"password" gorm:"comment:口令密码"`
	State                 string    `json:"state" gorm:"default:正常;comment:状态"`
	ExamState             string    `json:"exam\_state" gorm:"index;comment:考试状态"`
	ExamPermissionDetail  string    `json:"exam\_permission\_detail" gorm:"comment:考试权限"`   
	ExamTimeStart         time.Time `json:"exam\_time\_start" gorm:"index;comment:考试开始时间"`
	ExamTimeEnd           time.Time `json:"exam\_time\_end" gorm:"index;comment:考试结束时间"`
	LimitExamTime         bool      `json:"limit\_exam\_time" gorm:"comment:限制考试时间"`
	ExamTypeID            uint      `json:"exam\_type\_id" gorm:"not null;comment:考试类型"`
	TotalScore            uint      `json:"total\_score" gorm:"comment:考试总分"`
	SysUserID             uint      `json:"sys\_user\_id" gorm:"comment:指定人员Id"`
	ExamCreatedBy         uint      `json:"exam\_created\_by" gorm:"comment:考试创建人"`
	ExamPaperID           uint      `json:"exam\_paper\_id" gorm:"comment:试卷Id"`
	DeptID                uint      `json:"dept\_id" gorm:"comment:考试所属部门"`
	ExamPaper             ExamPaper `json:"exam\_paper" `
}

将考试基本信息以及试卷信息存入Redis

	var ExamId uint
	global.GVA_DB.Model(&exam.Exam{}).Select("id").Find(&ExamId)
	var ExamInfo exam.Exam
	//准备将查到的ExamInfo存入Redis中
	global.GVA_DB.Model(&exam.Exam{}).Where("id = ?", ExamId).First(&ExamInfo)
	// 过期时间=这场考试的开始时间-这场考试的结束时间
	passingDuration := ExamInfo.ExamTimeEnd.Sub(ExamInfo.ExamTimeStart)
	//将要存入redis的结构体序列化一下
	jsonExamInfo, _ := json.Marshal(ExamInfo) 
	//使用Hash数据结构存入结构体
	err = global.GVA_REDIS.HSet(c, "ExamInfo", string(ExamId), jsonExamInfo).Err()
	if err != nil {
		return errors2.New("考试信息加入Redis缓存失败")
	}
	// 给存入redis的hash元素加入过期时间
	global.GVA_REDIS.Expire(c, "ExamInfo:string(ExamId)", passingDuration)

存入redis的核心代码

  1. 先将结构体序列化
 var ExamInfo exam.Exam
 jsonExamInfo, _ := json.Marshal(ExamInfo) 

  1. 使用hash数据结构存储结构体(key:“ExamInfo”,field: string(ExamId), value:jsonExamInfo)
err = global.GVA_REDIS.HSet(c, "ExamInfo", string(ExamId), jsonExamInfo).Err()

把结构体从redis中取出

1.先从redis中取出来数据

jsonData, _ := global.GVA_REDIS.HGet(c, "ExamInfo", string(ID)).Result()

2.将数据反序列化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值