github.com/kaydxh/golang@v0.0.131/pkg/database/redis/redis_key_delete_test.go (about) 1 /* 2 *Copyright (c) 2023, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package redis_test 23 24 import ( 25 "context" 26 "flag" 27 "fmt" 28 "testing" 29 ) 30 31 var ( 32 keyPrefix = flag.String("keyPrefix", "_JOB_SERVICE", "key prefix") 33 deleteKeys = flag.Bool("deleteKeys", false, "delete keys") 34 ) 35 36 // GOOS=linux GOARCH=amd64 go test -c redis_key_delete_test.go redis_string_test.go 37 //go test -v --count=1 --test.timeout=0 -test.run TestDeletePrefixKeys 38 //go test -v --count=1 --test.timeout=0 -test.run TestDeletePrefixKeys -keyPrefix="test-" -deleteKeys=true 39 func TestDeletePrefixKeys(t *testing.T) { 40 db := GetDBOrDie() 41 42 testCases := []struct { 43 // KeyPrefix string 44 // 是否有过期时间 45 ttl bool 46 batch int64 47 // deleteKeys bool 48 dump bool 49 }{ 50 { 51 // KeyPrefix: "_JOB_SERVICE", 52 ttl: false, 53 batch: 500, 54 // deleteKeys: false, 55 dump: false, 56 }, 57 } 58 ctx := context.Background() 59 //ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 60 //defer cancel() 61 62 var count int64 63 var deletedKeys []string 64 for _, testCase := range testCases { 65 t.Run(fmt.Sprintf("case-keyPrefix[%v]-deleteKeys[%v]", *keyPrefix, *deleteKeys), func(t *testing.T) { 66 iter := db.Scan(ctx, 0, fmt.Sprintf("%s*", *keyPrefix), 0).Iterator() 67 if err != nil { 68 t.Fatalf("failed to scan key[%v], err: %v", *keyPrefix, err) 69 } 70 71 for iter.Next(ctx) { 72 key := iter.Val() 73 /* 74 tp, err := db.Type(ctx, key).Result() 75 if err != nil { 76 t.Errorf("failed to type key[%v], err: %v", key, err) 77 continue 78 } 79 80 switch tp { 81 case redis_.TypeString: 82 83 } 84 */ 85 /* 86 value, err := db.Get(ctx, key).Result() 87 if err != nil { 88 t.Fatalf("failed to get key[%v], err: %v", key, err) 89 } 90 */ 91 if testCase.dump { 92 dumpVal, err := db.Dump(ctx, key).Result() 93 if err != nil { 94 t.Fatalf("failed to Dump, err: %v", err) 95 } 96 //t.Logf("key[%v], dumpValue[%v], value[%v]", key, dumpVal, value) 97 t.Logf("key[%v], dumpValue[%v]", key, dumpVal) 98 } 99 //t.Logf("get key[%v] value[%v]", key, value) 100 101 //todo delete 102 if testCase.ttl { 103 d, err := db.TTL(ctx, key).Result() 104 if err != nil { 105 t.Fatalf("failed to get key[%v], err: %v", key, err) 106 } 107 if d == -1 { // -1 means no TTL 108 deletedKeys = append(deletedKeys, key) 109 count++ 110 } 111 112 } else { 113 count++ 114 deletedKeys = append(deletedKeys, key) 115 } 116 117 if count%testCase.batch == 0 { 118 if *deleteKeys { 119 if err := db.Del(ctx, deletedKeys...).Err(); err != nil { 120 t.Fatalf("failed to delete keys[%v], err: %v", key, err) 121 } 122 } 123 deletedKeys = nil 124 t.Logf("key number: %v ...", count) 125 } 126 } 127 if err := iter.Err(); err != nil { 128 t.Fatalf("failed to iter err: %v", err) 129 } 130 131 if len(deletedKeys) > 0 { 132 if *deleteKeys { 133 if err := db.Del(ctx, deletedKeys...).Err(); err != nil { 134 t.Fatalf("failed to delete keys[%v], err: %v", deletedKeys, err) 135 } 136 } 137 deletedKeys = nil 138 t.Logf("key number: %v ...", count) 139 } 140 141 }) 142 } 143 t.Logf("all key number: %v", count) 144 145 }