1 Star 1 Fork 0

凡卡/libp2parea

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
message_log.go 2.14 KB
一键复制 编辑 原始数据 按行查看 历史
凡卡 提交于 2023-11-29 11:01 . first commit
package persistence
import (
// "fmt"
gconfig "gitee.com/prestonTao/libp2parea/config"
"os"
"path/filepath"
"sync/atomic"
"time"
)
var msgTableKey uint64 = 0 //消息表主键,id自增长
/*
加载表格最大id
*/
func loadMsgTableKey() error {
rows, err := db.Query("select * from message order by id desc limit ?", 1)
if err != nil {
return err
}
for rows.Next() {
err = rows.Scan(&msgTableKey)
if err != nil {
break
}
}
// fmt.Println("加载的日志id最大为:", msgTableKey)
return err
}
/*
按页倒序读取日志
@count int 一页显示数量
@order int64 unix时间
*/
func findMsgLogPage(count int, order int64) ([]Message, error) {
//select * from message where updatetime > 0 order by updatetime desc limit 10
msgs := make([]Message, 0)
rows, err := db.Query("select * from message where updatetime > ? order by updatetime desc limit ?", order, count)
if err != nil {
return nil, err
}
for rows.Next() {
msg := Message{}
err = rows.Scan(&msg.Id, &msg.Sender, &msg.Recver, &msg.Content, &msg.UpdateTime)
if err != nil {
break
}
msgs = append(msgs, msg)
}
return msgs, err
}
/*
保存消息日志
*/
func SaveMsgLog(sendId, recver, content string) error {
// return tracefile(name, sendId, content)
stmt, err := db.Prepare("insert into message values(?,?,?,?,?)")
if err != nil {
return err
}
stmt.Exec(atomic.AddUint64(&msgTableKey, 1), sendId, recver, content, time.Now().Unix())
// friendIdsLock.Lock()
// friendIds[id] = id
// friendIdsLock.Unlock()
return nil
}
/*
打印内容到文件中
*/
func tracefile(name, sendId, content string) error {
fd, err := os.OpenFile(filepath.Join(gconfig.Path_configDir, folderName_msg, name), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err
}
fd_time := time.Now().Format("2006-01-02 15:04:05")
// fd_content := strings.Join([]string{"======", fd_time, "=====", str_content, "\n"}, "")
buf := []byte(sendId + " " + fd_time + " " + content + "\r\n")
fd.Write(buf)
fd.Close()
return nil
}
type Message struct {
Id int
Sender string
Recver string
Content string
UpdateTime int64
UpdateNano int64
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/prestonTao/libp2parea.git
git@gitee.com:prestonTao/libp2parea.git
prestonTao
libp2parea
libp2parea
3aaa451ef873

搜索帮助