注:这个是我们社区APP的问答社区的 数据库保存及数据读取的流程; 流程如下: 保存流程====接到客户端数据保存到数据库--》内存数据库 获取流程====处理客户端请求--》读取内存数据库数据(此时已经和数据库没有关系了,在读取的数据的时候) // 保存数据库,和获取保存的数据库的ID ; 然后保存在内存数据库 数据库保存:
// 发表问答社区,保存到数据库
func PlayerAnswerDataSaveDBFun(iAuthorRoleUID int32, strAuthorName string, strAuthorIcon string, strAnswerContent string) bool {
var strSql string
var tmpAnswerInfo Global_Define.StAnswerInfo
// MySQL数据库语句
strSql = "INSERT INTO t_AnswerInfo(AuthorRoleUID,AuthorName,AuthorIcon,AnswerContent,InsertTime)" +
"VALUES( ?, ?, ?, ?, ?)"
stmt, err := GSeverdb.Prepare(strSql)
if err != nil {
Log_Eio.Log(err.Error())
Log_Eio.Fmt("11111", err.Error())
return false
}
//时间戳到具体显示的转化
iLastLoginTime := time.Now().Unix()
// 数据库写入执行 ,插入数据的操作。
res, err := stmt.Exec(iAuthorRoleUID, strAuthorName, strAuthorIcon, strAnswerContent, iLastLoginTime)
if err != nil {
Log_Eio.Log(err.Error())
Log_Eio.Fmt("11111", err.Error())
return false
}
// 获取帖子的ID
strSqlS := "select AnswerId from t_AnswerInfo where AuthorName = '" + strAuthorName + "' AND InsertTime = '" + strconv.Itoa(int(iLastLoginTime)) + "'"
Log_Eio.Log(strSqlS)
Log_Eio.Fmt(strSqlS)
Rows, err1 := GSeverdb.Query(strSqlS)
if err != nil {
Log_Eio.Log(err1.Error())
Log_Eio.Fmt(err1.Error())
return false
}
var TMPstrId int
defer Rows.Close()
for Rows.Next() {
err11 := Rows.Scan(&TMPstrId)
if err11 != nil {
Log_Eio.Fmt(err11)
}
}
// 日志的打印数据更新
Log_Eio.Fmt(res)
// 整合数据
tmpAnswerInfo.AuthorRoleUID = strconv.Itoa(int(iAuthorRoleUID))
tmpAnswerInfo.AuthorName = strAuthorName
tmpAnswerInfo.AuthorIcon = strAuthorIcon
tmpAnswerInfo.AnswerContent = strAnswerContent
tmpAnswerInfo.Instertime = strconv.Itoa(int(iLastLoginTime))
tmpAnswerInfo.Answerid = strconv.Itoa(int(TMPstrId))
// 更新到内存数据库
Redis_DB.Redis_Write_AnswerInfoData(strconv.Itoa(int(iLastLoginTime)), tmpAnswerInfo)
return true
}
保存内存数据库:
// 用户产生一条问答
// hkey : 为 问答产生的时间戳: Instertime
func Redis_Write_AnswerInfoData(strkey_Instertime string, stAnswerInfo Global_Define.StAnswerInfo) bool {
Log_Eio.Log("Entry Redis_Write_AnswerInfoData")
Log_Eio.Fmt("Entry Redis_Write_AnswerInfoData")
// 数据的存储
itimekey, _ := strconv.Atoi(stAnswerInfo.Instertime)
bytetmp, _ := json.Marshal(stAnswerInfo)
_, error := GRedis_Client.Zadd(AnswerInfoKey, float64(itimekey), bytetmp)
if error != nil {
Log_Eio.Fmt("GRedis_Client.Zadd: Set data ", error)
Log_Eio.Log("GRedis_Client.Zadd: Set data ", error.Error())
return false
}
return true
}
内存数据库获取数据:
// 获取问答社区帖子列表的内存的数据的信息
// strkeytime : 为是账单产生的时间
// 返回数值为,一个map数据,这个数据是多个数据的组合的操作,最多数据为也数据
func Redis_Read_AnswerListInfoData(strkeytime string) (AnswerInfo map[string]*Global_Define.StAnswerInfo) {
Log_Eio.Log("Entry Redis_Read_AnswerListInfoData")
Log_Eio.Fmt("Entry Redis_Read_AnswerListInfoData")
AnswerInfo = make(map[string]*Global_Define.StAnswerInfo)
ftmpData, _ := strconv.Atoi(strkeytime)
Log_Eio.Fmt("strconv.Atoi(strkeytime):", ftmpData)
// 获取内存数据库的数据
bytesMap, _ := GRedis_Client.Zrevrange(AnswerInfoKey, 0, -1)
Log_Eio.Fmt("GRedis_Client.Zrevrange return:", bytesMap)
// 这里就需要对比时间,数据的操作,主要的数据的
// 1 循环取到数据。
var vcount = 0
var AnswerInfotmp *Global_Define.StAnswerInfo
for member, score := range bytesMap {
AnswerInfotmp = new(Global_Define.StAnswerInfo)
// 如果时间戳大于或者等于member
if ftmpData >= member {
// 临时的 转换
json.Unmarshal(score, AnswerInfotmp) // score转换结构体
// 判断数据与自己有关不
vcount++
Log_Eio.Fmt("GRedis_Client.Zrevrange string(score)", string(score))
//test
//json转map
var r Requestbody
r.req = string(score)
if req2map, err := r.Json2map(); err == nil {
// s := fmt.Sprintf("a %s", "string")
// fmt.Println(s)
AuthorRoleUID := fmt.Sprintf("%s", req2map["AuthorRoleUID"])
AuthorName := fmt.Sprintf("%s", req2map["AuthorName"])
AuthorIcon := fmt.Sprintf("%s", req2map["AuthorIcon"])
AnswerContent := fmt.Sprintf("%s", req2map["AnswerContent"])
Instertime := fmt.Sprintf("%s", req2map["Instertime"])
Answerid := fmt.Sprintf("%s", req2map["Answerid"])
// 转换的
AnswerInfotmp.AuthorRoleUID = AuthorRoleUID
AnswerInfotmp.AuthorName = AuthorName
AnswerInfotmp.AuthorIcon = AuthorIcon
AnswerInfotmp.AnswerContent = AnswerContent
AnswerInfotmp.Instertime = Instertime
AnswerInfotmp.Answerid = Answerid
Log_Eio.Fmt("------------------------------------------------")
Log_Eio.Fmt("AnswerInfotmp.Answerid", AnswerInfotmp.Answerid)
Log_Eio.Fmt("------------------------------------------------")
Log_Eio.Fmt("GRedis_Client.Zrevrange AnswerInfotmp:", AnswerInfotmp)
} else {
Log_Eio.Fmt("GRedis_Client.Zrevrange AnswerInfo[vcount]:error")
}
AnswerInfo[strconv.Itoa(vcount)] = AnswerInfotmp
Log_Eio.Fmt("GRedis_Client.Zrevrange AnswerInfo[vcount]:", AnswerInfo[strconv.Itoa(vcount)])
}
// 退出循环,当达到20条数据的时候
if vcount >= 20 {
return AnswerInfo
}
}
return AnswerInfo
}