浏览代码

add cache for endpoint by hook_id and plugin by plugin_id&tenant_id

zxfishhack 4 月之前
父节点
当前提交
1e5e03eaf0
共有 3 个文件被更改,包括 54 次插入8 次删除
  1. 26 8
      internal/server/endpoint.go
  2. 17 0
      internal/service/install_service/state.go
  3. 11 0
      internal/types/models/curd/atomic.go

+ 26 - 8
internal/server/endpoint.go

@@ -35,9 +35,17 @@ 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),
-	)
+	endpoint, err := db.GetCache[models.Endpoint](&db.GetCachePayload[models.Endpoint]{
+		Getter: 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
@@ -50,10 +58,20 @@ 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),
-	)
+	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},
+			},
+		})
 	if err != nil {
 		ctx.JSON(404, exception.BadRequestError(errors.New("plugin installation not found")).ToResponse())
 		return
@@ -73,6 +91,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)
 	}
 }

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

@@ -134,6 +134,14 @@ 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},
+		},
+	})
 	return db.WithTransaction(func(tx *gorm.DB) error {
 		if err := db.Delete(endpoint, tx); err != nil {
 			return err
@@ -202,6 +210,15 @@ func DisabledEndpoint(endpoint_id string, tenant_id string) error {
 			return nil
 		}
 
+		_ = db.DeleteCache[models.Endpoint](&db.DeleteCachePayload[models.Endpoint]{
+			Delete: func() error {
+				return nil
+			},
+			CacheKey: []db.KeyValuePair{
+				{Key: "hook_id", Val: endpoint.HookID},
+			},
+		})
+
 		endpoint.Enabled = false
 		if err := db.Update(endpoint, tx); err != nil {
 			return err

+ 11 - 0
internal/types/models/curd/atomic.go

@@ -185,6 +185,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},
+			},
+		})
+
 	if err != nil {
 		if err == db.ErrDatabaseNotFound {
 			return nil, errors.New("plugin has not been installed")