浏览代码

Merge pull request #128 from zxfishhack/cache-endpoint-and-plugin-install

add cache for endpoint by hook_id and pluginInstallation by plugin_id&tenant_id
Yeuoly 6 月之前
父节点
当前提交
398762558e
共有 3 个文件被更改,包括 69 次插入6 次删除
  1. 35 6
      internal/server/endpoint.go
  2. 20 0
      internal/service/install_service/state.go
  3. 14 0
      internal/types/models/curd/atomic.go

+ 35 - 6
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,9 +37,21 @@ 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.GetOne[models.Endpoint](
-		db.Equal("hook_id", hookId),
+	endpointCacheKey := strings.Join(
+		[]string{
+			"hook_id",
+			hookId,
+		},
+		":",
 	)
+	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
+		})
 	if err == db.ErrDatabaseNotFound {
 		ctx.JSON(404, exception.BadRequestError(errors.New("endpoint not found")).ToResponse())
 		return
@@ -50,9 +64,24 @@ func (app *App) EndpointHandler(ctx *gin.Context, hookId string, maxExecutionTim
 	}
 
 	// get plugin installation
-	pluginInstallation, err := db.GetOne[models.PluginInstallation](
-		db.Equal("plugin_id", endpoint.PluginID),
-		db.Equal("tenant_id", 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())
@@ -73,6 +102,6 @@ func (app *App) EndpointHandler(ctx *gin.Context, hookId string, maxExecutionTim
 	if ok, originalError := app.cluster.IsPluginOnCurrentNode(pluginUniqueIdentifier); !ok {
 		app.redirectPluginInvokeByPluginIdentifier(ctx, pluginUniqueIdentifier, originalError)
 	} else {
-		service.Endpoint(ctx, &endpoint, &pluginInstallation, maxExecutionTime, path)
+		service.Endpoint(ctx, endpoint, pluginInstallation, maxExecutionTime, path)
 	}
 }

+ 20 - 0
internal/service/install_service/state.go

@@ -1,6 +1,8 @@
 package install_service
 
 import (
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
+	gostrings "strings"
 	"time"
 
 	"github.com/langgenius/dify-plugin-daemon/internal/db"
@@ -134,6 +136,14 @@ func GetEndpoint(
 
 // uninstalls a plugin from db
 func UninstallEndpoint(endpoint *models.Endpoint) error {
+	cacheKey := gostrings.Join(
+		[]string{
+			"hook_id",
+			endpoint.HookID,
+		},
+		":",
+	)
+	_ = cache.AutoDelete[models.Endpoint](cacheKey)
 	return db.WithTransaction(func(tx *gorm.DB) error {
 		if err := db.Delete(endpoint, tx); err != nil {
 			return err
@@ -202,6 +212,16 @@ func DisabledEndpoint(endpoint_id string, tenant_id string) error {
 			return nil
 		}
 
+		endpointCacheKey := gostrings.Join(
+			[]string{
+				"hook_id",
+				endpoint.HookID,
+			},
+			":",
+		)
+
+		_ = cache.AutoDelete[models.Endpoint](endpointCacheKey)
+
 		endpoint.Enabled = false
 		if err := db.Update(endpoint, tx); err != nil {
 			return err

+ 14 - 0
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,6 +187,18 @@ func UninstallPlugin(
 		db.Equal("tenant_id", 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 {
 			return nil, errors.New("plugin has not been installed")