feat(api): refactor cache key handling for server and user lists

This commit is contained in:
Chang lue Tsen
2025-08-31 11:29:55 -04:00
parent 82fd674ae4
commit 6dc1bee14c
8 changed files with 143 additions and 38 deletions
+49 -4
View File
@@ -15,16 +15,17 @@ type customServerLogicModel interface {
const (
// ServerUserListCacheKey Server User List Cache Key
ServerUserListCacheKey = "server:user_list:id:"
ServerUserListCacheKey = "server:user:"
// ServerConfigCacheKey Server Config Cache Key
ServerConfigCacheKey = "server:config:id:"
ServerConfigCacheKey = "server:config:"
)
// FilterParams Filter Server Params
type FilterParams struct {
Page int
Size int
Ids []int64 // Server IDs
Search string
}
@@ -53,7 +54,9 @@ func (m *customServerModel) FilterServerList(ctx context.Context, params *Filter
s := "%" + params.Search + "%"
query = query.Where("`name` LIKE ? OR `address` LIKE ?", s, s)
}
if len(params.Ids) > 0 {
query = query.Where("id IN ?", params.Ids)
}
err := query.Count(&total).Limit(params.Size).Offset((params.Page - 1) * params.Size).Find(&servers).Error
return total, servers, err
}
@@ -101,7 +104,49 @@ func (m *customServerModel) ClearNodeCache(ctx context.Context, params *FilterNo
}
var cacheKeys []string
for _, node := range nodes {
cacheKeys = append(cacheKeys, fmt.Sprintf("%s%d", ServerUserListCacheKey, node.ServerId), fmt.Sprintf("%s%d", ServerConfigCacheKey, node.ServerId))
cacheKeys = append(cacheKeys, fmt.Sprintf("%s%d", ServerUserListCacheKey, node.ServerId))
if node.Protocol != "" {
var cursor uint64
for {
keys, newCursor, err := m.Cache.Scan(ctx, cursor, fmt.Sprintf("%s%d*", ServerConfigCacheKey, node.ServerId), 100).Result()
if err != nil {
return err
}
if len(keys) > 0 {
cacheKeys = append(keys, keys...)
}
cursor = newCursor
if cursor == 0 {
break
}
}
}
}
if len(cacheKeys) > 0 {
cacheKeys = tool.RemoveDuplicateElements(cacheKeys...)
return m.Cache.Del(ctx, cacheKeys...).Err()
}
return nil
}
// ClearServerCache Clear Server Cache
func (m *customServerModel) ClearServerCache(ctx context.Context, serverId int64) error {
var cacheKeys []string
cacheKeys = append(cacheKeys, fmt.Sprintf("%s%d", ServerUserListCacheKey, serverId))
var cursor uint64
for {
keys, newCursor, err := m.Cache.Scan(ctx, 0, fmt.Sprintf("%s%d*", ServerConfigCacheKey, serverId), 100).Result()
if err != nil {
return err
}
if len(keys) > 0 {
cacheKeys = append(cacheKeys, keys...)
}
cursor = newCursor
if cursor == 0 {
break
}
}
if len(cacheKeys) > 0 {
+7 -6
View File
@@ -5,8 +5,6 @@ import (
"errors"
"fmt"
"github.com/perfect-panel/server/internal/config"
"github.com/perfect-panel/server/pkg/cache"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
@@ -62,20 +60,23 @@ func (m *defaultServerModel) batchGetCacheKeys(Servers ...*Server) []string {
return keys
}
func (m *defaultServerModel) getCacheKeys(data *Server) []string {
if data == nil {
return []string{}
}
detailsKey := fmt.Sprintf("%s%v", CacheServerDetailPrefix, data.Id)
ServerIdKey := fmt.Sprintf("%s%v", cacheServerIdPrefix, data.Id)
configIdKey := fmt.Sprintf("%s%v", config.ServerConfigCacheKey, data.Id)
userIDKey := fmt.Sprintf("%s%d", config.ServerUserListCacheKey, data.Id)
//configIdKey := fmt.Sprintf("%s%v", config.ServerConfigCacheKey, data.Id)
//userIDKey := fmt.Sprintf("%s%d", config.ServerUserListCacheKey, data.Id)
// query protocols to get config keys
cacheKeys := []string{
ServerIdKey,
detailsKey,
configIdKey,
userIDKey,
//configIdKey,
//userIDKey,
}
return cacheKeys
}
+3 -4
View File
@@ -5,7 +5,6 @@ import (
"fmt"
"strings"
"github.com/perfect-panel/server/internal/config"
"gorm.io/gorm"
)
@@ -45,10 +44,10 @@ var (
// ClearCache Clear Cache
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)
//configKey := fmt.Sprintf("%s%d", config.ServerConfigCacheKey, id)
//userListKey := fmt.Sprintf("%s%v", config.ServerUserListCacheKey, id)
return m.DelCacheCtx(ctx, serverIdKey, configKey, userListKey)
return m.DelCacheCtx(ctx, serverIdKey)
}
// QueryServerCountByServerGroups Query Server Count By Server Groups