浏览代码

use AutoTypeWithGetter

zxfishhack 6 月之前
父节点
当前提交
3aa0490294
共有 3 个文件被更改,包括 67 次插入45 次删除
  1. 33 21
      internal/server/endpoint.go
  2. 21 14
      internal/service/install_service/state.go
  3. 13 10
      internal/types/models/curd/atomic.go

+ 33 - 21
internal/server/endpoint.go

@@ -2,6 +2,8 @@ package server
 
 import (
 	"errors"
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
+	"strings"
 	"time"
 
 	"github.com/gin-gonic/gin"
@@ -35,17 +37,22 @@ func (app *App) Endpoint(config *app.Config) func(c *gin.Context) {
 }
 
 func (app *App) EndpointHandler(ctx *gin.Context, hookId string, maxExecutionTime time.Duration, path string) {
-	endpoint, err := db.GetCache[models.Endpoint](&db.GetCachePayload[models.Endpoint]{
-		Getter: func() (*models.Endpoint, error) {
+	endpointCacheKey := strings.Join(
+		[]string{
+			"hook_id",
+			hookId,
+		},
+		":",
+	)
+	log.Info(endpointCacheKey)
+	endpoint, err := cache.AutoGetWithGetter[models.Endpoint](
+		endpointCacheKey,
+		func() (*models.Endpoint, error) {
 			v, err := db.GetOne[models.Endpoint](
 				db.Equal("hook_id", hookId),
 			)
 			return &v, err
-		},
-		CacheKey: []db.KeyValuePair{
-			{Key: "hook_id", Val: hookId},
-		},
-	})
+		})
 	if err == db.ErrDatabaseNotFound {
 		ctx.JSON(404, exception.BadRequestError(errors.New("endpoint not found")).ToResponse())
 		return
@@ -58,20 +65,25 @@ func (app *App) EndpointHandler(ctx *gin.Context, hookId string, maxExecutionTim
 	}
 
 	// get plugin installation
-	pluginInstallation, err := db.GetCache[models.PluginInstallation](
-		&db.GetCachePayload[models.PluginInstallation]{
-			Getter: func() (*models.PluginInstallation, error) {
-				v, err := db.GetOne[models.PluginInstallation](
-					db.Equal("plugin_id", endpoint.PluginID),
-					db.Equal("tenant_id", endpoint.TenantID),
-				)
-				return &v, err
-			},
-			CacheKey: []db.KeyValuePair{
-				{Key: "plugin_id", Val: endpoint.PluginID},
-				{Key: "tenant_id", Val: endpoint.TenantID},
-			},
-		})
+	pluginInstallationCacheKey := strings.Join(
+		[]string{
+			"plugin_id",
+			endpoint.PluginID,
+			"tenant_id",
+			endpoint.TenantID,
+		},
+		":",
+	)
+	pluginInstallation, err := cache.AutoGetWithGetter[models.PluginInstallation](
+		pluginInstallationCacheKey,
+		func() (*models.PluginInstallation, error) {
+			v, err := db.GetOne[models.PluginInstallation](
+				db.Equal("plugin_id", endpoint.PluginID),
+				db.Equal("tenant_id", endpoint.TenantID),
+			)
+			return &v, err
+		},
+	)
 	if err != nil {
 		ctx.JSON(404, exception.BadRequestError(errors.New("plugin installation not found")).ToResponse())
 		return

+ 21 - 14
internal/service/install_service/state.go

@@ -1,6 +1,9 @@
 package install_service
 
 import (
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
+	"log"
+	gostrings "strings"
 	"time"
 
 	"github.com/langgenius/dify-plugin-daemon/internal/db"
@@ -134,14 +137,15 @@ func GetEndpoint(
 
 // uninstalls a plugin from db
 func UninstallEndpoint(endpoint *models.Endpoint) error {
-	_ = db.DeleteCache[models.Endpoint](&db.DeleteCachePayload[models.Endpoint]{
-		Delete: func() error {
-			return nil
-		},
-		CacheKey: []db.KeyValuePair{
-			{Key: "hook_id", Val: endpoint.HookID},
+	cacheKey := gostrings.Join(
+		[]string{
+			"hook_id",
+			endpoint.HookID,
 		},
-	})
+		":",
+	)
+	log.Print(cacheKey)
+	_ = cache.AutoDelete[models.Endpoint](cacheKey)
 	return db.WithTransaction(func(tx *gorm.DB) error {
 		if err := db.Delete(endpoint, tx); err != nil {
 			return err
@@ -210,14 +214,17 @@ func DisabledEndpoint(endpoint_id string, tenant_id string) error {
 			return nil
 		}
 
-		_ = db.DeleteCache[models.Endpoint](&db.DeleteCachePayload[models.Endpoint]{
-			Delete: func() error {
-				return nil
+		endpointCacheKey := gostrings.Join(
+			[]string{
+				"hook_id",
+				endpoint.HookID,
 			},
-			CacheKey: []db.KeyValuePair{
-				{Key: "hook_id", Val: endpoint.HookID},
-			},
-		})
+			":",
+		)
+
+		log.Print(endpointCacheKey)
+
+		_ = cache.AutoDelete[models.Endpoint](endpointCacheKey)
 
 		endpoint.Enabled = false
 		if err := db.Update(endpoint, tx); err != nil {

+ 13 - 10
internal/types/models/curd/atomic.go

@@ -2,6 +2,8 @@ package curd
 
 import (
 	"errors"
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
+	"strings"
 
 	"github.com/langgenius/dify-plugin-daemon/internal/db"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/models"
@@ -185,16 +187,17 @@ func UninstallPlugin(
 		db.Equal("tenant_id", tenant_id),
 	)
 
-	_ = db.DeleteCache[models.PluginInstallation](
-		&db.DeleteCachePayload[models.PluginInstallation]{
-			Delete: func() error {
-				return nil
-			},
-			CacheKey: []db.KeyValuePair{
-				{Key: "plugin_id", Val: plugin_unique_identifier.PluginID()},
-				{Key: "tenant_id", Val: tenant_id},
-			},
-		})
+	pluginInstallationCacheKey := strings.Join(
+		[]string{
+			"plugin_id",
+			plugin_unique_identifier.PluginID(),
+			"tenant_id",
+			tenant_id,
+		},
+		":",
+	)
+
+	_ = cache.AutoDelete[models.PluginInstallation](pluginInstallationCacheKey)
 
 	if err != nil {
 		if err == db.ErrDatabaseNotFound {