mirror of https://github.com/rocboss/paopao-ce
commit
8faa135eed
@ -0,0 +1,30 @@
|
||||
// Copyright 2023 ROC. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package conf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/alimy/tryst/cache"
|
||||
)
|
||||
|
||||
const (
|
||||
_defaultKeyPoolSize = 128
|
||||
)
|
||||
|
||||
// 以下包含一些在cache中会用到的池化后的key
|
||||
var (
|
||||
KeyUnreadMsg cache.KeyPool[int64]
|
||||
)
|
||||
|
||||
func initCacheKeyPool() {
|
||||
poolSize := _defaultKeyPoolSize
|
||||
if poolSize < CacheSetting.KeyPoolSize {
|
||||
poolSize = CacheSetting.KeyPoolSize
|
||||
}
|
||||
KeyUnreadMsg = cache.MustKeyPool[int64](poolSize, func(key int64) string {
|
||||
return fmt.Sprintf("paopao:unreadmsg:%d", key)
|
||||
})
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
// Copyright 2023 ROC. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/redis/rueidis"
|
||||
"github.com/rocboss/paopao-ce/internal/conf"
|
||||
"github.com/rocboss/paopao-ce/internal/core"
|
||||
"github.com/rocboss/paopao-ce/pkg/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
_webCache core.WebCache = (*redisWebCache)(nil)
|
||||
)
|
||||
|
||||
type redisWebCache struct {
|
||||
cscExpire time.Duration
|
||||
unreadMsgExpire int64
|
||||
c rueidis.Client
|
||||
}
|
||||
|
||||
func (s *redisWebCache) Name() string {
|
||||
return "RedisWebCache"
|
||||
}
|
||||
|
||||
func (s *redisWebCache) Version() *semver.Version {
|
||||
return semver.MustParse("v0.1.0")
|
||||
}
|
||||
|
||||
func (s *redisWebCache) GetUnreadMsgCountResp(uid int64) ([]byte, error) {
|
||||
key := conf.KeyUnreadMsg.Get(uid)
|
||||
res, err := rueidis.MGetCache(s.c, context.Background(), s.cscExpire, []string{key})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
message := res[key]
|
||||
return message.AsBytes()
|
||||
}
|
||||
|
||||
func (s *redisWebCache) PutUnreadMsgCountResp(uid int64, data []byte) error {
|
||||
return s.c.Do(context.Background(), s.c.B().Set().
|
||||
Key(conf.KeyUnreadMsg.Get(uid)).
|
||||
Value(utils.String(data)).
|
||||
ExSeconds(s.unreadMsgExpire).
|
||||
Build()).
|
||||
Error()
|
||||
}
|
||||
|
||||
func (s *redisWebCache) DelUnreadMsgCountResp(uid int64) error {
|
||||
return s.c.Do(context.Background(), s.c.B().Del().Key(conf.KeyUnreadMsg.Get(uid)).Build()).Error()
|
||||
}
|
||||
|
||||
func newWebCache() *redisWebCache {
|
||||
s := conf.CacheSetting
|
||||
return &redisWebCache{
|
||||
cscExpire: s.CientSideCacheExpire,
|
||||
unreadMsgExpire: s.UnreadMsgExpire,
|
||||
c: conf.MustRedisClient(),
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
// Copyright 2023 ROC. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package joint
|
||||
|
||||
import (
|
||||
stdJson "encoding/json"
|
||||
|
||||
"github.com/rocboss/paopao-ce/pkg/json"
|
||||
)
|
||||
|
||||
func RespMarshal(data any) (stdJson.RawMessage, error) {
|
||||
return json.Marshal(data)
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
// Copyright 2023 ROC. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/alimy/tryst/event"
|
||||
"github.com/rocboss/paopao-ce/internal/core"
|
||||
"github.com/rocboss/paopao-ce/internal/core/ms"
|
||||
"github.com/rocboss/paopao-ce/internal/events"
|
||||
"github.com/rocboss/paopao-ce/internal/model/joint"
|
||||
"github.com/rocboss/paopao-ce/internal/model/web"
|
||||
)
|
||||
|
||||
type cacheUnreadMsgEvent struct {
|
||||
event.UnimplementedEvent
|
||||
ds core.DataService
|
||||
wc core.WebCache
|
||||
uid int64
|
||||
}
|
||||
|
||||
type createMessageEvent struct {
|
||||
event.UnimplementedEvent
|
||||
ds core.DataService
|
||||
wc core.WebCache
|
||||
message *ms.Message
|
||||
}
|
||||
|
||||
func onCacheUnreadMsgEvent(uid int64) {
|
||||
events.OnEvent(&cacheUnreadMsgEvent{
|
||||
ds: _ds,
|
||||
wc: _wc,
|
||||
uid: uid,
|
||||
})
|
||||
}
|
||||
|
||||
func onCreateMessageEvent(data *ms.Message) {
|
||||
events.OnEvent(&createMessageEvent{
|
||||
ds: _ds,
|
||||
wc: _wc,
|
||||
message: data,
|
||||
})
|
||||
}
|
||||
|
||||
func (e *cacheUnreadMsgEvent) Name() string {
|
||||
return "cacheUnreadMsgEvent"
|
||||
}
|
||||
|
||||
func (e *cacheUnreadMsgEvent) Action() error {
|
||||
count, err := e.ds.GetUnreadCount(e.uid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cacheUnreadMsgEvent action occurs error: %w", err)
|
||||
}
|
||||
resp := &joint.JsonResp{
|
||||
Code: 0,
|
||||
Msg: "success",
|
||||
Data: &web.GetUnreadMsgCountResp{
|
||||
Count: count,
|
||||
},
|
||||
}
|
||||
data, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cacheUnreadMsgEvent action marshal resp occurs error: %w", err)
|
||||
}
|
||||
if err = e.wc.PutUnreadMsgCountResp(e.uid, data); err != nil {
|
||||
return fmt.Errorf("cacheUnreadMsgEvent action put resp data to redis cache occurs error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *createMessageEvent) Name() string {
|
||||
return "createMessageEvent"
|
||||
}
|
||||
|
||||
func (e *createMessageEvent) Action() (err error) {
|
||||
if _, err = e.ds.CreateMessage(e.message); err == nil {
|
||||
err = e.wc.DelUnreadMsgCountResp(e.message.ReceiverUserID)
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Reference in new issue