| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 | package persistenceimport (	"encoding/hex"	"fmt"	"time"	"github.com/langgenius/dify-plugin-daemon/internal/utils/cache")type Persistence struct {	storage PersistenceStorage}const (	CACHE_KEY_PREFIX = "persistence:cache")func (c *Persistence) getCacheKey(tenant_id string, plugin_identity string, key string) string {	return fmt.Sprintf("%s:%s:%s:%s", CACHE_KEY_PREFIX, tenant_id, plugin_identity, key)}func (c *Persistence) Save(tenant_id string, plugin_identity string, key string, data []byte) error {	if len(key) > 64 {		return fmt.Errorf("key length must be less than 64 characters")	}	if err := c.storage.Save(tenant_id, plugin_identity, key, data); err != nil {		return err	}	// delete from cache	return cache.Del(c.getCacheKey(tenant_id, plugin_identity, key))}func (c *Persistence) Load(tenant_id string, plugin_identity string, key string) ([]byte, error) {	// check if the key exists in cache	h, err := cache.GetString(c.getCacheKey(tenant_id, plugin_identity, key))	if err != nil && err != cache.ErrNotFound {		return nil, err	}	if err == nil {		return hex.DecodeString(h)	}	// load from storage	data, err := c.storage.Load(tenant_id, plugin_identity, key)	if err != nil {		return nil, err	}	// add to cache	cache.Store(c.getCacheKey(tenant_id, plugin_identity, key), hex.EncodeToString(data), time.Minute*5)	return data, nil}func (c *Persistence) Delete(tenant_id string, plugin_identity string, key string) error {	// delete from cache and storage	err := cache.Del(c.getCacheKey(tenant_id, plugin_identity, key))	if err != nil {		return err	}	return c.storage.Delete(tenant_id, plugin_identity, key)}
 |