save caches
This commit is contained in:
parent
b870befa91
commit
e2f2442aa1
@ -7,13 +7,20 @@ import (
|
||||
"mt"
|
||||
"q5"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type FriendsMgr struct {
|
||||
cs.MsgHandlerImpl
|
||||
users map[string]*User
|
||||
friendships map[string][]*Friendship
|
||||
pendingReqs map[string]map[string]bool
|
||||
users map[string]*User
|
||||
searchCaches map[string]SearchCache
|
||||
friendships map[string][]*Friendship
|
||||
pendingReqs map[string]map[string]bool
|
||||
}
|
||||
|
||||
type SearchCache struct {
|
||||
Users []*User
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
var gameDBStore *q5.Mysql
|
||||
@ -44,6 +51,7 @@ func (fm *FriendsMgr) init() {
|
||||
fm.loadFriendshipsFromDB(friendDBStore)
|
||||
// 加载等待验证好友请求 列表
|
||||
fm.loadPendingRequestsFromDB(friendDBStore)
|
||||
fm.searchCaches = make(map[string]SearchCache)
|
||||
}
|
||||
|
||||
func (fm *FriendsMgr) unInit() {
|
||||
@ -52,8 +60,12 @@ func (fm *FriendsMgr) unInit() {
|
||||
}
|
||||
|
||||
func (fm *FriendsMgr) searchFriends(searchKeyword string) []*User {
|
||||
// By default, Search result save to caches..., key: search keyword, value: serial(search result),
|
||||
// And,cache expired: 2days
|
||||
if cachedResult, ok := fm.searchCaches[searchKeyword]; ok {
|
||||
if time.Since(cachedResult.LastModified) <= 10*time.Hour {
|
||||
return cachedResult.Users
|
||||
}
|
||||
}
|
||||
|
||||
listFriend := make([]*User, 10)
|
||||
lowercaseQuery := strings.ToLower(searchKeyword)
|
||||
for _, u := range fm.users {
|
||||
@ -65,6 +77,14 @@ func (fm *FriendsMgr) searchFriends(searchKeyword string) []*User {
|
||||
listFriend = append(listFriend, uEntity)
|
||||
}
|
||||
}
|
||||
|
||||
// Add to caches
|
||||
fm.searchCaches[searchKeyword] = SearchCache{
|
||||
Users: listFriend,
|
||||
LastModified: time.Now(),
|
||||
}
|
||||
fm.clearExpiredCaches()
|
||||
|
||||
return listFriend
|
||||
}
|
||||
|
||||
@ -230,6 +250,15 @@ func (fm *FriendsMgr) addFriendshipToMap(accountID string, friendship *Friendshi
|
||||
}
|
||||
}
|
||||
|
||||
func (fm *FriendsMgr) RegisterUser(accountId string, username string) {
|
||||
func (fm *FriendsMgr) registerUser(accountId string, username string) {
|
||||
fm.users[accountId] = &User{AccountId: accountId, Username: username}
|
||||
}
|
||||
|
||||
func (fm *FriendsMgr) clearExpiredCaches() {
|
||||
expirationTime := time.Now().Add(-24 * time.Hour)
|
||||
for key, entry := range fm.searchCaches {
|
||||
if entry.LastModified.Before(expirationTime) {
|
||||
delete(fm.searchCaches, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user