Skip to content

Instantly share code, notes, and snippets.

@agrison
Created July 23, 2015 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agrison/82e8db5ec30dda4f9d49 to your computer and use it in GitHub Desktop.
Save agrison/82e8db5ec30dda4f9d49 to your computer and use it in GitHub Desktop.
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