Initial backend repository commit.
Set up project files and add .gitignore to exclude local build/runtime artifacts. Made-with: Cursor
This commit is contained in:
147
internal/cache/keys.go
vendored
Normal file
147
internal/cache/keys.go
vendored
Normal file
@@ -0,0 +1,147 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// 缓存键前缀常量
|
||||
const (
|
||||
// 帖子相关
|
||||
PrefixPostList = "posts:list"
|
||||
PrefixPost = "posts:detail"
|
||||
|
||||
// 会话相关
|
||||
PrefixConversationList = "conversations:list"
|
||||
PrefixConversationDetail = "conversations:detail"
|
||||
|
||||
// 群组相关
|
||||
PrefixGroupMembers = "groups:members"
|
||||
PrefixGroupInfo = "groups:info"
|
||||
|
||||
// 未读数相关
|
||||
PrefixUnreadSystem = "unread:system"
|
||||
PrefixUnreadConversation = "unread:conversation"
|
||||
PrefixUnreadDetail = "unread:detail"
|
||||
|
||||
// 用户相关
|
||||
PrefixUserInfo = "users:info"
|
||||
PrefixUserMe = "users:me"
|
||||
)
|
||||
|
||||
// PostListKey 生成帖子列表缓存键
|
||||
// postType: 帖子类型 (recommend, hot, follow, latest)
|
||||
// page: 页码
|
||||
// pageSize: 每页数量
|
||||
// userID: 用户维度(仅在个性化列表如 follow 场景使用)
|
||||
func PostListKey(postType string, userID string, page, pageSize int) string {
|
||||
if userID == "" {
|
||||
return fmt.Sprintf("%s:%s:%d:%d", PrefixPostList, postType, page, pageSize)
|
||||
}
|
||||
return fmt.Sprintf("%s:%s:%s:%d:%d", PrefixPostList, postType, userID, page, pageSize)
|
||||
}
|
||||
|
||||
// PostDetailKey 生成帖子详情缓存键
|
||||
func PostDetailKey(postID string) string {
|
||||
return fmt.Sprintf("%s:%s", PrefixPost, postID)
|
||||
}
|
||||
|
||||
// ConversationListKey 生成会话列表缓存键
|
||||
func ConversationListKey(userID string, page, pageSize int) string {
|
||||
return fmt.Sprintf("%s:%s:%d:%d", PrefixConversationList, userID, page, pageSize)
|
||||
}
|
||||
|
||||
// ConversationDetailKey 生成会话详情缓存键
|
||||
func ConversationDetailKey(conversationID, userID string) string {
|
||||
return fmt.Sprintf("%s:%s:%s", PrefixConversationDetail, conversationID, userID)
|
||||
}
|
||||
|
||||
// GroupMembersKey 生成群组成员缓存键
|
||||
func GroupMembersKey(groupID string, page, pageSize int) string {
|
||||
return fmt.Sprintf("%s:%s:page:%d:size:%d", PrefixGroupMembers, groupID, page, pageSize)
|
||||
}
|
||||
|
||||
// GroupMembersAllKey 生成群组全量成员ID列表缓存键
|
||||
func GroupMembersAllKey(groupID string) string {
|
||||
return fmt.Sprintf("%s:all:%s", PrefixGroupMembers, groupID)
|
||||
}
|
||||
|
||||
// GroupInfoKey 生成群组信息缓存键
|
||||
func GroupInfoKey(groupID string) string {
|
||||
return fmt.Sprintf("%s:%s", PrefixGroupInfo, groupID)
|
||||
}
|
||||
|
||||
// UnreadSystemKey 生成系统消息未读数缓存键
|
||||
func UnreadSystemKey(userID string) string {
|
||||
return fmt.Sprintf("%s:%s", PrefixUnreadSystem, userID)
|
||||
}
|
||||
|
||||
// UnreadConversationKey 生成会话未读总数缓存键
|
||||
func UnreadConversationKey(userID string) string {
|
||||
return fmt.Sprintf("%s:%s", PrefixUnreadConversation, userID)
|
||||
}
|
||||
|
||||
// UnreadDetailKey 生成单个会话未读数缓存键
|
||||
func UnreadDetailKey(userID, conversationID string) string {
|
||||
return fmt.Sprintf("%s:%s:%s", PrefixUnreadDetail, userID, conversationID)
|
||||
}
|
||||
|
||||
// UserInfoKey 生成用户信息缓存键
|
||||
func UserInfoKey(userID string) string {
|
||||
return fmt.Sprintf("%s:%s", PrefixUserInfo, userID)
|
||||
}
|
||||
|
||||
// UserMeKey 生成当前用户信息缓存键
|
||||
func UserMeKey(userID string) string {
|
||||
return fmt.Sprintf("%s:%s", PrefixUserMe, userID)
|
||||
}
|
||||
|
||||
// InvalidatePostList 失效帖子列表缓存
|
||||
func InvalidatePostList(cache Cache) {
|
||||
cache.DeleteByPrefix(PrefixPostList)
|
||||
}
|
||||
|
||||
// InvalidatePostDetail 失效帖子详情缓存
|
||||
func InvalidatePostDetail(cache Cache, postID string) {
|
||||
cache.Delete(PostDetailKey(postID))
|
||||
}
|
||||
|
||||
// InvalidateConversationList 失效会话列表缓存
|
||||
func InvalidateConversationList(cache Cache, userID string) {
|
||||
cache.DeleteByPrefix(PrefixConversationList + ":" + userID + ":")
|
||||
}
|
||||
|
||||
// InvalidateConversationDetail 失效会话详情缓存
|
||||
func InvalidateConversationDetail(cache Cache, conversationID, userID string) {
|
||||
cache.Delete(ConversationDetailKey(conversationID, userID))
|
||||
}
|
||||
|
||||
// InvalidateGroupMembers 失效群组成员缓存
|
||||
func InvalidateGroupMembers(cache Cache, groupID string) {
|
||||
cache.DeleteByPrefix(PrefixGroupMembers + ":" + groupID)
|
||||
}
|
||||
|
||||
// InvalidateGroupInfo 失效群组信息缓存
|
||||
func InvalidateGroupInfo(cache Cache, groupID string) {
|
||||
cache.Delete(GroupInfoKey(groupID))
|
||||
}
|
||||
|
||||
// InvalidateUnreadSystem 失效系统消息未读数缓存
|
||||
func InvalidateUnreadSystem(cache Cache, userID string) {
|
||||
cache.Delete(UnreadSystemKey(userID))
|
||||
}
|
||||
|
||||
// InvalidateUnreadConversation 失效会话未读数缓存
|
||||
func InvalidateUnreadConversation(cache Cache, userID string) {
|
||||
cache.Delete(UnreadConversationKey(userID))
|
||||
}
|
||||
|
||||
// InvalidateUnreadDetail 失效单个会话未读数缓存
|
||||
func InvalidateUnreadDetail(cache Cache, userID, conversationID string) {
|
||||
cache.Delete(UnreadDetailKey(userID, conversationID))
|
||||
}
|
||||
|
||||
// InvalidateUserInfo 失效用户信息缓存
|
||||
func InvalidateUserInfo(cache Cache, userID string) {
|
||||
cache.Delete(UserInfoKey(userID))
|
||||
cache.Delete(UserMeKey(userID))
|
||||
}
|
||||
Reference in New Issue
Block a user