當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。