Ejemplo n.º 1
0
func save(r redis.AsyncClient, key string, obj interface{}, w http.ResponseWriter) {
	var b bytes.Buffer

	z := zlib.NewWriter(&b)
	defer z.Close()

	je := json.NewEncoder(z)

	err := je.Encode(obj)
	if err != nil {
		log.Fatal("Failed to json Encode with error: ", err)
	}
	z.Flush()

	f, rerr := r.Set(key, b.Bytes())
	if rerr != nil {
		panic(rerr)
	}
	_, rerr, timeout := f.TryGet(50000000000)
	if rerr != nil {
		panic(rerr)
	}
	if timeout {
		savetimeout++
		log.Println("save timeout! count: ", savetimeout)
		fmt.Fprintf(w, "Save failed for %s", key)
	}
}
Ejemplo n.º 2
0
func primeKey(key string, r redis.AsyncClient) {
	path := "document.json"
	file, err := os.Open(path)
	if err != nil {
		panic(err)
	}
	reader := bufio.NewReader(file)
	document, _ := ioutil.ReadAll(reader)
	var b bytes.Buffer
	z := zlib.NewWriter(&b)
	z.Write(document)
	z.Close()
	f, rerr := r.Set(key, b.Bytes())
	if rerr != nil {
		panic(rerr)
	}
	_, rerr, timeout := f.TryGet(50000000000)
	if rerr != nil {
		panic(rerr)
	}
	if timeout {
		savetimeout++
		log.Println("save timeout! count: ", savetimeout)
	}
}
Ejemplo n.º 3
0
func load(r redis.AsyncClient, k string, w http.ResponseWriter) (obj interface{}) {
	f, rerr := r.Get(k)
	if rerr != nil {
		panic(rerr)
	}
	val, rerr, timeout := f.TryGet(50000000000)
	if rerr != nil {
		panic(rerr)
	}
	if timeout {
		loadtimeout++
		log.Println("load timeout! count: ", loadtimeout)
		fmt.Fprintf(w, "Save failed for %s", key)
		return
	}
	setPromise, srerr := r.Set(key, val)
	if srerr != nil {
		panic(rerr)
	}
	_, grerr, timeout := setPromise.TryGet(50000000000)
	if grerr != nil {
		panic(rerr)
	}
	if timeout {
		savetimeout++
		log.Println("save timeout! count: ", savetimeout)
		fmt.Fprintf(w, "Save failed for %s", key)
	}
	return
}
Ejemplo n.º 4
0
func doSet(id string, signal chan int, client redis.AsyncClient, cnt int) {
	key := "set-" + id
	value := []byte("foo")
	for i := 0; i < cnt; i++ {
		client.Set(key, value)
	}
	signal <- 1
}