Commit 6300183e authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

update supported chats cache

parent 873e75af
...@@ -3,6 +3,8 @@ package dao ...@@ -3,6 +3,8 @@ package dao
import ( import (
"fmt" "fmt"
dbModel "sdk_api/model/db" dbModel "sdk_api/model/db"
"sync"
"time"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
...@@ -59,7 +61,19 @@ func (d *Dao) UserLeft(userId, chatId int) (err error) { ...@@ -59,7 +61,19 @@ func (d *Dao) UserLeft(userId, chatId int) (err error) {
Update("left_at", gorm.Expr("NOW()")).Error Update("left_at", gorm.Expr("NOW()")).Error
} }
var (
supportedChatsCache = map[int]time.Time{}
supportedChatsCacheLock = sync.RWMutex{}
)
func (d *Dao) IsSupportedChat(chatId int) (exist bool, err error) { func (d *Dao) IsSupportedChat(chatId int) (exist bool, err error) {
supportedChatsCacheLock.RLock()
expired, ok := supportedChatsCache[chatId]
supportedChatsCacheLock.RUnlock()
if ok && expired.After(time.Now()) {
return true, nil
}
a := dbModel.ChatGroup{} a := dbModel.ChatGroup{}
err = d.db.Model(&dbModel.ChatGroup{}). err = d.db.Model(&dbModel.ChatGroup{}).
Where("chat_id = ?", chatId). Where("chat_id = ?", chatId).
...@@ -67,7 +81,15 @@ func (d *Dao) IsSupportedChat(chatId int) (exist bool, err error) { ...@@ -67,7 +81,15 @@ func (d *Dao) IsSupportedChat(chatId int) (exist bool, err error) {
if err == gorm.ErrRecordNotFound { if err == gorm.ErrRecordNotFound {
return false, nil return false, nil
} }
return err == nil, err
if err != nil {
return false, err
}
supportedChatsCacheLock.Lock()
supportedChatsCache[chatId] = time.Now().Add(1 * time.Hour)
supportedChatsCacheLock.Unlock()
return true, err
} }
func (d *Dao) addTestGroup() (err error) { func (d *Dao) addTestGroup() (err error) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment