Files
open-im-server/pkg/common/db/cache/extend_msg_set.go
T

65 lines
2.1 KiB
Go
Raw Normal View History

2023-02-07 18:43:43 +08:00
package cache
import (
2023-02-09 14:40:49 +08:00
"context"
2023-03-23 19:02:20 +08:00
"time"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
2023-02-07 18:43:43 +08:00
"github.com/dtm-labs/rockscache"
2023-03-23 19:02:20 +08:00
"github.com/go-redis/redis/v8"
2023-02-07 18:43:43 +08:00
)
2023-02-10 15:46:29 +08:00
const (
extendMsgSetCache = "EXTEND_MSG_SET_CACHE:"
extendMsgCache = "EXTEND_MSG_CACHE:"
)
2023-03-23 19:02:20 +08:00
type ExtendMsgSetCache interface {
metaCache
NewCache() ExtendMsgSetCache
GetExtendMsg(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, firstModifyTime int64) (extendMsg *unrelation.ExtendMsgModel, err error)
DelExtendMsg(clientMsgID string) ExtendMsgSetCache
}
type ExtendMsgSetCacheRedis struct {
metaCache
expireTime time.Duration
rcClient *rockscache.Client
extendMsgSetDB unrelation.ExtendMsgSetModelInterface
2023-02-07 18:43:43 +08:00
}
2023-02-09 14:40:49 +08:00
2023-03-23 19:02:20 +08:00
func NewExtendMsgSetCacheRedis(rdb redis.UniversalClient, extendMsgSetDB unrelation.ExtendMsgSetModelInterface, options rockscache.Options) ExtendMsgSetCache {
rcClient := rockscache.NewClient(rdb, options)
return &ExtendMsgSetCacheRedis{
metaCache: NewMetaCacheRedis(rcClient),
expireTime: time.Second * 30 * 60,
extendMsgSetDB: extendMsgSetDB,
rcClient: rcClient,
}
}
func (e *ExtendMsgSetCacheRedis) NewCache() ExtendMsgSetCache {
return &ExtendMsgSetCacheRedis{
2023-03-27 17:20:36 +08:00
metaCache: NewMetaCacheRedis(e.rcClient, e.metaCache.GetPreDelKeys()...),
2023-03-23 19:02:20 +08:00
expireTime: e.expireTime,
extendMsgSetDB: e.extendMsgSetDB,
rcClient: e.rcClient,
}
}
func (e *ExtendMsgSetCacheRedis) getKey(clientMsgID string) string {
2023-02-20 10:49:39 +08:00
return extendMsgCache + clientMsgID
}
2023-03-23 19:02:20 +08:00
func (e *ExtendMsgSetCacheRedis) GetExtendMsg(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, firstModifyTime int64) (extendMsg *unrelation.ExtendMsgModel, err error) {
return getCache(ctx, e.rcClient, e.getKey(clientMsgID), e.expireTime, func(ctx context.Context) (*unrelation.ExtendMsgModel, error) {
return e.extendMsgSetDB.TakeExtendMsg(ctx, sourceID, sessionType, clientMsgID, firstModifyTime)
2023-02-20 10:49:39 +08:00
})
2023-02-09 14:40:49 +08:00
}
2023-03-23 19:02:20 +08:00
func (e *ExtendMsgSetCacheRedis) DelExtendMsg(clientMsgID string) ExtendMsgSetCache {
new := e.NewCache()
new.AddKeys(e.getKey(clientMsgID))
return new
2023-02-09 14:40:49 +08:00
}