当前位置: 首页>>代码示例>>Golang>>正文


Golang redis.New函数代码示例

本文整理汇总了Golang中menteslibres/net/gosexy/redis.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestPoolClose

func TestPoolClose(t *testing.T) {
	conn := redis.New()
	err := conn.Connect("localhost", 6379)
	if err != nil {
		t.Errorf("pool.Close test failed: could not get connection")
	}

	if len(redisStore.pool.connections) != 0 {
		t.Errorf("pool.Close test failed: connection pool not empty")
	}
	redisStore.pool.Close(conn)

	if len(redisStore.pool.connections) != 1 {
		t.Errorf("pool.Close test failed: connection pool length expected to be 1 got %v", len(redisStore.pool.connections))
	}

	conn = redis.New()
	err = conn.Connect("localhost", 6379)
	if err != nil {
		t.Errorf("pool.Close test failed: could not get connection")
	}

	redisStore.pool.Close(conn)
	if len(redisStore.pool.connections) != 1 { //testing maxIdle; maxIdle = 1
		t.Errorf("pool.Close test failed: connection pool length expected to be 1 got %s", len(redisStore.pool.connections))
	}
}
开发者ID:gleez,项目名称:incus,代码行数:27,代码来源:redis_store_test.go

示例2: main

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.ConnectNonBlock(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("Publisher will send messages 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("Reading subscription...\n")
	var ls []string

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

	log.Printf("Done, quitting.\n")

	consumer.Quit()
	publisher.Quit()

}
开发者ID:carriercomm,项目名称:redis-3,代码行数:55,代码来源:main.go

示例3: spawnConsumer

func spawnConsumer() error {
	var err error
	var consumer *redis.Client

	consumer = redis.New()

	err = consumer.ConnectNonBlock(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("Consumer will read exactly 6 messages before quitting.\n")

	go consumer.Subscribe(rec, "channel")

	var ls []string

	for j := 0; j < 6; j++ {
		ls = <-rec
		log.Printf("Consumer received message[%d]: %v\n", j, strings.Join(ls, ", "))
	}

	log.Printf("Closing consumer...\n")

	consumer.Quit()

	return nil
}
开发者ID:carriercomm,项目名称:redis-3,代码行数:34,代码来源:main.go

示例4: main

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()

}
开发者ID:AidHamza,项目名称:redis,代码行数:33,代码来源:main.go

示例5: spawnPublisher

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
}
开发者ID:carriercomm,项目名称:redis-3,代码行数:30,代码来源:main.go

示例6: NewQueue

// Attempts to create a new named queue.
func NewQueue(name string) (self *Queue, err error) {
	self = &Queue{}

	self.client = redis.New()

	host := os.Getenv("REDIS_SERVER")
	if host == "" {
		host = "127.0.0.1"
	}

	port, _ := strconv.Atoi(os.Getenv("REDIS_PORT"))
	if port == 0 {
		port = redis.DefaultPort
	}

	err = self.client.ConnectWithTimeout(host, redis.DefaultPort, connectTimeout)
	if err != nil {
		return nil, err
	}

	self.name = name
	self.queueName = fmt.Sprintf("%s-queue", self.name)

	return self, nil
}
开发者ID:xingskycn,项目名称:queue,代码行数:26,代码来源:main.go

示例7: client

func (r RedisConnection) client() *redis.Client {
	rc := redis.New()
	err := rc.ConnectNonBlock(r.Host, r.Port)
	if err != nil {
		log.Fatalf("FATAL RedisConnection.client(): %s", err)
	}

	return rc
}
开发者ID:ashmckenzie,项目名称:rpi-moteino-collector,代码行数:9,代码来源:main.go

示例8: rhost_copy

func rhost_copy(r *Redis_Server) (*Redis_Server, error) {
	rnew := new(Redis_Server)
	rnew.r = redis.New()
	rnew.host = r.host
	rnew.port = r.port
	rnew.db = r.db
	rnew.pass = r.pass
	return rnew, nil
}
开发者ID:jjangg96,项目名称:redis-transfer,代码行数:9,代码来源:transfer.go

示例9: init

func init() {
	rand.Seed(time.Now().UnixNano())
	client = redis.New()
	err := client.Connect("localhost", 6379)
	if err != nil {
		log.Fatalf("Connect failed: %s\n", err.Error())
		return
	}

	log.Println("Connected to redis-server.")
}
开发者ID:yuriadams,项目名称:shorter,代码行数:11,代码来源:url.go

示例10: main

func main() {
	var client *redis.Client

	client = redis.New()
	client.Connect("localhost", 6379)

	s, _ := client.Ping()
	fmt.Println(s)
	client.Incr("hello")
	s, _ = client.Get("hello")
	fmt.Println(s)
	client.Quit()
}
开发者ID:panjianbo,项目名称:gosrc,代码行数:13,代码来源:redis.go

示例11: TestPooltestConn

func TestPooltestConn(t *testing.T) {
	conn := redis.New()
	err := conn.Connect("localhost", 6379)
	err = redisStore.pool.testConn(conn)
	if err != nil {
		t.Fatalf("testConn test failed: %s", err.Error())
	}

	conn.Quit()
	err = redisStore.pool.testConn(conn)
	if err == nil {
		t.Fatalf("testConn test failed: expected error got nil")
	}
}
开发者ID:gleez,项目名称:incus,代码行数:14,代码来源:redis_store_test.go

示例12: main

func main() {
	var err error

	client = redis.New()

	err = client.Connect(host, port)
	if err != nil {
		log.Printf(err.Error())
	}

	compact := "HELLO"
	max := len(compact)

	client.Multi()
	client.Del("keywordlookup")
	client.Del("keywordlookup2")

	for i := 1; i <= max; i++ {
		s := compact[:i]
		client.ZAdd("keywordlookup", 0, s)
		client.ZAdd("keywordlookup2", 0, s)
	}

	client.ZInterStore("keywordmatch", 2, "keywordlookup", "keywordlookup2")
	client.ZRevRange("keywordmatch", 0, 0)
	res, err := client.Exec()

	if err != nil {
		log.Printf(err.Error())
	}

	for _, el := range res {
		switch v := el.(type) {
		case []interface{}:
			log.Printf("Got []interface{}")
			for i, vv := range v {
				log.Printf("Value at index %d (%v) has kind %s, we convert it to string: %v", i, vv, reflect.TypeOf(vv).Kind(), to.String(vv))
			}
		case interface{}:
			log.Printf("Got value of kind %s (%v), we convert it to string: %s\n", reflect.TypeOf(v).Kind(), v, to.String(v))
		}
	}

	client.Quit()

}
开发者ID:janceChun,项目名称:redis,代码行数:46,代码来源:main.go

示例13: main

func main() {
	var rc = redis.New()
	var err error
	if err = rc.Connect("localhost", 6379); err != nil {
		log.Fatalf("Couldn't connect to redis server.")
	}

	if _, err = rc.HSet("key", "field", "value"); err != nil {
		log.Fatalf("Error setting value: %s", err)
	}

	var value string
	if value, err = rc.HGet("key", "field"); err != nil {
		log.Fatalf("Error getting value: %s", err)
	}

	fmt.Printf("Value = %s\n", value)
}
开发者ID:rendon,项目名称:gosnippets,代码行数:18,代码来源:gosexy_redis.go

示例14: main

func main() {
	client = redis.New()
	defer client.Quit()
	err := client.Connect(host, port)
	if err != nil {
		println(err.Error())
		return
	}
	println("connect to redis server")
	//example ping
	println("sending ping")
	s, err = client.Ping()
	if err != nil {
		println("Ping error")
		return
	}
	println("received " + s)
	//end of example ping

	//example set get
	println("set Xargin")
	client.Set("Xargin", 1)
	s, err = client.Get("Xargin")
	if err != nil {
		println("get Xargin error")
		return
	}
	println("get Xargin = " + s)
	//end of exmplae set get

	//example incr
	println("increment Xargin")
	client.Incr("Xargin")
	s, err = client.Get("Xargin")
	if err != nil {
		println("get Xargin err")
		return
	}
	println("get Xargin = " + s)
	//end
	log.Println("end")
}
开发者ID:cch123,项目名称:test,代码行数:42,代码来源:redistest.go

示例15: GetRedisClient

func GetRedisClient() *redis.Client {

	if RedisClientPool == nil {
		RedisClientPool = make(chan *redis.Client, MAX_POOL_SIZE)
	}
	if len(RedisClientPool) == 0 {
		go func() {
			for i := 0; i < MAX_POOL_SIZE; i++ {
				client := redis.New()
				err := client.Connect("localhost", 6379)
				if err != nil {
					panic(err)
				}
				PutRedisClient(client)
			}

		}()
	}
	return <-RedisClientPool
}
开发者ID:panjianbo,项目名称:gosrc,代码行数:20,代码来源:redis_pool.go


注:本文中的menteslibres/net/gosexy/redis.New函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。