Skip to content

Instantly share code, notes, and snippets.

@agrison agrison/foo.go
Created Jul 23, 2015

Embed
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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.