Skip to content

Commit a5bb0b2

Browse files
committed
缓存使用新版本
1 parent 1d738d6 commit a5bb0b2

File tree

9 files changed

+130
-131
lines changed

9 files changed

+130
-131
lines changed

database/chat.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ func AddChatMessage(msg *ChatMessage) error {
275275
// 将聊天数据推送到缓存定长队列中去
276276
// @TODO 暂时这样push,但是这样处理不准确,需要做优化. 因为每个响应的是时长不一样,可能导致顺序不是正确的,甚至是断续的. 如 [1,3,2,4,7,6,5,8,9,11,10,19]
277277
cacheKey := cacheKeyFormatLastMessageList(msg.RoomID, msg.SessionType)
278-
err = jcache.Push(GlobCtx, cacheKey, msg)
278+
err = GlobCache.Push(GlobCtx, cacheKey, msg)
279279
if err != nil && err.Error() == "WRONGTYPE Operation against a key holding the wrong kind of value" {
280-
jcache.Del(GlobCtx, cacheKey)
280+
GlobCache.Del(GlobCtx, cacheKey)
281281
}
282282

283283
if err == nil {
284-
jcache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
284+
GlobCache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
285285
}
286286

287287
return nil
@@ -340,13 +340,13 @@ func AddChatMessageTx(msg *ChatMessage) error {
340340

341341
// 将聊天数据推送到缓存定长队列中去
342342
cacheKey := cacheKeyFormatLastMessageList(msg.RoomID, msg.SessionType)
343-
err = jcache.Push(GlobCtx, cacheKey, msg)
343+
err = GlobCache.Push(GlobCtx, cacheKey, msg)
344344
if err != nil {
345-
jcache.Del(GlobCtx, cacheKey)
345+
GlobCache.Del(GlobCtx, cacheKey)
346346
log.Error().Err(err).Msgf("推送消息到缓存列表失败:\n %+v", err)
347347
}
348348
if err == nil {
349-
jcache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
349+
GlobCache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
350350
}
351351

352352
return nil, nil
@@ -494,10 +494,10 @@ func GetLastChatMessageList(roomID string, sessionType int, opts ...*GetOptions)
494494

495495
// 如果有使用缓存,则从缓存中获取
496496
if opt.UseCache() {
497-
exists, _ := jcache.Exists(GlobCtx, cacheKey)
498-
if exists {
497+
exists, _ := GlobCache.Exists(GlobCtx, cacheKey)
498+
if exists > 0 {
499499
var messages []*ChatMessage
500-
err := jcache.RangAndScan(GlobCtx, &messages, cacheKey, defaultLastLimit)
500+
err := GlobCache.RangAndScan(GlobCtx, &messages, cacheKey, 0, defaultLastLimit-1)
501501
if err == nil {
502502
return messages, nil
503503
}
@@ -521,7 +521,7 @@ func GetLastChatMessageList(roomID string, sessionType int, opts ...*GetOptions)
521521
// @todo 设置缓存为找不到记录
522522

523523
cacheKey := cacheKeyFormatLastMessageList(roomID, sessionType)
524-
if err := jcache.SetNX(GlobCtx, cacheKey, nil, jcache.DefaultEmptySetNXDuration); err != nil {
524+
if err := GlobCache.SetNX(GlobCtx, cacheKey, nil, jcache.DefaultEmptySetNXDuration); err != nil {
525525
log.Error().Err(err).Str("cache_key", cacheKey).Msg("缓存写入失败")
526526
}
527527

@@ -543,16 +543,16 @@ func GetLastChatMessageList(roomID string, sessionType int, opts ...*GetOptions)
543543
pushData = append(pushData, msg)
544544
}
545545

546-
err = jcache.Push(GlobCtx, cacheKey, pushData...)
546+
err = GlobCache.Push(GlobCtx, cacheKey, pushData...)
547547
if err != nil && err.Error() == "WRONGTYPE Operation against a key holding the wrong kind of value" {
548-
jcache.Del(GlobCtx, cacheKey)
549-
err = jcache.Push(GlobCtx, cacheKey, pushData...)
548+
GlobCache.Del(GlobCtx, cacheKey)
549+
err = GlobCache.Push(GlobCtx, cacheKey, pushData...)
550550
if err != nil {
551551
log.Warn().Err(err).Msg("缓存插入聊天消息失败")
552552
}
553553
}
554554
if err == nil {
555-
jcache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
555+
GlobCache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
556556
}
557557

558558
// 设置缓存

database/database.go

+20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"github.com/jerbe/jim/config"
99
"github.com/jerbe/jim/errors"
1010

11+
"github.com/jerbe/jcache"
12+
"github.com/jerbe/jcache/driver"
13+
1114
_ "github.com/go-sql-driver/mysql"
1215
"github.com/jmoiron/sqlx"
1316
"github.com/redis/go-redis/v9"
@@ -77,6 +80,8 @@ var (
7780

7881
GlobDB *Database
7982

83+
GlobCache *jcache.Client
84+
8085
initialized bool
8186
)
8287

@@ -87,6 +92,21 @@ func Init(cfg config.Config) (db *Database, err error) {
8792

8893
CacheKeyPrefix = cfg.Main.ServerName
8994

95+
// 初始化缓存
96+
c := driver.RedisConfig{
97+
Mode: cfg.Redis.Mode,
98+
MasterName: cfg.Redis.MasterName,
99+
Addrs: cfg.Redis.Addrs,
100+
Database: cfg.Redis.Database,
101+
Username: cfg.Redis.Username,
102+
Password: cfg.Redis.Password,
103+
}
104+
cache := jcache.NewClient(
105+
driver.NewMemory(),
106+
driver.NewRedis(driver.RedisOptions().Config(&c)))
107+
108+
GlobCache = cache
109+
90110
// 初始化MYSQL
91111
mysqlCfg := cfg.MySQL
92112
mysqlConnStr := mysqlCfg.URI

database/group.go

+32-32
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func AddGroup(group *Group, opts ...*SetOptions) (int64, error) {
8686
group.ID = insertId
8787

8888
if opt.UpdateCache() {
89-
jcache.Set(GlobCtx, cacheKeyFormatGroupID(insertId), group, jcache.DefaultExpirationDuration)
89+
GlobCache.Set(GlobCtx, cacheKeyFormatGroupID(insertId), group, jcache.DefaultExpirationDuration)
9090
}
9191

9292
return insertId, nil
@@ -99,7 +99,7 @@ func GetGroup(id int64, opts ...*GetOptions) (*Group, error) {
9999
if opt.UseCache() {
100100
cacheKey := cacheKeyFormatGroupID(id)
101101
group := new(Group)
102-
err := jcache.CheckAndScan(GlobCtx, group, cacheKey)
102+
err := GlobCache.CheckAndScan(GlobCtx, group, cacheKey)
103103
if err == nil {
104104
return group, nil
105105
}
@@ -118,7 +118,7 @@ func GetGroup(id int64, opts ...*GetOptions) (*Group, error) {
118118
if errors.IsNoRecord(err) {
119119
// 写入缓存,如果key不存在的话
120120
var cacheKey = cacheKeyFormatGroupID(id)
121-
if e := jcache.SetNX(GlobCtx, cacheKey, nil, jcache.DefaultEmptySetNXDuration); e != nil {
121+
if e := GlobCache.SetNX(GlobCtx, cacheKey, nil, jcache.DefaultEmptySetNXDuration); e != nil {
122122
log.Error().Err(e).Str("err_format", fmt.Sprintf("%+v", e)).Str("cache_key", cacheKey).Msg("缓存写入失败")
123123
}
124124
}
@@ -127,7 +127,7 @@ func GetGroup(id int64, opts ...*GetOptions) (*Group, error) {
127127

128128
if opt.UpdateCache() {
129129
cacheKey := cacheKeyFormatGroupID(id)
130-
jcache.Set(GlobCtx, cacheKey, group, jcache.RandomExpirationDuration())
130+
GlobCache.Set(GlobCtx, cacheKey, group, jcache.RandomExpirationDuration())
131131
}
132132

133133
return group, nil
@@ -193,7 +193,7 @@ func UpdateGroup(groupID int64, data *UpdateGroupData, opts ...*SetOptions) erro
193193

194194
defer func() {
195195
if opt.UpdateCache() {
196-
jcache.Del(GlobCtx, cacheKeyFormatGroupID(groupID))
196+
GlobCache.Del(GlobCtx, cacheKeyFormatGroupID(groupID))
197197
}
198198
}()
199199

@@ -288,7 +288,7 @@ func AddGroupMembers(groupID int64, data *AddGroupMembersData, opts ...*SetOptio
288288
return 0, errors.Wrap(err)
289289
}
290290
if opt.UpdateCache() {
291-
jcache.Del(GlobCtx, cacheKeyFormatGroupMembers(groupID))
291+
GlobCache.Del(GlobCtx, cacheKeyFormatGroupMembers(groupID))
292292
}
293293

294294
rowsAffected, err := rs.RowsAffected()
@@ -300,9 +300,9 @@ func GetGroupMemberCount(groupID int64, opts ...*GetOptions) (int64, error) {
300300
opt := MergeGetOptions(opts)
301301
if opt.UseCache() {
302302
cacheKey := cacheKeyFormatGroupMembers(groupID)
303-
exists, _ := jcache.Exists(GlobCtx, cacheKey)
304-
if exists {
305-
cnt, err := jcache.HLen(GlobCtx, cacheKey)
303+
exists, _ := GlobCache.Exists(GlobCtx, cacheKey)
304+
if exists > 0 {
305+
cnt, err := GlobCache.HLen(GlobCtx, cacheKey)
306306
if err == nil {
307307
return cnt, nil
308308
}
@@ -324,9 +324,9 @@ func GetGroupMemberIDs(groupID int64, opts ...*GetOptions) ([]int64, error) {
324324
var userIDs []int64
325325
if opt.UseCache() {
326326
cacheKey := cacheKeyFormatGroupMembers(groupID)
327-
exists, _ := jcache.Exists(GlobCtx, cacheKey)
328-
if exists {
329-
err := jcache.HKeysAndScan(GlobCtx, cacheKey, userIDs)
327+
exists, _ := GlobCache.Exists(GlobCtx, cacheKey)
328+
if exists > 0 {
329+
err := GlobCache.HKeysAndScan(GlobCtx, userIDs, cacheKey)
330330
if err == nil {
331331
return userIDs, nil
332332
}
@@ -359,9 +359,9 @@ func GetGroupMemberIDsString(groupID int64, opts ...*GetOptions) ([]string, erro
359359
var userIDs []string
360360
if opt.UseCache() {
361361
cacheKey := cacheKeyFormatGroupMembers(groupID)
362-
exists, _ := jcache.Exists(GlobCtx, cacheKey)
363-
if exists {
364-
keys, err := jcache.HKeys(GlobCtx, cacheKey)
362+
exists, _ := GlobCache.Exists(GlobCtx, cacheKey)
363+
if exists > 0 {
364+
keys, err := GlobCache.HKeys(GlobCtx, cacheKey)
365365
if err == nil {
366366
return keys, nil
367367
}
@@ -433,7 +433,7 @@ func RemoveGroupMembers(filter *RemoveGroupMembersFilter, opts ...*SetOptions) (
433433
for i := 0; i < len(membersIDs); i++ {
434434
keys = append(keys, strconv.FormatInt(membersIDs[i], 10))
435435
}
436-
jcache.HDel(GlobCtx, cacheKeyFormatGroupMembers(groupID), keys...)
436+
GlobCache.HDel(GlobCtx, cacheKeyFormatGroupMembers(groupID), keys...)
437437
}
438438

439439
rowsAffected, err := rs.RowsAffected()
@@ -446,9 +446,9 @@ func GetGroupAllMembers(groupID int64, opts ...*GetOptions) ([]*GroupMember, err
446446
gms := make([]*GroupMember, 0)
447447
if opt.UseCache() {
448448
cacheKey := cacheKeyFormatGroupMembers(groupID)
449-
exists, _ := jcache.Exists(GlobCtx, cacheKey)
450-
if exists {
451-
err := jcache.HValsScan(GlobCtx, gms, cacheKeyFormatGroupMembers(groupID))
449+
exists, _ := GlobCache.Exists(GlobCtx, cacheKey)
450+
if exists > 0 {
451+
err := GlobCache.HValsAndScan(GlobCtx, gms, cacheKeyFormatGroupMembers(groupID))
452452
if err == nil {
453453
return gms, nil
454454
}
@@ -475,8 +475,8 @@ func GetGroupAllMembers(groupID int64, opts ...*GetOptions) ([]*GroupMember, err
475475
m[strconv.FormatInt(gms[i].UserID, 10)] = gms[i]
476476
}
477477

478-
jcache.HSet(GlobCtx, cacheKey, m)
479-
jcache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
478+
GlobCache.HSet(GlobCtx, cacheKey, m)
479+
GlobCache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
480480
}
481481

482482
return gms, nil
@@ -488,14 +488,14 @@ func GetGroupMembers(groupID int64, memberIDs []int64, opts ...*GetOptions) ([]*
488488
gms := make([]*GroupMember, 0)
489489
if opt.UseCache() {
490490
cacheKey := cacheKeyFormatGroupMembers(groupID)
491-
exists, _ := jcache.Exists(GlobCtx, cacheKey)
492-
if exists {
491+
exists, _ := GlobCache.Exists(GlobCtx, cacheKey)
492+
if exists > 0 {
493493
var memberKeys = make([]string, len(memberIDs))
494494
for i := 0; i < len(memberIDs); i++ {
495495
memberKeys[i] = fmt.Sprintf("%d", memberIDs[i])
496496
}
497497

498-
err := jcache.HMGetAndScan(GlobCtx, gms, cacheKeyFormatGroupMembers(groupID), memberKeys...)
498+
err := GlobCache.HMGetAndScan(GlobCtx, gms, cacheKeyFormatGroupMembers(groupID), memberKeys...)
499499
if err == nil && len(gms) == len(memberIDs) {
500500
return gms, nil
501501
}
@@ -526,8 +526,8 @@ func GetGroupMembers(groupID int64, memberIDs []int64, opts ...*GetOptions) ([]*
526526
m[strconv.FormatInt(gms[i].UserID, 10)] = gms[i]
527527
}
528528

529-
jcache.HSet(GlobCtx, cacheKey, m)
530-
jcache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
529+
GlobCache.HSet(GlobCtx, cacheKey, m)
530+
GlobCache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
531531
}
532532

533533
return gms, nil
@@ -539,9 +539,9 @@ func GetGroupMember(groupID, memberID int64, opts ...*GetOptions) (*GroupMember,
539539
member := new(GroupMember)
540540
if opt.UseCache() {
541541
cacheKey := cacheKeyFormatGroupMembers(groupID)
542-
exists, _ := jcache.Exists(GlobCtx, cacheKey)
543-
if exists {
544-
err := jcache.HGetAndScan(GlobCtx, member, cacheKeyFormatGroupMembers(groupID), fmt.Sprintf("%d", memberID))
542+
exists, _ := GlobCache.Exists(GlobCtx, cacheKey)
543+
if exists > 0 {
544+
err := GlobCache.HGetAndScan(GlobCtx, member, cacheKeyFormatGroupMembers(groupID), fmt.Sprintf("%d", memberID))
545545
if err == nil {
546546
return member, nil
547547
}
@@ -558,8 +558,8 @@ func GetGroupMember(groupID, memberID int64, opts ...*GetOptions) (*GroupMember,
558558
if opt.UpdateCache() {
559559
cacheKey := cacheKeyFormatGroupMembers(groupID)
560560

561-
if ok, _ := jcache.Exists(GlobCtx, cacheKey); ok {
562-
jcache.HSet(GlobCtx, cacheKey, fmt.Sprintf("%d", memberID), member)
561+
if exists, _ := GlobCache.Exists(GlobCtx, cacheKey); exists > 0 {
562+
GlobCache.HSet(GlobCtx, cacheKey, fmt.Sprintf("%d", memberID), member)
563563
}
564564
}
565565

@@ -616,7 +616,7 @@ func UpdateGroupMember(groupID, userID int64, data *UpdateGroupMemberData, opts
616616

617617
if opt.UpdateCache() {
618618
cacheKey := cacheKeyFormatGroupMembers(groupID)
619-
jcache.Del(GlobCtx, cacheKey)
619+
GlobCache.Del(GlobCtx, cacheKey)
620620
}
621621
return nil
622622
}

0 commit comments

Comments
 (0)