dtbam62840
2015-07-17 10:24
浏览 143

从redis中获取所有密钥

How to get all keys of Redis in db and store it in list or array in golang using redigo?

redisPool := redis.NewPool(func() (redis.Conn, error) {
    con, err := redis.Dial("tcp", *redisAddress)
    con.Do("SELECT", 0)
    if err != nil {
        return nil, err
    }
    return con, err
}, *maxConnections)
fmt.Println("Redis Connection Established...!")
con := redisPool.Get()

//defer con.Close()
fmt.Println("Redis Connected...!")
//var sl []string = make([]string, len, cap)
var ab interface{}
ab, errA := con.Do("Keys", "*")
fmt.Println(ab)
fmt.Println(errA)

图片转代码服务由CSDN问答提供 功能建议

如何使用Redigo在db中获取Redis的所有键并将其存储在golang中的列表或数组中?

  redisPool:= redis.NewPool(func()(redis.Conn,error){
 con,err:= redis.Dial(“ tcp”,* redisAddress)
 con  .Do(“ SELECT”,0)
如果err!= nil {
 return nil,err 
} 
 return con,err 
},* maxConnections)
fmt.Println(“ Redis建立连接。  。!“)
con:= redisPool.Get()
 
 //延迟con.Close()
fmt.Println(” Redis Connected ...!“)
 // var sl [] string = make  ([],len,cap)
var ab interface {} 
ab,errA:= con.Do(“ Keys”,“ *”)
fmt.Println(ab)
fmt.Println(errA)
 <  / code>  
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doxd96148 2015-07-17 12:23
    已采纳

    Use the Strings function to convert the result to a slice of strings:

    keys, err := redis.Strings(cn.Do("KEYS", "*"))
    if err != nil {
        // handle error
    }
    for _, key := range keys {
       fmt.Println(key)
    }
    
    点赞 打赏 评论
  • douji4948 2019-04-22 15:02

    Since Redis only has one thread, the KEYS command will block all other requests until it has finished, so it's not a good approach for production. Instead, use SCAN. see SCAN documentation here

    点赞 打赏 评论

相关推荐 更多相似问题