Skip to content

Commit

Permalink
fix: 优化缓存逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Redmomn committed Nov 27, 2024
1 parent 96210f1 commit 96c7639
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
43 changes: 41 additions & 2 deletions client/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ func (c *QQClient) GetUID(uin uint32, groupUin ...uint32) string {
return ""
}
}
if uid := c.cache.GetUID(uin, groupUin...); uid != "" {
return uid
}
if len(groupUin) == 0 {
_ = c.RefreshFriendCache()
} else {
_ = c.RefreshGroupMembersCache(groupUin[0])
}
return c.cache.GetUID(uin, groupUin...)
}

Expand All @@ -32,6 +40,14 @@ func (c *QQClient) GetUin(uid string, groupUin ...uint32) uint32 {
return 0
}
}
if uin := c.cache.GetUin(uid, groupUin...); uin != 0 {
return uin
}
if len(groupUin) == 0 {
_ = c.RefreshFriendCache()
} else {
_ = c.RefreshGroupMembersCache(groupUin[0])
}
return c.cache.GetUin(uid, groupUin...)
}

Expand All @@ -42,6 +58,10 @@ func (c *QQClient) GetCachedFriendInfo(uin uint32) *entity.User {
return nil
}
}
if friend := c.cache.GetFriend(uin); friend != nil {
return friend
}
_ = c.RefreshFriendCache()
return c.cache.GetFriend(uin)
}

Expand All @@ -62,6 +82,10 @@ func (c *QQClient) GetCachedGroupInfo(groupUin uint32) *entity.Group {
return nil
}
}
if g := c.cache.GetGroupInfo(groupUin); g != nil {
return g
}
_ = c.RefreshAllGroupsInfo()
return c.cache.GetGroupInfo(groupUin)
}

Expand All @@ -82,6 +106,10 @@ func (c *QQClient) GetCachedMemberInfo(uin, groupUin uint32) *entity.GroupMember
return nil
}
}
if m := c.cache.GetGroupMember(uin, groupUin); m != nil {
return m
}
_ = c.RefreshGroupMemberCache(uin, groupUin)
return c.cache.GetGroupMember(uin, groupUin)
}

Expand All @@ -92,6 +120,10 @@ func (c *QQClient) GetCachedMembersInfo(groupUin uint32) map[uint32]*entity.Grou
return nil
}
}
if gm := c.cache.GetGroupMembers(groupUin); gm != nil {
return gm
}
_ = c.RefreshGroupMembersCache(groupUin)
return c.cache.GetGroupMembers(groupUin)
}

Expand Down Expand Up @@ -139,11 +171,18 @@ func (c *QQClient) GetCachedRkeyInfos() map[entity.RKeyType]*entity.RKeyInfo {

// RefreshFriendCache 刷新好友缓存
func (c *QQClient) RefreshFriendCache() error {
friendsData, err := c.GetFriendsData()
friends, err := c.GetFriendsData()
if err != nil {
return err
}
c.cache.RefreshAllFriend(friends)
unidirectionalFriends, err := c.GetUnidirectionalFriendList()
if err != nil {
return err
}
c.cache.RefreshAllFriend(friendsData)
for _, f := range unidirectionalFriends {
c.cache.RefreshFriend(f)
}
return nil
}

Expand Down
5 changes: 3 additions & 2 deletions client/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ func (c *QQClient) SendGroupSign(groupUin uint32) (*oidb2.BotGroupClockInResult,

// GetUnidirectionalFriendList 获取单向好友列表
// ref https://github.com/Mrs4s/MiraiGo/blob/54bdd873e3fed9fe1c944918924674dacec5ac76/client/web.go#L23
func (c *QQClient) GetUnidirectionalFriendList() (ret []*entity.User, err error) {
func (c *QQClient) GetUnidirectionalFriendList() ([]*entity.User, error) {
webRsp := &struct {
BlockList []struct {
Uin uint32 `json:"uint64_uin"`
Expand All @@ -1355,6 +1355,7 @@ func (c *QQClient) GetUnidirectionalFriendList() (ret []*entity.User, err error)
if webRsp.ErrorCode != 0 {
return nil, fmt.Errorf("web sso request error: %v", webRsp.ErrorCode)
}
ret := make([]*entity.User, 0, len(webRsp.BlockList))
for _, block := range webRsp.BlockList {
decodeBase64String := func(str string) string {
b, err := base64.StdEncoding.DecodeString(str)
Expand All @@ -1371,7 +1372,7 @@ func (c *QQClient) GetUnidirectionalFriendList() (ret []*entity.User, err error)
Source: decodeBase64String(block.SourceBytes),
})
}
return
return ret, err
}

// DeleteUnidirectionalFriend 删除单向好友
Expand Down

0 comments on commit 96c7639

Please sign in to comment.