func spawnConsumer() error { var err error var consumer *redis.Client consumer = redis.New() err = consumer.Connect(host, port) if err != nil { log.Fatalf("Consumer failed to connect: %s\n", err.Error()) return err } log.Println("Consumer connected to redis-server.") rec := make(chan []string) log.Printf("Waiting for rec channel. Ctrl + C to quit.\n") go consumer.Subscribe(rec, "channel") var ls []string for { ls = <-rec log.Printf("Consumer received: %v\n", strings.Join(ls, ", ")) } consumer.Quit() return nil }
func spawnPublisher() error { var err error var publisher *redis.Client publisher = redis.New() err = publisher.Connect(host, port) if err != nil { log.Fatalf("Publisher failed to connect: %s\n", err.Error()) return err } log.Println("Publisher connected to redis-server.") log.Println("Publishing some messages...") publisher.Publish("channel", "Hello world!") publisher.Publish("channel", "Do you know how to count?") for i := 0; i < 3; i++ { publisher.Publish("channel", i) } log.Printf("Closing publisher.\n") publisher.Quit() return nil }
func redisGetHandler(w http.ResponseWriter, r *http.Request) { var client *redis.Client var err error client = redis.New() err = client.Connect("localhost", 6379) if err != nil { fmt.Fprintf(w, "/connect fail/") return } defer client.Quit() key := r.FormValue("key") s, err := client.Get(key) if err != nil { fmt.Fprintf(w, "/values: is empty%s/", s) return } fmt.Fprintf(w, "/values:%s/", s) }
func redisSetHandler(w http.ResponseWriter, r *http.Request) { value := r.FormValue("value") key := r.FormValue("key") fmt.Fprintf(w, "/set key:%s,value:%s/", key, value) var err error var client *redis.Client client = redis.New() err = client.Connect("localhost", 6379) if err != nil { fmt.Fprintf(w, "/connect fail/") return } defer client.Quit() client.Set(key, value) if err != nil { fmt.Fprintf(w, "/set values: is empty%s/") return } fmt.Fprintf(w, "/set key:%s,value:%s/", key, value) }