Skip to content

Commit 42cc068

Browse files
sosyzLinkinStars
authored andcommitted
refactor(plugin): improved initialization of the KV storage plugin
1 parent 20d7e4b commit 42cc068

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

internal/service/plugin_common/plugin_common_service.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,13 @@ func (ps *PluginCommonService) GetUserPluginConfig(ctx context.Context, req *sch
135135
}
136136

137137
func (ps *PluginCommonService) initPluginData() {
138-
plugin.SetKVStorageDB(&plugin.Data{
139-
DB: ps.data.DB,
140-
Cache: ps.data.Cache,
138+
_ = plugin.CallKVStorage(func(k plugin.KVStorage) error {
139+
k.SetOperator(plugin.NewKVOperator(
140+
ps.data.DB,
141+
ps.data.Cache,
142+
k.Info().SlugName,
143+
))
144+
return nil
141145
})
142146

143147
// init plugin status

plugin/kv_storage.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -323,22 +323,16 @@ type KVStorage interface {
323323
}
324324

325325
var (
326-
_,
327-
registerPluginKVStorage = func() (CallFn[KVStorage], RegisterFn[KVStorage]) {
328-
callFn, registerFn := MakePlugin[KVStorage](false)
329-
return callFn, func(p KVStorage) {
330-
registerFn(p)
331-
kvStoragePluginStack.plugins = append(kvStoragePluginStack.plugins, p)
332-
}
333-
}()
334-
kvStoragePluginStack = &Stack[KVStorage]{}
326+
CallKVStorage,
327+
registerKVStorage = MakePlugin[KVStorage](true)
335328
)
336329

337-
func SetKVStorageDB(data *Data) {
338-
for _, p := range kvStoragePluginStack.plugins {
339-
p.SetOperator(&KVOperator{
340-
data: data,
341-
pluginSlugName: p.Info().SlugName,
342-
})
330+
// NewKVOperator creates a new KV storage operator with the specified database engine, cache and plugin name.
331+
// It returns a KVOperator instance that can be used to interact with the plugin's storage.
332+
func NewKVOperator(db *xorm.Engine, cache cache.Cache, pluginSlugName string) *KVOperator {
333+
return &KVOperator{
334+
data: &Data{DB: db, Cache: cache},
335+
pluginSlugName: pluginSlugName,
336+
cacheTTL: 30 * time.Minute,
343337
}
344338
}

plugin/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func Register(p Base) {
124124
}
125125

126126
if _, ok := p.(KVStorage); ok {
127-
registerPluginKVStorage(p.(KVStorage))
127+
registerKVStorage(p.(KVStorage))
128128
}
129129
}
130130

0 commit comments

Comments
 (0)