optimize func of cleanup recommend redis cache
This commit is contained in:
parent
8e4bdfd0e9
commit
7969861746
8
main.go
8
main.go
|
|
@ -152,6 +152,12 @@ func main() {
|
|||
// panic(err)
|
||||
// }
|
||||
|
||||
err := model.CleanupRecommendRedisCache(ctx)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "clean up component measurement and attribute group failed", "error", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
measurementSet, err := database.GetFullMeasurementSet(tx)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "generate component measurement group failed", "error", err)
|
||||
|
|
@ -163,8 +169,6 @@ func main() {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("generate full path mapping result:%v\n", parentPath)
|
||||
|
||||
compAttrSet, err := database.GenAllAttributeMap(tx)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "generate component attribute group failed", "error", err)
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ import (
|
|||
"modelRT/orm"
|
||||
|
||||
"github.com/RediSearch/redisearch-go/v2/redisearch"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
|
@ -140,6 +139,7 @@ func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, ful
|
|||
}
|
||||
|
||||
if len(attrNameMembers) > 0 {
|
||||
pipe.SAdd(ctx, constants.RedisAllMeasTagSetKey, attrNameMembers)
|
||||
pipe.SAdd(ctx, specCompMeasKey, attrNameMembers)
|
||||
}
|
||||
|
||||
|
|
@ -12,7 +12,6 @@ import (
|
|||
"modelRT/orm"
|
||||
|
||||
"github.com/RediSearch/redisearch-go/v2/redisearch"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// TraverseMeasurementGroupTables define func to traverse component measurement group tables
|
||||
|
|
@ -184,6 +183,4 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
|
|||
}
|
||||
|
||||
return compTagToFullPath, nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
// 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)
|
||||
fmt.Printf("delete patterm:%s\n", pattern)
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Loading…
Reference in New Issue