Skip to content

Commit

Permalink
fix & refactor: client cache and more detailed variable name
Browse files Browse the repository at this point in the history
  • Loading branch information
Redmomn committed Apr 19, 2024
1 parent b846e8d commit f53caf3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
6 changes: 3 additions & 3 deletions cache/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ type Cache struct {
refreshLock sync.RWMutex
// FriendCache 好友缓存 uin *entity.Friend
FriendCache map[uint32]*entity.Friend
// GroupCache 群信息缓存 groupUin *entity.Group
GroupCache map[uint32]*entity.Group
// GroupInfoCache 群信息缓存 groupUin *entity.Group
GroupInfoCache map[uint32]*entity.Group
// GroupMemberCache 群内群员信息缓存 groupUin uin *entity.GroupMember
GroupMemberCache map[uint32]map[uint32]*entity.GroupMember
}

func NewCache() *Cache {
return &Cache{
FriendCache: make(map[uint32]*entity.Friend),
GroupCache: make(map[uint32]*entity.Group),
GroupInfoCache: make(map[uint32]*entity.Group),
GroupMemberCache: make(map[uint32]map[uint32]*entity.GroupMember),
}
}
26 changes: 18 additions & 8 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ func (c *Cache) GetFriend(uin uint32) *entity.Friend {
return c.FriendCache[uin]
}

// GetGroup 获取群聊信息
func (c *Cache) GetGroup(groupUin uint32) *entity.Group {
// GetGroupInfo 获取群信息
func (c *Cache) GetGroupInfo(groupUin uint32) *entity.Group {
c.refreshLock.RLock()
defer c.refreshLock.RUnlock()
return c.GroupCache[groupUin]
return c.GroupInfoCache[groupUin]
}

// GetAllGroups 获取所有群聊信息
func (c *Cache) GetAllGroups() map[uint32]*entity.Group {
// GetAllGroupsInfo 获取所有群信息
func (c *Cache) GetAllGroupsInfo() map[uint32]*entity.Group {
c.refreshLock.RLock()
defer c.refreshLock.RUnlock()
groups := make(map[uint32]*entity.Group, len(c.GroupCache))
for group, grpInfo := range c.GroupCache {
groups := make(map[uint32]*entity.Group, len(c.GroupInfoCache))
for group, grpInfo := range c.GroupInfoCache {
groups[group] = grpInfo
}
return groups
Expand All @@ -88,20 +88,30 @@ func (c *Cache) GetGroupMembers(groupUin uint32) map[uint32]*entity.GroupMember
return members
}

// FriendCacheIsEmpty 好友信息缓存是否为空
func (c *Cache) FriendCacheIsEmpty() bool {
c.refreshLock.RLock()
defer c.refreshLock.RUnlock()
return len(c.FriendCache) == 0
}

func (c *Cache) GroupCacheIsEmpty() bool {
// GroupMembersCacheIsEmpty 群成员缓存是否为空
func (c *Cache) GroupMembersCacheIsEmpty() bool {
c.refreshLock.RLock()
defer c.refreshLock.RUnlock()
return len(c.GroupMemberCache) == 0
}

// GroupMemberCacheIsEmpty 指定群的群成员缓存是否为空
func (c *Cache) GroupMemberCacheIsEmpty(groupUin uint32) bool {
c.refreshLock.RLock()
defer c.refreshLock.RUnlock()
return len(c.GroupMemberCache[groupUin]) == 0
}

// GroupInfoCacheIsEmpty 群信息缓存是否为空
func (c *Cache) GroupInfoCacheIsEmpty() bool {
c.refreshLock.RLock()
defer c.refreshLock.RUnlock()
return len(c.GroupInfoCache) == 0
}
6 changes: 3 additions & 3 deletions cache/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func (c *Cache) RefreshAll(friendCache map[uint32]*entity.Friend, groupCache map
defer c.refreshLock.Unlock()
c.FriendCache = friendCache
c.GroupMemberCache = groupMemberCache
c.GroupCache = groupCache
c.GroupInfoCache = groupCache
}

// RefreshFriend 刷新一个好友的缓存
Expand Down Expand Up @@ -51,12 +51,12 @@ func (c *Cache) RefreshAllGroupMembers(groupMemberCache map[uint32]map[uint32]*e
func (c *Cache) RefreshGroup(group *entity.Group) {
c.refreshLock.Lock()
defer c.refreshLock.Unlock()
c.GroupCache[group.GroupUin] = group
c.GroupInfoCache[group.GroupUin] = group
}

// RefreshAllGroup 刷新所有群的群信息缓存
func (c *Cache) RefreshAllGroup(groupCache map[uint32]*entity.Group) {
c.refreshLock.Lock()
defer c.refreshLock.Unlock()
c.GroupCache = groupCache
c.GroupInfoCache = groupCache
}
54 changes: 46 additions & 8 deletions client/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,40 @@ func (c *QQClient) GetUin(uid string, groupUin ...uint32) uint32 {
return c.cache.GetUin(uid, groupUin...)
}

// GetFriendInfo 获取好友信息
func (c *QQClient) GetFriendInfo(uin uint32) *entity.Friend {
// GetCachedFriendInfo 获取好友信息(缓存)
func (c *QQClient) GetCachedFriendInfo(uin uint32) *entity.Friend {
if c.cache.FriendCacheIsEmpty() {
c.RefreshFriendCache()
}
return c.cache.GetFriend(uin)
}

// GetMemberInfo 获取群成员信息
func (c *QQClient) GetMemberInfo(uin, groupUin uint32) *entity.GroupMember {
// GetCachedGroupInfo 获取群信息(缓存)
func (c *QQClient) GetCachedGroupInfo(groupUin uint32) *entity.Group {
if c.cache.GroupInfoCacheIsEmpty() {
c.RefreshGroupMembersCache(groupUin)
}
return c.cache.GetGroupInfo(groupUin)
}

// GetCachedAllGroupsInfo 获取所有群信息(缓存)
func (c *QQClient) GetCachedAllGroupsInfo() map[uint32]*entity.Group {
if c.cache.GroupInfoCacheIsEmpty() {
c.RefreshAllGroupsInfo()
}
return c.cache.GetAllGroupsInfo()
}

// GetCachedMemberInfo 获取群成员信息(缓存)
func (c *QQClient) GetCachedMemberInfo(uin, groupUin uint32) *entity.GroupMember {
if c.cache.GroupMemberCacheIsEmpty(groupUin) {
c.RefreshGroupMembersCache(groupUin)
}
return c.cache.GetGroupMember(uin, groupUin)
}

func (c *QQClient) GetMembersInfo(groupUin uint32) map[uint32]*entity.GroupMember {
// GetCachedMembersInfo 获取指定群所有群成员信息(缓存)
func (c *QQClient) GetCachedMembersInfo(groupUin uint32) map[uint32]*entity.GroupMember {
if c.cache.GroupMemberCacheIsEmpty(groupUin) {
c.RefreshGroupMembersCache(groupUin)
}
Expand All @@ -64,15 +81,23 @@ func (c *QQClient) RefreshGroupMembersCache(groupUin uint32) {
c.cache.RefreshGroupMembers(groupUin, groupData)
}

// RefreshAllGroupCache 刷新所有群的群成员缓存
func (c *QQClient) RefreshAllGroupCache() {
// RefreshAllGroupMembersCache 刷新所有群的群成员缓存
func (c *QQClient) RefreshAllGroupMembersCache() {
groupsData, err := c.GetAllGroupsMembersData()
if err != nil {
return
}
c.cache.RefreshAllGroupMembers(groupsData)
}

func (c *QQClient) RefreshAllGroupsInfo() {
groupsData, err := c.GetAllGroupsInfo()
if err != nil {
return
}
c.cache.RefreshAllGroup(groupsData)
}

// GetFriendsData 获取好友列表数据
func (c *QQClient) GetFriendsData() (map[uint32]*entity.Friend, error) {
friends, err := c.FetchFriends()
Expand Down Expand Up @@ -123,6 +148,19 @@ func (c *QQClient) GetAllGroupsMembersData() (map[uint32]map[uint32]*entity.Grou
}
groupsData[group.GroupUin] = groupMembersData
}
loginLogger.Infof("获取%d个群和成员信息", len(groupsData))
loginLogger.Infof("获取%d个群的成员信息", len(groupsData))
return groupsData, err
}

func (c *QQClient) GetAllGroupsInfo() (map[uint32]*entity.Group, error) {
groupsInfo, err := c.FetchGroups()
if err != nil {
return nil, err
}
groupsData := make(map[uint32]*entity.Group, len(groupsInfo))
for _, group := range groupsInfo {
groupsData[group.GroupUin] = group
}
loginLogger.Infof("获取%d个群信息", len(groupsData))
return groupsData, err
}
2 changes: 1 addition & 1 deletion client/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func preprocessMessage(client *QQClient, groupUin uint32, elements []message2.IM
for _, element := range elements {
switch elem := element.(type) {
case *message2.AtElement:
member := client.GetMemberInfo(elem.Target, groupUin)
member := client.GetCachedMemberInfo(elem.Target, groupUin)
if member != nil {
elem.UID = member.Uid
if member.MemberCard != "" {
Expand Down

0 comments on commit f53caf3

Please sign in to comment.