Skip to content

Commit

Permalink
Add user map for cached
Browse files Browse the repository at this point in the history
  • Loading branch information
ego008 committed May 7, 2024
1 parent 0fd09c0 commit 6e0f96e
Show file tree
Hide file tree
Showing 24 changed files with 70 additions and 43 deletions.
4 changes: 2 additions & 2 deletions controller/admin_comment_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func (h *BaseHandler) AdminCommentEditPage(ctx *fasthttp.RequestCtx) {
comment := model.CommentGetById(db, tidI, cidI)

evn := &admin.CommentEdit{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "评论修改"
evn.PageName = "admin_comment_edit"

author, _ := model.UserGetById(db, comment.UserId)

if author.ID == 0 {
author = curUser
author = evn.CurrentUser
}

evn.NodeLst = model.NodeGetAll(h.App.Mc, h.App.Db)
Expand Down
4 changes: 2 additions & 2 deletions controller/admin_comment_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (h *BaseHandler) AdminCommentReviewPage(ctx *fasthttp.RequestCtx) {
db := h.App.Db

evn := &admin.CommentEdit{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "待审核评论"
evn.PageName = "admin_comment_review"
Expand Down Expand Up @@ -56,7 +56,7 @@ func (h *BaseHandler) AdminCommentReviewPage(ctx *fasthttp.RequestCtx) {
author, _ = model.UserGetById(db, rec.UserId)
}
if author.ID == 0 {
author = curUser
author = evn.CurrentUser
}

evn.NodeLst = model.NodeGetAll(h.App.Mc, h.App.Db)
Expand Down
2 changes: 1 addition & 1 deletion controller/admin_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (h *BaseHandler) AdminLinkPage(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &admin.Link{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "链接管理"
evn.PageName = "admin_link"
Expand Down
2 changes: 1 addition & 1 deletion controller/admin_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (h *BaseHandler) AdminNodePage(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &admin.Node{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "分区管理"
evn.PageName = "admin_node"
Expand Down
2 changes: 1 addition & 1 deletion controller/admin_ratelimit_iplookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (h *BaseHandler) AdminRateLimitIpLookup(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &admin.IpLookup{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "Ip Lookup"
evn.PageName = "admin_IpLookup"
Expand Down
2 changes: 1 addition & 1 deletion controller/admin_ratelimit_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (h *BaseHandler) AdminRateLimitSetting(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &admin.RateLimitSetting{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "Rate Limit Setting"
evn.PageName = "admin_RateLimitSetting"
Expand Down
2 changes: 1 addition & 1 deletion controller/admin_site_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (h *BaseHandler) AdminSiteConfigPage(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &admin.SiteConfig{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "网站设置"
evn.PageName = "admin_site_setting"
Expand Down
2 changes: 1 addition & 1 deletion controller/admin_site_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (h *BaseHandler) AdminSiteRouterPage(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &admin.SiteRouter{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "自定义路由"
evn.PageName = "admin_site_router"
Expand Down
4 changes: 2 additions & 2 deletions controller/admin_topic_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (h *BaseHandler) AdminTopicAddPage(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &admin.TopicAdd{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "发表文章"
evn.PageName = "topic_input"
Expand All @@ -35,7 +35,7 @@ func (h *BaseHandler) AdminTopicAddPage(ctx *fasthttp.RequestCtx) {
NodeId: 1,
UserId: curUser.ID,
}
evn.DefaultUser = curUser
evn.DefaultUser = evn.CurrentUser
evn.HasMsg = model.MsgCheckHasOne(db, curUser.ID)
// evn.DefaultNode, _ = model.NodeGetById(h.App.Db, evn.DefaultTopic.NodeId)
// evn.DefaultNode = model.Node{}
Expand Down
4 changes: 2 additions & 2 deletions controller/admin_topic_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (h *BaseHandler) AdminTopicEditPage(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &admin.TopicAdd{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "编辑帖子"
evn.PageName = "admin_topic_edit"
Expand Down Expand Up @@ -54,7 +54,7 @@ func (h *BaseHandler) AdminTopicEditPage(ctx *fasthttp.RequestCtx) {
author, _ = model.UserGetById(h.App.Db, rec.UserId)
}
if author.ID == 0 {
author = curUser
author = evn.CurrentUser
}

evn.ReadMoreBreak = model.ReadMoreBreak
Expand Down
4 changes: 2 additions & 2 deletions controller/admin_topic_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (h *BaseHandler) AdminTopicReviewPage(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &admin.TopicAdd{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "待审核帖子"
evn.PageName = "admin_topic_review"
Expand Down Expand Up @@ -53,7 +53,7 @@ func (h *BaseHandler) AdminTopicReviewPage(ctx *fasthttp.RequestCtx) {
author, _ = model.UserGetById(h.App.Db, rec.UserId)
}
if author.ID == 0 {
author = curUser
author = evn.CurrentUser
}

evn.DefaultTopic = model.Topic{
Expand Down
2 changes: 1 addition & 1 deletion controller/admin_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (h *BaseHandler) AdminUserPage(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &admin.User{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "用户管理"

Expand Down
42 changes: 29 additions & 13 deletions controller/base_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/valyala/fasthttp"
"goyoubbs/model"
"net"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -57,26 +58,41 @@ func ReadUserIP(ctx *fasthttp.RequestCtx) string {
return ""
}

func (h *BaseHandler) CurrentUser(ctx *fasthttp.RequestCtx) (model.User, error) {
var user model.User
func (h *BaseHandler) CurrentUser(ctx *fasthttp.RequestCtx) (*model.User, error) {
user := &model.User{}
ssValue := h.GetCookie(ctx, "SessionID")
if len(ssValue) == 0 {
return user, errors.New("SessionID cookie not found ")
}
z := strings.Split(ssValue, ":")
uid := z[0]
sessionID := z[1]

rs := h.App.Db.Hget(model.UserTbName, sdb.DS2b(uid))
if rs.State == "ok" {
_ = json.Unmarshal(rs.Data[0], &user)
if sessionID == user.Session {
_ = h.SetCookie(ctx, "SessionID", ssValue, 365)
return user, nil
index := strings.Index(ssValue, ":")
if index == -1 {
return user, errors.New("SessionID cookie not found ")
}
uId, _ := strconv.ParseUint(ssValue[:index], 10, 64)
if uId == 0 {
return user, errors.New("UserID is 0 ")
}
var ok bool
model.UserMapMux.RLock()
user, ok = model.UserMap[uId]
model.UserMapMux.RUnlock()
if !ok {
rs := h.App.Db.Hget(model.UserTbName, sdb.I2b(uId))
if rs.OK() {
_ = json.Unmarshal(rs.Data[0], &user)
if user.ID > 0 {
model.UserMapMux.Lock()
model.UserMap[uId] = user
model.UserMapMux.Unlock()
}
}
}
if user != nil {
_ = h.SetCookie(ctx, "SessionID", ssValue, 365)
return user, nil
}

return user, errors.New("user not found")
return &model.User{}, errors.New("user not found")
}

func (h *BaseHandler) SetCookie(ctx *fasthttp.RequestCtx, name, value string, days int) error {
Expand Down
2 changes: 1 addition & 1 deletion controller/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (h *BaseHandler) HomePage(ctx *fasthttp.RequestCtx) {
evn := &ybs.HomePage{}
evn.SiteCf = scf
evn.Title = scf.Name
evn.CurrentUser = curUser
evn.CurrentUser = *curUser

evn.SiteInfo = model.GetSiteInfo(db)
evn.DefaultNode = model.Node{ID: 1}
Expand Down
2 changes: 1 addition & 1 deletion controller/member_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (h *BaseHandler) MemberPage(ctx *fasthttp.RequestCtx) {
evn := &ybs.MemberPage{}
evn.SiteCf = scf
evn.Title = "会员: " + user.Name + " 最近" + titleText + " - " + scf.Name
evn.CurrentUser = curUser
evn.CurrentUser = *curUser

evn.NodeLst = model.NodeGetAll(h.App.Mc, db)
evn.TopicPageInfo = topicPageInfo
Expand Down
2 changes: 1 addition & 1 deletion controller/my_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (h *BaseHandler) MyMsgPage(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &ybs.MyMsg{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "未读信息"

Expand Down
2 changes: 1 addition & 1 deletion controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (h *BaseHandler) NodePage(ctx *fasthttp.RequestCtx) {
evn := &ybs.NodePage{}
evn.SiteCf = scf
evn.Title = "Category: " + node.Name + " - " + scf.Name
evn.CurrentUser = curUser
evn.CurrentUser = *curUser

evn.DefaultNode = node
evn.NodeLst = model.NodeGetAll(h.App.Mc, db)
Expand Down
2 changes: 1 addition & 1 deletion controller/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (h *BaseHandler) SearchPage(ctx *fasthttp.RequestCtx) {
evn := &ybs.SearchPage{}
evn.SiteCf = scf
evn.Title = "搜索: " + q + " - " + scf.Name
evn.CurrentUser = curUser
evn.CurrentUser = *curUser

evn.Q = q
evn.NodeLst = model.NodeGetAll(h.App.Mc, db)
Expand Down
2 changes: 1 addition & 1 deletion controller/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (h *BaseHandler) TagPage(ctx *fasthttp.RequestCtx) {
evn := &ybs.TagPage{}
evn.SiteCf = scf
evn.Title = "Tag: " + tagRaw + " - " + scf.Name
evn.CurrentUser = curUser
evn.CurrentUser = *curUser

evn.Tag = tagLower
evn.NodeLst = model.NodeGetAll(h.App.Mc, db)
Expand Down
4 changes: 2 additions & 2 deletions controller/topic_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (h *BaseHandler) TopicAddPage(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &ybs.UserTopicAdd{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "发表文章"
evn.PageName = "topic_input"
Expand All @@ -42,7 +42,7 @@ func (h *BaseHandler) TopicAddPage(ctx *fasthttp.RequestCtx) {
NodeId: 1,
UserId: curUser.ID,
}
evn.DefaultUser = curUser
evn.DefaultUser = evn.CurrentUser
evn.HasMsg = model.MsgCheckHasOne(db, curUser.ID)
evn.DefaultNode = node
evn.NodeLst = model.NodeGetAll(h.App.Mc, db)
Expand Down
2 changes: 1 addition & 1 deletion controller/topic_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h *BaseHandler) TopicDetailPage(ctx *fasthttp.RequestCtx) {
safeTitle := html.EscapeString(topic.Title)

evn := &ybs.TopicDetailPage{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = safeTitle + " - " + scf.Name
evn.Keywords = topic.Tags
Expand Down
6 changes: 3 additions & 3 deletions controller/user_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func (h *BaseHandler) UserSettingPage(ctx *fasthttp.RequestCtx) {
scf := h.App.Cf.Site

evn := &ybs.UserSetting{}
evn.CurrentUser = curUser
evn.CurrentUser = *curUser
evn.SiteCf = scf
evn.Title = "个人设置"

//
evn.User = curUser
evn.User = evn.CurrentUser

evn.NodeLst = model.NodeGetAll(h.App.Mc, h.App.Db)

Expand Down Expand Up @@ -71,7 +71,7 @@ func (h *BaseHandler) UserSettingPost(ctx *fasthttp.RequestCtx) {
db := h.App.Db

// 编辑
obj := curUser
obj := *curUser

if len(rec.Password) > 0 && len(rec.Password0) > 0 {
if rec.Password0 != obj.Password {
Expand Down
7 changes: 6 additions & 1 deletion model/cached.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package model

import "sync/atomic"
import (
"sync"
"sync/atomic"
)

var (
RateLimitDay int
RateLimitHour int
BadIpPrefixLst = NewConStrSlice() // 17.121
AllowIpPrefixLst = NewConStrSlice() // Manual input, select from all ipInfo list
BadBotNameMap atomic.Value //map[string]interface{}{} // key: string, value: name. DataForSeoBot,SeznamBot,GrapeshotCrawler,
UserMapMux = &sync.RWMutex{}
UserMap = make(map[uint64]*User) // current user map key: uint64 user id
)

func init() {
Expand Down
6 changes: 6 additions & 0 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ func UserSet(db *sdb.DB, obj User) User {
return User{}
}
_ = db.Hset(UserTbName, sdb.I2b(obj.ID), jb)
// update UserMap
UserMapMux.Lock()
if _, ok := UserMap[obj.ID]; ok {
UserMap[obj.ID] = &obj
}
UserMapMux.Unlock()
return obj
}

Expand Down

0 comments on commit 6e0f96e

Please sign in to comment.