skip to Main Content

With go-redis I am using iterator to scan a large number of keys like below

scanIterr := client.Scan(0, "*", 1000).Iterator()
for scanIterr.Next() {
    key := scanIterr.Val()
    fmt.Printf("%vn", key)
    if key == keyLookingFor {
        kyeFound = true
        return
    }
}

I tried to do the exact same with valkey-go but ended up with following code

res := client.Do(ctx, client.B().Scan().Cursor(0).Match("*").Count(1000).Build())
se, _ := resss.AsScanEntry()
for _, key := range se.Elements {
    fmt.Printf("%vn", key)
    if key == keyLookingFor {
        kyeFound = true
        return
    }
}

I don’t think this valkey code will read all the keys like the one with redis. Also the valkey varient reads all the keys at once but the go-redis code reads in count sized batches.

Can someone suggest the right valkey-go alternative for the code with go-redis?

2

Answers


  1. Chosen as BEST ANSWER

    With some help figured out a way to scan for all keys with required batch-count.

    var cursor uint64 = 0
    for {
         resss := client.Do(ctx, client.B().Scan().Cursor(cursor).Match("*").Count(2).Build())
         se, err := resss.AsScanEntry()
         if err != nil {
             fmt.Printf("client.Scan failed %vn", err)
         }
         for _, sitem := range se.Elements {
             fmt.Printf("Scaned Item::: %vn", sitem)
         }
         cursor = se.Cursor
         if cursor == 0 {
             break
         }
    }
    

  2. I’m a maintainer of ValKey-Glide, part of ValKey org.
    First, go to ValKey-Go and open an issue, I believe the community will put effort to implement what missing.

    Moreover, this one is important for me, 🙂 ValKey-Glide is soon will go Beta with glide for Go, and by Feb/March we will go GA.
    If you would like to be a Beta user, we would love to hear! And I recommend staying tuned for the GA, I think Glide will become the golden standard of the clients.

    If this is something which is significant for you to have, please open an issue at Glide repo as well, even if you use another client.
    We highly appreciate and looking to get users’ needs, on top of what we bring from many years of working on clients.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search