github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/store/redis/helper.go (about) 1 package redis 2 3 import ( 4 "context" 5 "strings" 6 ) 7 8 // extracts node name from key 9 // /nodestatus/nodename -> nodename 10 func extractNodename(s string) string { 11 ps := strings.Split(s, "/") 12 return ps[len(ps)-1] 13 } 14 15 func parseStatusKey(key string) (string, string, string, string) { 16 parts := strings.Split(key, "/") 17 l := len(parts) 18 return parts[l-4], parts[l-3], parts[l-2], parts[l-1] 19 } 20 21 // getByKeyPattern gets key-value pairs that key matches pattern 22 func (r *Rediaron) getByKeyPattern(ctx context.Context, pattern string, limit int64) (map[string]string, error) { 23 var ( 24 cursor uint64 25 result []string 26 err error 27 count int64 28 keys = []string{} 29 ) 30 for { 31 result, cursor, err = r.cli.Scan(ctx, cursor, pattern, 0).Result() 32 if err != nil { 33 return nil, err 34 } 35 36 keys = append(keys, result...) 37 count += int64(len(result)) 38 if cursor == 0 || (limit > 0 && count >= limit) { 39 break 40 } 41 } 42 if limit > 0 && int64(len(keys)) >= limit { 43 keys = keys[:limit] 44 } 45 return r.GetMulti(ctx, keys) 46 }