parent
8e3890ed88
commit
9b043feca7
@ -1,38 +1,30 @@
|
|||||||
// Copyright © 2024 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package cachekey
|
package cachekey
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MaxSeq = "MAX_SEQ:"
|
MallocSeq = "MALLOC_SEQ:"
|
||||||
MinSeq = "MIN_SEQ:"
|
MallocMinSeqLock = "MALLOC_MIN_SEQ:"
|
||||||
ConversationUserMinSeq = "CON_USER_MIN_SEQ:"
|
|
||||||
HasReadSeq = "HAS_READ_SEQ:"
|
SeqUserMaxSeq = "SEQ_USER_MAX:"
|
||||||
|
SeqUserMinSeq = "SEQ_USER_MIN:"
|
||||||
|
SeqUserReadSeq = "SEQ_USER_READ:"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMaxSeqKey(conversationID string) string {
|
func GetMallocSeqKey(conversationID string) string {
|
||||||
return MaxSeq + conversationID
|
return MallocSeq + conversationID
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMallocMinSeqKey(conversationID string) string {
|
||||||
|
return MallocMinSeqLock + conversationID
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMinSeqKey(conversationID string) string {
|
func GetSeqUserMaxSeqKey(conversationID string, userID string) string {
|
||||||
return MinSeq + conversationID
|
return SeqUserMaxSeq + conversationID + ":" + userID
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetHasReadSeqKey(conversationID string, userID string) string {
|
func GetSeqUserMinSeqKey(conversationID string, userID string) string {
|
||||||
return HasReadSeq + userID + ":" + conversationID
|
return SeqUserMinSeq + conversationID + ":" + userID
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConversationUserMinSeqKey(conversationID, userID string) string {
|
func GetSeqUserReadSeqKey(conversationID string, userID string) string {
|
||||||
return ConversationUserMinSeq + conversationID + "u:" + userID
|
return SeqUserReadSeq + conversationID + ":" + userID
|
||||||
}
|
}
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
package cachekey
|
|
||||||
|
|
||||||
const (
|
|
||||||
MallocSeq = "MALLOC_SEQ:"
|
|
||||||
MallocMinSeqLock = "MALLOC_MIN_SEQ:"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetMallocSeqKey(conversationID string) string {
|
|
||||||
return MallocSeq + conversationID
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetMallocMinSeqKey(conversationID string) string {
|
|
||||||
return MallocMinSeqLock + conversationID
|
|
||||||
}
|
|
@ -1,183 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package redis
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
|
||||||
"github.com/openimsdk/tools/errs"
|
|
||||||
"github.com/openimsdk/tools/utils/stringutil"
|
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewSeqCache(rdb redis.UniversalClient) cache.SeqCache {
|
|
||||||
return &seqCache{rdb: rdb}
|
|
||||||
}
|
|
||||||
|
|
||||||
type seqCache struct {
|
|
||||||
rdb redis.UniversalClient
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) getMaxSeqKey(conversationID string) string {
|
|
||||||
return cachekey.GetMaxSeqKey(conversationID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) getMinSeqKey(conversationID string) string {
|
|
||||||
return cachekey.GetMinSeqKey(conversationID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) getHasReadSeqKey(conversationID string, userID string) string {
|
|
||||||
return cachekey.GetHasReadSeqKey(conversationID, userID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) getConversationUserMinSeqKey(conversationID, userID string) string {
|
|
||||||
return cachekey.GetConversationUserMinSeqKey(conversationID, userID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) setSeq(ctx context.Context, conversationID string, seq int64, getkey func(conversationID string) string) error {
|
|
||||||
return errs.Wrap(c.rdb.Set(ctx, getkey(conversationID), seq, 0).Err())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) getSeq(ctx context.Context, conversationID string, getkey func(conversationID string) string) (int64, error) {
|
|
||||||
val, err := c.rdb.Get(ctx, getkey(conversationID)).Int64()
|
|
||||||
if err != nil {
|
|
||||||
return 0, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
return val, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) getSeqs(ctx context.Context, items []string, getkey func(s string) string) (m map[string]int64, err error) {
|
|
||||||
m = make(map[string]int64, len(items))
|
|
||||||
for i, v := range items {
|
|
||||||
res, err := c.rdb.Get(ctx, getkey(v)).Result()
|
|
||||||
if err != nil && err != redis.Nil {
|
|
||||||
return nil, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
val := stringutil.StringToInt64(res)
|
|
||||||
if val != 0 {
|
|
||||||
m[items[i]] = val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//func (c *seqCache) SetMaxSeq(ctx context.Context, conversationID string, maxSeq int64) error {
|
|
||||||
// return c.setSeq(ctx, conversationID, maxSeq, c.getMaxSeqKey)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *seqCache) GetMaxSeqs(ctx context.Context, conversationIDs []string) (m map[string]int64, err error) {
|
|
||||||
// return c.getSeqs(ctx, conversationIDs, c.getMaxSeqKey)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *seqCache) GetMaxSeq(ctx context.Context, conversationID string) (int64, error) {
|
|
||||||
// return c.getSeq(ctx, conversationID, c.getMaxSeqKey)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *seqCache) SetMinSeq(ctx context.Context, conversationID string, minSeq int64) error {
|
|
||||||
// return c.setSeq(ctx, conversationID, minSeq, c.getMinSeqKey)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *seqCache) setSeqs(ctx context.Context, seqs map[string]int64, getkey func(key string) string) error {
|
|
||||||
// for conversationID, seq := range seqs {
|
|
||||||
// if err := c.rdb.Set(ctx, getkey(conversationID), seq, 0).Err(); err != nil {
|
|
||||||
// return errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//func (c *seqCache) GetMinSeqs(ctx context.Context, conversationIDs []string) (map[string]int64, error) {
|
|
||||||
// return c.getSeqs(ctx, conversationIDs, c.getMinSeqKey)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *seqCache) GetMinSeq(ctx context.Context, conversationID string) (int64, error) {
|
|
||||||
// return c.getSeq(ctx, conversationID, c.getMinSeqKey)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *seqCache) SetMinSeqs(ctx context.Context, seqs map[string]int64) error {
|
|
||||||
// return c.setSeqs(ctx, seqs, c.getMinSeqKey)
|
|
||||||
//}
|
|
||||||
|
|
||||||
func (c *seqCache) setSeqs(ctx context.Context, seqs map[string]int64, getkey func(key string) string) error {
|
|
||||||
for conversationID, seq := range seqs {
|
|
||||||
if err := c.rdb.Set(ctx, getkey(conversationID), seq, 0).Err(); err != nil {
|
|
||||||
return errs.Wrap(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) GetConversationUserMinSeq(ctx context.Context, conversationID string, userID string) (int64, error) {
|
|
||||||
val, err := c.rdb.Get(ctx, c.getConversationUserMinSeqKey(conversationID, userID)).Int64()
|
|
||||||
if err != nil {
|
|
||||||
return 0, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
return val, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) GetConversationUserMinSeqs(ctx context.Context, conversationID string, userIDs []string) (m map[string]int64, err error) {
|
|
||||||
return c.getSeqs(ctx, userIDs, func(userID string) string {
|
|
||||||
return c.getConversationUserMinSeqKey(conversationID, userID)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) SetConversationUserMinSeq(ctx context.Context, conversationID string, userID string, minSeq int64) error {
|
|
||||||
return errs.Wrap(c.rdb.Set(ctx, c.getConversationUserMinSeqKey(conversationID, userID), minSeq, 0).Err())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) SetConversationUserMinSeqs(ctx context.Context, conversationID string, seqs map[string]int64) (err error) {
|
|
||||||
return c.setSeqs(ctx, seqs, func(userID string) string {
|
|
||||||
return c.getConversationUserMinSeqKey(conversationID, userID)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) SetUserConversationsMinSeqs(ctx context.Context, userID string, seqs map[string]int64) (err error) {
|
|
||||||
return c.setSeqs(ctx, seqs, func(conversationID string) string {
|
|
||||||
return c.getConversationUserMinSeqKey(conversationID, userID)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) SetHasReadSeq(ctx context.Context, userID string, conversationID string, hasReadSeq int64) error {
|
|
||||||
return errs.Wrap(c.rdb.Set(ctx, c.getHasReadSeqKey(conversationID, userID), hasReadSeq, 0).Err())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) SetHasReadSeqs(ctx context.Context, conversationID string, hasReadSeqs map[string]int64) error {
|
|
||||||
return c.setSeqs(ctx, hasReadSeqs, func(userID string) string {
|
|
||||||
return c.getHasReadSeqKey(conversationID, userID)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) UserSetHasReadSeqs(ctx context.Context, userID string, hasReadSeqs map[string]int64) error {
|
|
||||||
return c.setSeqs(ctx, hasReadSeqs, func(conversationID string) string {
|
|
||||||
return c.getHasReadSeqKey(conversationID, userID)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) GetHasReadSeqs(ctx context.Context, userID string, conversationIDs []string) (map[string]int64, error) {
|
|
||||||
return c.getSeqs(ctx, conversationIDs, func(conversationID string) string {
|
|
||||||
return c.getHasReadSeqKey(conversationID, userID)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *seqCache) GetHasReadSeq(ctx context.Context, userID string, conversationID string) (int64, error) {
|
|
||||||
val, err := c.rdb.Get(ctx, c.getHasReadSeqKey(conversationID, userID)).Int64()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return val, nil
|
|
||||||
}
|
|
@ -0,0 +1,89 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/dtm-labs/rockscache"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||||||
|
"github.com/openimsdk/tools/errs"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewSeqUserCacheRedis(rdb redis.UniversalClient, mgo database.SeqUser) cache.SeqUser {
|
||||||
|
return &seqUserCacheRedis{
|
||||||
|
rdb: rdb,
|
||||||
|
mgo: mgo,
|
||||||
|
readSeqWriteRatio: 100,
|
||||||
|
expireTime: time.Hour * 24 * 7,
|
||||||
|
readExpireTime: time.Hour * 24 * 30,
|
||||||
|
rocks: rockscache.NewClient(rdb, *GetRocksCacheOptions()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type seqUserCacheRedis struct {
|
||||||
|
rdb redis.UniversalClient
|
||||||
|
mgo database.SeqUser
|
||||||
|
rocks *rockscache.Client
|
||||||
|
expireTime time.Duration
|
||||||
|
readExpireTime time.Duration
|
||||||
|
readSeqWriteRatio int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *seqUserCacheRedis) getSeqUserMaxSeqKey(conversationID string, userID string) string {
|
||||||
|
return cachekey.GetSeqUserMaxSeqKey(conversationID, userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *seqUserCacheRedis) getSeqUserMinSeqKey(conversationID string, userID string) string {
|
||||||
|
return cachekey.GetSeqUserMinSeqKey(conversationID, userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *seqUserCacheRedis) getSeqUserReadSeqKey(conversationID string, userID string) string {
|
||||||
|
return cachekey.GetSeqUserReadSeqKey(conversationID, userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *seqUserCacheRedis) GetMaxSeq(ctx context.Context, conversationID string, userID string) (int64, error) {
|
||||||
|
return getCache(ctx, s.rocks, s.getSeqUserMaxSeqKey(conversationID, userID), s.expireTime, func(ctx context.Context) (int64, error) {
|
||||||
|
return s.mgo.GetMaxSeq(ctx, conversationID, userID)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *seqUserCacheRedis) SetMaxSeq(ctx context.Context, conversationID string, userID string, seq int64) error {
|
||||||
|
if err := s.mgo.SetMaxSeq(ctx, conversationID, userID, seq); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return s.rocks.TagAsDeleted2(ctx, s.getSeqUserMaxSeqKey(conversationID, userID))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *seqUserCacheRedis) GetMinSeq(ctx context.Context, conversationID string, userID string) (int64, error) {
|
||||||
|
return getCache(ctx, s.rocks, s.getSeqUserMinSeqKey(conversationID, userID), s.expireTime, func(ctx context.Context) (int64, error) {
|
||||||
|
return s.mgo.GetMaxSeq(ctx, conversationID, userID)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *seqUserCacheRedis) SetMinSeq(ctx context.Context, conversationID string, userID string, seq int64) error {
|
||||||
|
if err := s.mgo.SetMinSeq(ctx, conversationID, userID, seq); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return s.rocks.TagAsDeleted2(ctx, s.getSeqUserMinSeqKey(conversationID, userID))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *seqUserCacheRedis) GetReadSeq(ctx context.Context, conversationID string, userID string) (int64, error) {
|
||||||
|
return getCache(ctx, s.rocks, s.getSeqUserReadSeqKey(conversationID, userID), s.readExpireTime, func(ctx context.Context) (int64, error) {
|
||||||
|
return s.mgo.GetMaxSeq(ctx, conversationID, userID)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *seqUserCacheRedis) SetReadSeq(ctx context.Context, conversationID string, userID string, seq int64) error {
|
||||||
|
if seq%s.readSeqWriteRatio == 0 {
|
||||||
|
if err := s.mgo.SetReadSeq(ctx, conversationID, userID, seq); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := s.rocks.RawSet(ctx, s.getSeqUserReadSeqKey(conversationID, userID), strconv.Itoa(int(seq)), s.readExpireTime); err != nil {
|
||||||
|
return errs.Wrap(err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -1,30 +0,0 @@
|
|||||||
package cache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SeqCache interface {
|
|
||||||
//SetMaxSeq(ctx context.Context, conversationID string, maxSeq int64) error
|
|
||||||
//GetMaxSeqs(ctx context.Context, conversationIDs []string) (map[string]int64, error)
|
|
||||||
//GetMaxSeq(ctx context.Context, conversationID string) (int64, error)
|
|
||||||
//SetMinSeq(ctx context.Context, conversationID string, minSeq int64) error
|
|
||||||
//SetMinSeqs(ctx context.Context, seqs map[string]int64) error
|
|
||||||
//GetMinSeqs(ctx context.Context, conversationIDs []string) (map[string]int64, error)
|
|
||||||
//GetMinSeq(ctx context.Context, conversationID string) (int64, error)
|
|
||||||
GetConversationUserMinSeq(ctx context.Context, conversationID string, userID string) (int64, error)
|
|
||||||
GetConversationUserMinSeqs(ctx context.Context, conversationID string, userIDs []string) (map[string]int64, error)
|
|
||||||
SetConversationUserMinSeq(ctx context.Context, conversationID string, userID string, minSeq int64) error
|
|
||||||
// seqs map: key userID value minSeq
|
|
||||||
SetConversationUserMinSeqs(ctx context.Context, conversationID string, seqs map[string]int64) (err error)
|
|
||||||
// seqs map: key conversationID value minSeq
|
|
||||||
SetUserConversationsMinSeqs(ctx context.Context, userID string, seqs map[string]int64) error
|
|
||||||
// has read seq
|
|
||||||
SetHasReadSeq(ctx context.Context, userID string, conversationID string, hasReadSeq int64) error
|
|
||||||
// k: user, v: seq
|
|
||||||
SetHasReadSeqs(ctx context.Context, conversationID string, hasReadSeqs map[string]int64) error
|
|
||||||
// k: conversation, v :seq
|
|
||||||
UserSetHasReadSeqs(ctx context.Context, userID string, hasReadSeqs map[string]int64) error
|
|
||||||
GetHasReadSeqs(ctx context.Context, userID string, conversationIDs []string) (map[string]int64, error)
|
|
||||||
GetHasReadSeq(ctx context.Context, userID string, conversationID string) (int64, error)
|
|
||||||
}
|
|
@ -0,0 +1,12 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
type SeqUser interface {
|
||||||
|
GetMaxSeq(ctx context.Context, conversationID string, userID string) (int64, error)
|
||||||
|
SetMaxSeq(ctx context.Context, conversationID string, userID string, seq int64) error
|
||||||
|
GetMinSeq(ctx context.Context, conversationID string, userID string) (int64, error)
|
||||||
|
SetMinSeq(ctx context.Context, conversationID string, userID string, seq int64) error
|
||||||
|
GetReadSeq(ctx context.Context, conversationID string, userID string) (int64, error)
|
||||||
|
SetReadSeq(ctx context.Context, conversationID string, userID string, seq int64) error
|
||||||
|
}
|
Loading…
Reference in new issue