Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample Go app to mass import 1M keys in redis
package main
import (
"fmt"
"log"
"net"
"runtime"
"time"
"github.com/garyburd/redigo/redis"
"github.com/twinj/uuid"
)
const numKeys = 1000000
var uuids [numKeys]string
var client redis.Conn
func prepareUUids() {
for i := 0; i < numKeys; i++ {
uuids[i] = uuid.NewV4().String()
}
}
func createClient() {
c, err := net.Dial("unix", "/tmp/redis.sock")
if err != nil {
log.Fatal(err)
return
}
client = redis.NewConn(c, 10*time.Second, 10*time.Second)
}
func massImport() {
for i := 0; i < numKeys; i++ {
client.Send("SET", uuids[i], uuids[i])
}
client.Flush()
}
func closeClient() {
client.Close()
}
func main() {
runtime.GOMAXPROCS(8)
prepareUUids()
createClient()
start := time.Now()
massImport()
elapsed := time.Since(start)
fmt.Println("Took ", elapsed)
closeClient()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment