1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

fix private msg cache.

This commit is contained in:
Mrs4s 2021-03-01 09:20:42 +08:00
parent 8d5283edfc
commit 331dd98a7c
2 changed files with 15 additions and 1 deletions

View File

@ -49,7 +49,7 @@ func (c *QQClient) c2cMessageSyncProcessor(rsp *msg.GetMessageResponse) {
continue continue
} }
strKey := fmt.Sprintf("%d%d%d%d", pMsg.Head.GetFromUin(), pMsg.Head.GetToUin(), pMsg.Head.GetMsgSeq(), pMsg.Head.GetMsgUid()) strKey := fmt.Sprintf("%d%d%d%d", pMsg.Head.GetFromUin(), pMsg.Head.GetToUin(), pMsg.Head.GetMsgSeq(), pMsg.Head.GetMsgUid())
if _, ok := c.msgSvcCache.Get(strKey); ok { if _, ok := c.msgSvcCache.GetAndUpdate(strKey, time.Minute*5); ok {
continue continue
} }
c.msgSvcCache.Add(strKey, "", time.Minute) c.msgSvcCache.Add(strKey, "", time.Minute)

View File

@ -72,6 +72,20 @@ func (cache *Cache) Get(key string) (interface{}, bool) {
return nil, false return nil, false
} }
func (cache *Cache) GetAndUpdate(key string, ttl time.Duration) (interface{}, bool) {
cache.lock.RLock()
defer cache.lock.RUnlock()
e, ok := cache.cache[key]
if ok && e.expiry != nil {
expiry := time.Now().Add(ttl)
e.expiry = &expiry
return e.value, true
}
return nil, false
}
// Add - add key/value in cache // Add - add key/value in cache
func (cache *Cache) Add(key string, value interface{}, ttl time.Duration) { func (cache *Cache) Add(key string, value interface{}, ttl time.Duration) {
cache.lock.Lock() cache.lock.Lock()