diff --git a/client/c2c_processor.go b/client/c2c_processor.go index 319e61ae..879125ba 100644 --- a/client/c2c_processor.go +++ b/client/c2c_processor.go @@ -49,7 +49,7 @@ func (c *QQClient) c2cMessageSyncProcessor(rsp *msg.GetMessageResponse) { continue } 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 } c.msgSvcCache.Add(strKey, "", time.Minute) diff --git a/utils/ttl.go b/utils/ttl.go index ad33332f..fed888b7 100644 --- a/utils/ttl.go +++ b/utils/ttl.go @@ -72,6 +72,20 @@ func (cache *Cache) Get(key string) (interface{}, bool) { 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 func (cache *Cache) Add(key string, value interface{}, ttl time.Duration) { cache.lock.Lock()