Files
Xiyuan Chen 0b66639aba feat(cache): cache the tenant model provider list in the daemon (#780)
* fix(parser): let UnmarshalJson return slice payloads

validator.Struct rejects any non-struct top-level value, so UnmarshalJson
with a slice T always failed before returning. Skip validation for slices
alongside maps and strings.

UnmarshalJsonBytes2Slice remains the variant that validates per element.

* feat(cache): cache tenant model installations in the daemon

Dify caches the per-tenant model provider list, but enterprise installs
plugins through the daemon directly, so Dify never learns its cache is
stale and a newly assigned plugin stays invisible until the TTL expires.

Move the cache to where the writes happen. ListModels now reads through a
Redis hash keyed per tenant, with the page as the field so a single DEL
drops every cached page, and InstallPlugin/UninstallPlugin/UpgradePlugin
invalidate it.

The invalidation is deferred rather than run after the transaction: a
retried install aborts on ErrPluginAlreadyInstalled before reaching any
post-transaction code, so a DEL placed there is unreachable once it has
failed once.

TTL defaults to 60 minutes, configurable via
PLUGIN_MODEL_INSTALLATIONS_CACHE_TTL.

* Update PLUGIN_MODEL_INSTALLATIONS_CACHE_TTL value

* Increase PluginModelInstallationsCacheTTL to 1440

* feat(cache): put the model installations cache behind a switch, off by default

The daemon cache holds the same tenant payload dify already caches, so leaving
both on doubles the redis footprint and forces a scale-up before a cloud
production rollout. PLUGIN_MODEL_INSTALLATIONS_CACHE_ENABLED defaults to false;
enable it only where dify runs with PLUGIN_MODEL_PROVIDERS_CACHE_ENABLED=false.
2026-07-28 02:01:23 -07:00

69 lines
1.4 KiB
Go

package parser
import (
"encoding/json"
"reflect"
"github.com/langgenius/dify-plugin-daemon/pkg/validators"
)
func UnmarshalJson[T any](text string) (T, error) {
return UnmarshalJsonBytes[T]([]byte(text))
}
func UnmarshalJsonBytes[T any](data []byte) (T, error) {
var result T
err := json.Unmarshal(data, &result)
if err != nil {
return result, err
}
// the entity validator only accepts structs, so kinds it cannot handle skip validation
switch reflect.TypeOf(result).Kind() {
case reflect.Map, reflect.String, reflect.Slice:
return result, nil
}
if err := validators.GlobalEntitiesValidator.Struct(&result); err != nil {
return result, err
}
return result, err
}
func UnmarshalJsonBytes2Slice[T any](data []byte) ([]T, error) {
var result []T
err := json.Unmarshal(data, &result)
if err != nil {
return nil, err
}
for _, item := range result {
if err := validators.GlobalEntitiesValidator.Struct(&item); err != nil {
return nil, err
}
}
return result, err
}
func MarshalJson[T any](data T) string {
b, _ := json.Marshal(data)
return string(b)
}
func MarshalJsonBytes[T any](data T) []byte {
b, _ := json.Marshal(data)
return b
}
func UnmarshalJsonBytes2Map(data []byte) (map[string]any, error) {
result := map[string]any{}
err := json.Unmarshal(data, &result)
return result, err
}
func UnmarshalJson2Map(json string) (map[string]any, error) {
return UnmarshalJsonBytes2Map([]byte(json))
}