Commit 5dfde887 authored by zhengyaoqiu's avatar zhengyaoqiu

feat(colorway): 代码优化

parent 6523c96a
......@@ -12,9 +12,9 @@ import (
)
type Cache[T any] interface {
Filter(data ChangeStreamData) bson.M
ChangeStreamFilter(data ChangeStreamData) bson.M
DocumentEqual(a, b T) bool
GetAllDocuments() []T
GetCacheDocuments() []T
}
type Base[T any] struct {
......@@ -48,13 +48,13 @@ func (receiver *Base[T]) StartCache(ctx context.Context, cache Cache[T]) error {
log.Println(err)
continue
}
filter := cache.Filter(changeStreamData)
filter := cache.ChangeStreamFilter(changeStreamData)
document, err := receiver.baseModel.FindOne(ctx, filter)
if err != nil {
log.Println(err)
continue
}
documents := receiver.GetAllDocuments()
documents := receiver.GetCacheDocuments()
for i := range documents {
if cache.DocumentEqual(documents[i], document) {
documents[i] = document
......@@ -72,7 +72,7 @@ func (receiver *Base[T]) StartCache(ctx context.Context, cache Cache[T]) error {
return nil
}
func (receiver *Base[T]) GetAllDocuments() []T {
func (receiver *Base[T]) GetCacheDocuments() []T {
receiver.rwMutex.RLock()
defer receiver.rwMutex.RUnlock()
tmp := make([]T, len(receiver.documents))
......@@ -86,7 +86,7 @@ func (receiver *Base[T]) setAllDocuments(documents []T) {
receiver.documents = documents
}
func (receiver *Base[T]) Filter(data ChangeStreamData) bson.M {
func (receiver *Base[T]) ChangeStreamFilter(data ChangeStreamData) bson.M {
id, err := primitive.ObjectIDFromHex(data.DocumentKey.Id.Oid)
if err != nil {
return nil
......
package mongo
import productModel "git.chillcy.com/golang/colorway/internal/pkg/database/mongo/model/product"
import (
productModel "git.chillcy.com/golang/colorway/internal/pkg/database/mongo/model/product"
sitePlatformPriceConfigModel "git.chillcy.com/golang/colorway/internal/pkg/database/mongo/model/site_platform_price_config"
)
const DatabaseColorWay = "color_way"
type Manager struct {
ProductModel productModel.ProductModel
SitePlatformPriceConfigModel sitePlatformPriceConfigModel.SitePlatformPriceConfigModel
}
func NewManager(mongoUrl string) *Manager {
return &Manager{
ProductModel: productModel.NewProductModel(mongoUrl, DatabaseColorWay, productModel.Collection),
SitePlatformPriceConfigModel: sitePlatformPriceConfigModel.NewSitePlatformPriceConfigModel(mongoUrl, DatabaseColorWay, sitePlatformPriceConfigModel.Collection),
}
}
......@@ -8,6 +8,8 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)
const Collection = "site_platform_price_config"
type SitePlatformPriceConfig struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Site string `json:"site" bson:"site"`
......
......@@ -3,7 +3,7 @@ package convert
import (
"git.chillcy.com/golang/chillcy/pkg/slice"
"git.chillcy.com/golang/chillcy/project/pc4/pkg/rpc/pb/pc4"
model "git.chillcy.com/golang/colorway/internal/pkg/database/mongo/product"
model "git.chillcy.com/golang/colorway/internal/pkg/database/mongo/model/product"
"git.chillcy.com/golang/colorway/internal/rpc/internal/svc/cache"
"sync"
)
......
......@@ -5,15 +5,15 @@ import (
"flag"
"git.chillcy.com/golang/chillcy/pkg/etcd"
"git.chillcy.com/golang/chillcy/project/pc4/pkg/rpc/client/category"
"git.chillcy.com/golang/colorway/internal/pkg/database"
"git.chillcy.com/golang/colorway/internal/pkg/database/model"
"git.chillcy.com/golang/colorway/internal/pkg/database/mysql"
"git.chillcy.com/golang/colorway/internal/pkg/database/mysql/model"
"github.com/zeromicro/go-zero/zrpc"
"log"
)
var _env = flag.String("env", "dev", "环境配置")
var _categoryService category.Category
var _databaseManager *database.Manager
var _databaseManager *mysql.Manager
func init() {
flag.Parse()
......@@ -26,7 +26,7 @@ func init() {
// todo dsn etcd
dsn := "root:root@tcp(127.0.0.1:3306)/colorway?charset=utf8mb4&collation=utf8mb4_unicode_ci&parseTime=true&loc=Local"
var err error
_databaseManager, err = database.NewManager(dsn)
_databaseManager, err = mysql.NewManager(dsn)
if err != nil {
panic(err)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment