90 lines
2.4 KiB
Go
90 lines
2.4 KiB
Go
// Package model define model struct of model runtime service
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"modelRT/constants"
|
|
"modelRT/diagram"
|
|
"modelRT/logger"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// CleanupRecommendRedisCache define func to cleanup component measurement and attribute roup in redis cache
|
|
func CleanupRecommendRedisCache(ctx context.Context) error {
|
|
rdb := diagram.GetRedisClientInstance()
|
|
|
|
attrTypes, err := rdb.SMembers(ctx, constants.RedisAllConfigSetKey).Result()
|
|
if err != nil {
|
|
logger.Error(ctx, "failed to get attribute types from redis", "error", err)
|
|
return err
|
|
}
|
|
|
|
for _, attrType := range attrTypes {
|
|
pattern := fmt.Sprintf("*_%s", attrType)
|
|
if err := deleteKeysByPattern(ctx, rdb, pattern); err != nil {
|
|
logger.Warn(ctx, "cleanup attribute type pattern failed",
|
|
"attr_type", attrType, "pattern", pattern, "error", err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
fixedKeys := []string{
|
|
constants.RedisAllGridSetKey,
|
|
constants.RedisAllZoneSetKey,
|
|
constants.RedisAllStationSetKey,
|
|
constants.RedisAllCompNSPathSetKey,
|
|
constants.RedisAllCompTagSetKey,
|
|
constants.RedisAllMeasTagSetKey,
|
|
constants.RedisAllConfigSetKey,
|
|
constants.RedisSearchDictName,
|
|
}
|
|
|
|
if err := rdb.Del(ctx, fixedKeys...).Err(); err != nil {
|
|
logger.Error(ctx, "delete fixed redis keys failed", "error", err)
|
|
return err
|
|
}
|
|
|
|
patterns := []string{
|
|
"*_zone_tag_keys", // correspond RedisSpecGridZoneSetKey
|
|
"*_station_tag_keys", // correspond RedisSpecZoneStationSetKey
|
|
"*_component_nspath_keys", // correspond RedisSpecStationCompNSPATHSetKey
|
|
"*_component_tag_keys", // correspond RedisSpecCompNSPathCompTagSetKey
|
|
"*_measurement_tag_keys", // correspond RedisSpecCompTagMeasSetKey
|
|
}
|
|
|
|
for _, pattern := range patterns {
|
|
if err := deleteKeysByPattern(ctx, rdb, pattern); err != nil {
|
|
logger.Warn(ctx, "cleanup pattern keys failed", "pattern", pattern, "error", err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
logger.Info(ctx, "cleanup component measurement and attribute group in redis cache completed")
|
|
return nil
|
|
}
|
|
|
|
func deleteKeysByPattern(ctx context.Context, rdb *redis.Client, pattern string) error {
|
|
var cursor uint64
|
|
for {
|
|
keys, nextCursor, err := rdb.Scan(ctx, cursor, pattern, 100).Result()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(keys) > 0 {
|
|
if err := rdb.Del(ctx, keys...).Err(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
cursor = nextCursor
|
|
if cursor == 0 {
|
|
break
|
|
}
|
|
}
|
|
return nil
|
|
}
|