Beispiel #1
0
func main() {
	var err error

	publisher = redis.New()

	err = publisher.Connect(host, port)

	if err != nil {
		log.Fatalf("Publisher failed to connect: %s\n", err.Error())
		return
	}

	log.Println("Publisher connected to redis-server.")

	consumer = redis.New()

	err = consumer.Connect(host, port)

	if err != nil {
		log.Fatalf("Consumer failed to connect: %s\n", err.Error())
		return
	}

	log.Println("Consumer connected to redis-server.")

	rec := make(chan []string)

	log.Printf("Consumer is now inside a go-routine.\n")
	go consumer.Subscribe(rec, "channel")

	log.Printf("Publishing in another go-routine\n")

	go func() {
		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("Waiting for rec channel. Ctrl + C to quit.\n")
	var ls []string

	for {
		ls = <-rec
		log.Printf("Consumer received: %v\n", strings.Join(ls, ", "))
	}

	consumer.Quit()
	publisher.Quit()

}
Beispiel #2
0
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
}
Beispiel #3
0
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
}
Beispiel #4
0
func main() {
	var s string
	var err error

	client = redis.New()

	err = client.Connect(host, port)

	if err != nil {
		log.Fatalf("Connect failed: %s\n", err.Error())
		return
	}

	log.Println("Connected to redis-server.")

	log.Printf("Sending PING...\n")
	s, err = client.Ping()

	if err != nil {
		log.Fatalf("Could not ping: %s\n", err.Error())
		return
	}

	log.Printf("Received %s!\n", s)

	client.Quit()

}
Beispiel #5
0
func newRedisStore(redis_host string, redis_port uint) *RedisStore {

	return &RedisStore{
		ClientsKey,
		PageKey,

		redis_host,
		redis_port,

		redisPool{
			connections: make(chan *redis.Client, 6),
			maxIdle:     6,

			connFn: func() (*redis.Client, error) {
				client := redis.New()
				err := client.Connect(redis_host, redis_port)

				if err != nil {
					log.Printf("Redis connect failed: %s\n", err.Error())
					return nil, err
				}

				return client, nil
			},
		},
	}

}
Beispiel #6
0
func main() {
	var err error

	client = redis.New()

	err = client.Connect(host, port)

	if err != nil {
		log.Fatalf("Connect failed: %s\n", err.Error())
		return
	}

	log.Println("Connected to redis-server.")

	log.Printf("DEL mylist")
	client.Del("mylist")

	for i := 0; i < 10; i++ {
		log.Printf("RPUSH mylist %d\n", i*2)
		client.RPush("mylist", i*2)
	}

	log.Printf("LRANGE mylist 0 5")
	var ls []string
	ls, err = client.LRange("mylist", 0, 5)

	for i := 0; i < len(ls); i++ {
		log.Printf("> mylist[%d] = %s\n", i, ls[i])
	}

	client.Quit()

}
Beispiel #7
0
func main() {
	var (
		host     string
		password string
		port     uint
	)

	flag.StringVar(&host, "host", "", "Redis host")
	flag.StringVar(&password, "password", "", "Redis password")
	flag.UintVar(&port, "port", 0, "Redis port")

	flag.Parse()

	if host == "" {
		log.Fatal("Host not defined")
	}

	if port == 0 {
		log.Fatal("Port not defined")
	}

	client := *redis.New()
	err := client.Connect(host, port)
	if err != nil {
		log.Fatalf("Cannot connect to redis: %s", err)
	}

	fmt.Println("{}")
}
Beispiel #8
0
func NewCommander(host string, port uint, rkey string) *Commander {
	c := redis.New()
	err := c.Connect(host, port)
	return &Commander{host: host,
		port: port,
		rkey: rkey,
		cli:  c,
		err:  err}
}
Beispiel #9
0
func init() {
	client = redis.New()
	err := client.Connect("localhost", 6379)

	if err != nil {
		log.Println(err)
		fmt.Println("Failed to connect to the Redis Database")
	}
}
Beispiel #10
0
func NewRedisDal() (*RedisDal, error) {
	client := redis.New()
	err := client.Connect(HOST, PORT)
	//	client.HS
	if err != nil {
		return nil, err
	}

	return &RedisDal{client}, nil
}
Beispiel #11
0
func newRedisConn() *redis.Client {
	r := redis.New()
	err := r.Connect(Settings.String("redis_host", "127.0.0.1"), uint(Settings.Int("redis_port", 6379)))
	if err != nil {
		stderr.Fatalf("Redis failed to initialize: %s.", err)
	}

	// Select our DB.
	_, err = r.Select(int64(Settings.Int("redis_db", 0)))
	if err != nil {
		stderr.Fatalf("Redis failed to select DB: %s.", err)
	}

	return r
}
Beispiel #12
0
func (this *RedisStore) Subscribe(c chan []string, channel string) (*redis.Client, error) {
	consumer := redis.New()
	err := consumer.ConnectNonBlock(this.server, this.port)
	if err != nil {
		return nil, err
	}

	if _, err := consumer.Ping(); err != nil {
		return nil, err
	}

	go consumer.Subscribe(c, channel)
	<-c // ignore subscribe command

	return consumer, nil
}
Beispiel #13
0
func main() {
	var err error

	client = redis.New()

	err = client.Connect(host, port)

	if err != nil {
		log.Fatalf("Connect failed: %s\n", err.Error())
		return
	}

	log.Println("Connected to redis-server.")

	// DEL hello
	log.Printf("DEL hello\n")
	client.Command(nil, "DEL", "hello")

	// SET hello 1
	log.Printf("SET hello 1\n")
	client.Command(nil, "SET", "hello", 1)

	// INCR hello
	log.Printf("INCR hello\n")
	client.Command(nil, "INCR", "hello")

	// GET hello
	var i int
	log.Printf("GET hello\n")
	err = client.Command(&i, "GET", "hello")

	if err != nil {
		log.Fatalf("Could not GET: %s\n", err.Error())
		return
	}

	log.Printf("> hello = %d\n", i)

	client.Quit()
}
Beispiel #14
0
func main() {
	var s string
	var err error

	client = redis.New()

	err = client.Connect(host, port)

	if err != nil {
		log.Fatalf("Connect failed: %s\n", err.Error())
		return
	}

	log.Println("Connected to redis-server.")

	// DEL hello
	log.Printf("DEL hello\n")
	client.Del("hello")

	// SET hello 1
	log.Printf("SET hello 1\n")
	client.Set("hello", 1)

	// INCR hello
	log.Printf("INCR hello\n")
	client.Incr("hello")

	// GET hello
	log.Printf("GET hello\n")
	s, err = client.Get("hello")

	if err != nil {
		log.Fatalf("Could not GET: %s\n", err.Error())
		return
	}

	log.Printf("> hello = %s\n", s)

	client.Quit()
}
Beispiel #15
0
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)
}
Beispiel #16
0
func TestGosexyRedisConnect(t *testing.T) {
	var s string
	var err error

	gosexyRedisClient = redis.New()

	err = gosexyRedisClient.ConnectWithTimeout(host, port, time.Second*1)

	if err != nil {
		t.Fatalf("Connect failed: %v", err)
	}

	s, err = gosexyRedisClient.Ping()

	if err != nil {
		t.Fatalf("Command failed: %v", err)
	}

	if s != "PONG" {
		t.Fatalf("Failed")
	}
}
Beispiel #17
0
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)
}