feat: community RPC 从内存存储迁移到 ent 数据库
This commit is contained in:
@@ -1,15 +1,54 @@
|
||||
package svc
|
||||
|
||||
import "juwan-backend/app/community/rpc/internal/config"
|
||||
import (
|
||||
stdsql "database/sql"
|
||||
"juwan-backend/app/community/rpc/internal/config"
|
||||
"juwan-backend/app/community/rpc/internal/models"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
"juwan-backend/common/redisx"
|
||||
"juwan-backend/common/snowflakex"
|
||||
"juwan-backend/pkg/adapter"
|
||||
"time"
|
||||
|
||||
"ariga.io/entcache"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
Store *CommunityStore
|
||||
Config config.Config
|
||||
Snowflake snowflake.SnowflakeServiceClient
|
||||
CommunityModelRW *models.Client
|
||||
CommunityModelRO *models.Client
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
rawRW, err := stdsql.Open("pgx", c.DB.Master)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
rawRO, err := stdsql.Open("pgx", c.DB.Slaves)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
RWConn := sql.OpenDB(dialect.Postgres, rawRW)
|
||||
ROConn := sql.OpenDB(dialect.Postgres, rawRO)
|
||||
|
||||
redisCluster, err := redisx.ConnectMasterSlaveCluster(c.CacheConf, 5*time.Second)
|
||||
if redisCluster == nil || err != nil {
|
||||
logx.Errorf("failed to connect to Redis cluster: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
RWDrv := entcache.NewDriver(RWConn, entcache.TTL(30*time.Second), entcache.Levels(adapter.NewRedisCache(redisCluster.Client)))
|
||||
RODrv := entcache.NewDriver(ROConn, entcache.TTL(30*time.Second), entcache.Levels(adapter.NewRedisCache(redisCluster.Client)))
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
Store: NewCommunityStore(),
|
||||
Config: c,
|
||||
CommunityModelRW: models.NewClient(models.Driver(RWDrv)),
|
||||
CommunityModelRO: models.NewClient(models.Driver(RODrv)),
|
||||
Snowflake: snowflakex.NewClient(c.SnowflakeRpcConf),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"juwan-backend/app/community/rpc/pb"
|
||||
)
|
||||
|
||||
type CommunityStore struct {
|
||||
Mu sync.RWMutex
|
||||
|
||||
nextPostID int64
|
||||
nextCommentID int64
|
||||
|
||||
Posts map[int64]*pb.Posts
|
||||
Comments map[int64]*pb.Comments
|
||||
PostLikes map[string]*pb.PostLikes
|
||||
CommentLikes map[string]*pb.CommentLikes
|
||||
}
|
||||
|
||||
func NewCommunityStore() *CommunityStore {
|
||||
return &CommunityStore{
|
||||
nextPostID: 1000,
|
||||
nextCommentID: 1000,
|
||||
Posts: make(map[int64]*pb.Posts),
|
||||
Comments: make(map[int64]*pb.Comments),
|
||||
PostLikes: make(map[string]*pb.PostLikes),
|
||||
CommentLikes: make(map[string]*pb.CommentLikes),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CommunityStore) NextPost() int64 {
|
||||
s.nextPostID++
|
||||
return s.nextPostID
|
||||
}
|
||||
|
||||
func (s *CommunityStore) NextComment() int64 {
|
||||
s.nextCommentID++
|
||||
return s.nextCommentID
|
||||
}
|
||||
Reference in New Issue
Block a user