You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by li...@apache.org on 2025/05/09 03:50:34 UTC

(answer) 02/09: refactor(plugin): improved initialization of the KV storage plugin

This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/answer.git

commit 42cc06886abcdfc808cc6c3b5c637f31c5799029
Author: Sonui <me...@sonui.cn>
AuthorDate: Sat Mar 1 11:22:40 2025 +0800

    refactor(plugin): improved initialization of the KV storage plugin
---
 .../service/plugin_common/plugin_common_service.go | 10 ++++++---
 plugin/kv_storage.go                               | 24 ++++++++--------------
 plugin/plugin.go                                   |  2 +-
 3 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/internal/service/plugin_common/plugin_common_service.go b/internal/service/plugin_common/plugin_common_service.go
index de6d981d..d3aa839b 100644
--- a/internal/service/plugin_common/plugin_common_service.go
+++ b/internal/service/plugin_common/plugin_common_service.go
@@ -135,9 +135,13 @@ func (ps *PluginCommonService) GetUserPluginConfig(ctx context.Context, req *sch
 }
 
 func (ps *PluginCommonService) initPluginData() {
-	plugin.SetKVStorageDB(&plugin.Data{
-		DB:    ps.data.DB,
-		Cache: ps.data.Cache,
+	_ = plugin.CallKVStorage(func(k plugin.KVStorage) error {
+		k.SetOperator(plugin.NewKVOperator(
+			ps.data.DB,
+			ps.data.Cache,
+			k.Info().SlugName,
+		))
+		return nil
 	})
 
 	// init plugin status
diff --git a/plugin/kv_storage.go b/plugin/kv_storage.go
index 9714487f..b4d940b8 100644
--- a/plugin/kv_storage.go
+++ b/plugin/kv_storage.go
@@ -323,22 +323,16 @@ type KVStorage interface {
 }
 
 var (
-	_,
-	registerPluginKVStorage = func() (CallFn[KVStorage], RegisterFn[KVStorage]) {
-		callFn, registerFn := MakePlugin[KVStorage](false)
-		return callFn, func(p KVStorage) {
-			registerFn(p)
-			kvStoragePluginStack.plugins = append(kvStoragePluginStack.plugins, p)
-		}
-	}()
-	kvStoragePluginStack = &Stack[KVStorage]{}
+	CallKVStorage,
+	registerKVStorage = MakePlugin[KVStorage](true)
 )
 
-func SetKVStorageDB(data *Data) {
-	for _, p := range kvStoragePluginStack.plugins {
-		p.SetOperator(&KVOperator{
-			data:           data,
-			pluginSlugName: p.Info().SlugName,
-		})
+// NewKVOperator creates a new KV storage operator with the specified database engine, cache and plugin name.
+// It returns a KVOperator instance that can be used to interact with the plugin's storage.
+func NewKVOperator(db *xorm.Engine, cache cache.Cache, pluginSlugName string) *KVOperator {
+	return &KVOperator{
+		data:           &Data{DB: db, Cache: cache},
+		pluginSlugName: pluginSlugName,
+		cacheTTL:       30 * time.Minute,
 	}
 }
diff --git a/plugin/plugin.go b/plugin/plugin.go
index dc8c35ea..a9e17310 100644
--- a/plugin/plugin.go
+++ b/plugin/plugin.go
@@ -124,7 +124,7 @@ func Register(p Base) {
 	}
 
 	if _, ok := p.(KVStorage); ok {
-		registerPluginKVStorage(p.(KVStorage))
+		registerKVStorage(p.(KVStorage))
 	}
 }