Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
colorway
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
golang
colorway
Commits
83fdf144
Commit
83fdf144
authored
Jul 03, 2025
by
zhengyaoqiu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(colorway): 代码优化
parent
5dfde887
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
13 deletions
+29
-13
cache.go
internal/pkg/database/mongo/cache/cache.go
+16
-11
manager.go
internal/pkg/database/mongo/manager.go
+9
-0
site_platform_price_config_model.go
...platform_price_config/site_platform_price_config_model.go
+4
-2
No files found.
internal/pkg/database/mongo/cache/cache.go
View file @
83fdf144
...
...
@@ -11,23 +11,28 @@ import (
"sync"
)
type
Cache
[
T
any
]
interface
{
ChangeStreamFilter
(
data
ChangeStreamData
)
bson
.
M
type
ChangeStream
[
T
any
]
interface
{
DocumentEqual
(
a
,
b
T
)
bool
Filter
(
data
ChangeStreamData
)
bson
.
M
}
type
Cache
[
T
any
]
interface
{
GetCacheDocuments
()
[]
T
StartCache
(
ctx
context
.
Context
)
error
}
type
Base
[
T
any
]
struct
{
baseModel
model
.
Model
[
T
]
documents
[]
T
rwMutex
*
sync
.
RWMutex
baseModel
model
.
Model
[
T
]
changeStream
ChangeStream
[
T
]
documents
[]
T
rwMutex
*
sync
.
RWMutex
}
func
NewBase
[
T
any
](
baseModel
model
.
Model
[
T
])
*
Base
[
T
]
{
return
&
Base
[
T
]{
baseModel
:
baseModel
}
func
NewBase
[
T
any
](
baseModel
model
.
Model
[
T
]
,
changeStream
ChangeStream
[
T
]
)
*
Base
[
T
]
{
return
&
Base
[
T
]{
baseModel
:
baseModel
,
changeStream
:
changeStream
}
}
func
(
receiver
*
Base
[
T
])
StartCache
(
ctx
context
.
Context
,
cache
Cache
[
T
]
)
error
{
func
(
receiver
*
Base
[
T
])
StartCache
(
ctx
context
.
Context
)
error
{
documents
,
_
,
err
:=
receiver
.
baseModel
.
Find
(
ctx
,
bson
.
M
{})
if
err
!=
nil
{
return
err
...
...
@@ -48,7 +53,7 @@ func (receiver *Base[T]) StartCache(ctx context.Context, cache Cache[T]) error {
log
.
Println
(
err
)
continue
}
filter
:=
cache
.
ChangeStream
Filter
(
changeStreamData
)
filter
:=
receiver
.
changeStream
.
Filter
(
changeStreamData
)
document
,
err
:=
receiver
.
baseModel
.
FindOne
(
ctx
,
filter
)
if
err
!=
nil
{
log
.
Println
(
err
)
...
...
@@ -56,7 +61,7 @@ func (receiver *Base[T]) StartCache(ctx context.Context, cache Cache[T]) error {
}
documents
:=
receiver
.
GetCacheDocuments
()
for
i
:=
range
documents
{
if
cache
.
DocumentEqual
(
documents
[
i
],
document
)
{
if
receiver
.
changeStream
.
DocumentEqual
(
documents
[
i
],
document
)
{
documents
[
i
]
=
document
}
}
...
...
@@ -86,7 +91,7 @@ func (receiver *Base[T]) setAllDocuments(documents []T) {
receiver
.
documents
=
documents
}
func
(
receiver
*
Base
[
T
])
ChangeStream
Filter
(
data
ChangeStreamData
)
bson
.
M
{
func
(
receiver
*
Base
[
T
])
Filter
(
data
ChangeStreamData
)
bson
.
M
{
id
,
err
:=
primitive
.
ObjectIDFromHex
(
data
.
DocumentKey
.
Id
.
Oid
)
if
err
!=
nil
{
return
nil
...
...
internal/pkg/database/mongo/manager.go
View file @
83fdf144
package
mongo
import
(
"context"
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"
)
...
...
@@ -18,3 +19,11 @@ func NewManager(mongoUrl string) *Manager {
SitePlatformPriceConfigModel
:
sitePlatformPriceConfigModel
.
NewSitePlatformPriceConfigModel
(
mongoUrl
,
DatabaseColorWay
,
sitePlatformPriceConfigModel
.
Collection
),
}
}
func
(
receiver
*
Manager
)
StartCache
()
error
{
err
:=
receiver
.
SitePlatformPriceConfigModel
.
StartCache
(
context
.
Background
())
if
err
!=
nil
{
return
err
}
}
internal/pkg/database/mongo/model/site_platform_price_config/site_platform_price_config_model.go
View file @
83fdf144
...
...
@@ -13,6 +13,7 @@ type (
SitePlatformPriceConfigModel
interface
{
sitePlatformPriceConfigModel
cache
.
Cache
[
SitePlatformPriceConfig
]
cache
.
ChangeStream
[
SitePlatformPriceConfig
]
}
customSitePlatformPriceConfigModel
struct
{
...
...
@@ -25,10 +26,11 @@ type (
func
NewSitePlatformPriceConfigModel
(
url
,
db
,
collection
string
)
SitePlatformPriceConfigModel
{
conn
:=
mon
.
MustNewModel
(
url
,
db
,
collection
)
defaultModel
:=
newDefaultSitePlatformPriceConfigModel
(
conn
)
return
&
customSitePlatformPriceConfigModel
{
customModel
:=
&
customSitePlatformPriceConfigModel
{
defaultSitePlatformPriceConfigModel
:
defaultModel
,
Base
:
cache
.
NewBase
[
SitePlatformPriceConfig
](
defaultModel
),
}
customModel
.
Base
=
cache
.
NewBase
[
SitePlatformPriceConfig
](
defaultModel
,
customModel
)
return
customModel
}
func
(
receiver
*
customSitePlatformPriceConfigModel
)
DocumentEqual
(
a
,
b
SitePlatformPriceConfig
)
bool
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment