feat(subscription): enhance subscription cache management and improve error handling

This commit is contained in:
Chang lue Tsen
2025-08-15 14:45:54 -04:00
parent 740dd48763
commit 2be1c4f6ed
11 changed files with 187 additions and 28 deletions
+2 -1
View File
@@ -46,8 +46,9 @@ var (
func (m *customServerModel) ClearCache(ctx context.Context, id int64) error {
serverIdKey := fmt.Sprintf("%s%v", cacheServerIdPrefix, id)
configKey := fmt.Sprintf("%s%d", config.ServerConfigCacheKey, id)
userListKey := fmt.Sprintf("%s%v", config.ServerUserListCacheKey, id)
return m.DelCacheCtx(ctx, serverIdKey, configKey)
return m.DelCacheCtx(ctx, serverIdKey, configKey, userListKey)
}
// QueryServerCountByServerGroups Query Server Count By Server Groups
+23
View File
@@ -4,9 +4,11 @@ import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"github.com/perfect-panel/server/internal/config"
"github.com/perfect-panel/server/internal/model/server"
"github.com/perfect-panel/server/pkg/cache"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
@@ -69,6 +71,27 @@ func (m *defaultSubscribeModel) getCacheKeys(data *Subscribe) []string {
}
}
}
// Temporary solution waiting for refactoring
if data.ServerGroup != "" {
cacheKey := strings.Split(data.ServerGroup, ",")
groupIds := make([]int64, 0)
for _, v := range cacheKey {
if v != "" {
id, _ := strconv.ParseInt(v, 10, 64)
if id > 0 {
groupIds = append(groupIds, id)
}
}
}
var ids []int64
_ = m.Transaction(context.Background(), func(tx *gorm.DB) error {
return tx.Model(&server.Server{}).Where("group_id IN ?", groupIds).Pluck("id", &ids).Error
})
for _, id := range ids {
serverKey = append(serverKey, fmt.Sprintf("%s%v", config.ServerUserListCacheKey, id))
}
}
cacheKeys := []string{SubscribeIdKey}
if len(serverKey) > 0 {
cacheKeys = append(cacheKeys, serverKey...)
+9 -14
View File
@@ -35,7 +35,7 @@ type customSubscribeLogicModel interface {
QuerySubscribeIdsByServerIdAndServerGroupId(ctx context.Context, serverId, serverGroupId int64) ([]*Subscribe, error)
QuerySubscribeMinSortByIds(ctx context.Context, ids []int64) (int64, error)
QuerySubscribeListByIds(ctx context.Context, ids []int64) ([]*Subscribe, error)
ClearCache(ctx context.Context, id int64) error
ClearCache(ctx context.Context, id ...int64) error
}
// NewModel returns a model for the database table.
@@ -109,23 +109,18 @@ func (m *customSubscribeModel) QuerySubscribeListByIds(ctx context.Context, ids
return list, err
}
func (m *customSubscribeModel) ClearCache(ctx context.Context, id int64) error {
if id <= 0 {
func (m *customSubscribeModel) ClearCache(ctx context.Context, ids ...int64) error {
if len(ids) <= 0 {
return nil
}
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
cacheKeys := m.getCacheKeys(data)
cacheKeys = append(cacheKeys, m.getCacheKeys(&Subscribe{Id: id})...)
for _, key := range cacheKeys {
if err := m.CachedConn.DelCacheCtx(ctx, key); err != nil {
var cacheKeys []string
for _, id := range ids {
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
cacheKeys = append(cacheKeys, m.getCacheKeys(data)...)
}
return nil
return m.CachedConn.DelCacheCtx(ctx, cacheKeys...)
}
+1 -1
View File
@@ -64,7 +64,7 @@ func (s *Subscribe) GetCacheKeys() []string {
if s == nil {
return []string{}
}
keys := []string{}
keys := make([]string, 0)
if s.Token != "" {
keys = append(keys, fmt.Sprintf("%s%s", cacheUserSubscribeTokenPrefix, s.Token))