Exemplo n.º 1
0
func dump_db(port int, db int, output io.Writer) {
	var client redis.Client

	if port != 0 {
		client.Addr = "127.0.0.1:" + strconv.Itoa(port)
	}

	if db != 0 {
		client.Db = db
	}

	fmt.Fprintf(output, "FLUSHDB\r\n")

	keys, err := client.Keys("*")

	if err != nil {
		println("Redis-dump failed", err.Error())
		return
	}

	for _, key := range keys {
		typ, _ := client.Type(key)

		if typ == "string" {
			data, _ := client.Get(key)
			fmt.Fprintf(output, "SET %s %d\r\n%s\r\n", key, len(data), data)
		} else if typ == "list" {
			llen, _ := client.Llen(key)
			for i := 0; i < llen; i++ {
				data, _ := client.Lindex(key, i)
				fmt.Fprintf(output, "RPUSH %s %d\r\n%s\r\n", key, len(data), data)
			}
		} else if typ == "set" {
			members, _ := client.Smembers(key)
			for _, data := range members {
				fmt.Fprintf(output, "SADD %s %d\r\n%s\r\n", key, len(data), data)
			}
		}
	}

}