首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go语言社区 APP --问答模块数据存储流程及代码

Go语言社区 APP --问答模块数据存储流程及代码

作者头像
李海彬
发布2018-03-19 17:30:51
7980
发布2018-03-19 17:30:51
举报
文章被收录于专栏:Golang语言社区Golang语言社区

注:这个是我们社区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

}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-01-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档