批量删除指定前缀的key
This commit is contained in:
parent
181c6912e1
commit
d09e01b03b
@ -2,7 +2,9 @@ package cluster
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go_dreamfactory/lego/sys/redis/core"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
@ -86,6 +88,24 @@ func (this *Redis) Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...
|
||||
return
|
||||
}
|
||||
|
||||
// 删除
|
||||
func (this *Redis) DelAllPrefixkey(ctx context.Context, prefix string) {
|
||||
iter := this.client.Scan(ctx, 0, prefix+"*", 0).Iterator()
|
||||
for iter.Next(ctx) {
|
||||
key := iter.Val()
|
||||
|
||||
// 使用 DEL 命令删除 key
|
||||
if err := this.client.Del(ctx, key).Err(); err != nil {
|
||||
log.Printf("Failed to delete key %s: %v", key, err)
|
||||
} else {
|
||||
fmt.Printf("Deleted key: %s\n", key)
|
||||
}
|
||||
}
|
||||
if err := iter.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
//锁
|
||||
func (this *Redis) Lock(key string, outTime int) (result bool, err error) {
|
||||
cmd := redis.NewBoolCmd(this.client.Context(), "set", key, 1, "ex", outTime, "nx")
|
||||
|
@ -124,8 +124,8 @@ type (
|
||||
EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd
|
||||
ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd
|
||||
// ScriptLoad(ctx context.Context, script string) *redis.StringCmd
|
||||
//
|
||||
FlushAll(ctx context.Context) *redis.StatusCmd
|
||||
DelAllPrefixkey(ctx context.Context, prefix string)
|
||||
}
|
||||
|
||||
ISys interface {
|
||||
|
@ -369,6 +369,11 @@ func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64)
|
||||
return this.client.ZScan(key, _cursor, match, count)
|
||||
}
|
||||
|
||||
// 删除
|
||||
func (this *Redis) DelAllPrefixkey(ctx context.Context, prefix string) {
|
||||
this.client.DelAllPrefixkey(ctx, prefix)
|
||||
}
|
||||
|
||||
//lua Script
|
||||
func (this *Redis) NewScript(src string) *redis.StringCmd {
|
||||
return this.client.NewScript(src)
|
||||
|
@ -2,7 +2,9 @@ package single
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go_dreamfactory/lego/sys/redis/core"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
@ -97,6 +99,23 @@ func (this *Redis) UnLock(key string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// 删除
|
||||
func (this *Redis) DelAllPrefixkey(ctx context.Context, prefix string) {
|
||||
iter := this.client.Scan(ctx, 0, prefix+"*", 0).Iterator()
|
||||
for iter.Next(ctx) {
|
||||
key := iter.Val()
|
||||
// 使用 DEL 命令删除 key
|
||||
if err := this.client.Del(ctx, key).Err(); err != nil {
|
||||
log.Printf("Failed to delete key %s: %v", key, err)
|
||||
} else {
|
||||
fmt.Printf("Deleted key: %s\n", key)
|
||||
}
|
||||
}
|
||||
if err := iter.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
//lua Script
|
||||
func (this *Redis) NewScript(src string) *redis.StringCmd {
|
||||
script := redis.NewScript(src)
|
||||
|
@ -46,6 +46,7 @@ func (this *VikingRank) Start() (err error) {
|
||||
// 0 0 0 ? * MON // 每周一零点
|
||||
cron.AddFunc("0 0 0 ? * MON", this.TimerSeason)
|
||||
|
||||
this.TimerSeason()
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/x/bsonx"
|
||||
@ -239,6 +240,7 @@ func (this *ModelSRank) raceSettlement() {
|
||||
szReward []*cfg.GameVikingRewardData
|
||||
mailCid string
|
||||
)
|
||||
|
||||
for iBossType := 1; iBossType <= 3; iBossType++ {
|
||||
mailCid = fmt.Sprintf("SeasonViking%dReward", iBossType)
|
||||
tableName := this.TableName + strconv.Itoa(int(iBossType))
|
||||
@ -274,4 +276,11 @@ func (this *ModelSRank) raceSettlement() {
|
||||
}, ids...)
|
||||
}
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
this.DBModel.Redis.DelAllPrefixkey(ctx, "vikingsrank")
|
||||
|
||||
this.DBModel.DB.DeleteMany(core.SqlTable(this.TableName), bson.M{})
|
||||
return
|
||||
}
|
||||
|
39
modules/viking/vking_test.go
Normal file
39
modules/viking/vking_test.go
Normal file
@ -0,0 +1,39 @@
|
||||
package viking_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
func main_test() {
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建 Redis 客户端
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:10011", // 你的 Redis 地址
|
||||
Password: "li13451234",
|
||||
DB: 10, // 使用的数据库编号
|
||||
})
|
||||
|
||||
// 指定要删除的 key 的前缀
|
||||
prefix := "your_prefix"
|
||||
|
||||
// 使用 SCAN 命令遍历匹配前缀的 key
|
||||
iter := client.Scan(ctx, 0, prefix+"*", 0).Iterator()
|
||||
for iter.Next(ctx) {
|
||||
key := iter.Val()
|
||||
|
||||
// 使用 DEL 命令删除 key
|
||||
if err := client.Del(ctx, key).Err(); err != nil {
|
||||
log.Printf("Failed to delete key %s: %v", key, err)
|
||||
} else {
|
||||
fmt.Printf("Deleted key: %s\n", key)
|
||||
}
|
||||
}
|
||||
if err := iter.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user