• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang redis.Dial函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: topDestinations

func topDestinations(rw http.ResponseWriter, req *http.Request) {

	client, err := redis.Dial("tcp", "localhost:6379")
	if err != nil {
		fmt.Println("Problem communicating to Redis...")
		log.Fatal(err)
	} else {
		defer client.Close()
	}

	response := client.Cmd("ZREVRANGE", "popularity", "0", "-1", "WITHSCORES")
	// here we create a byte buffer to construct the html response
	buffer := bytes.NewBufferString("<TABLE>")

	l, _ := response.List()
	for _, elemStr := range l {
		buffer.WriteString("<TR><TD>")
		buffer.WriteString(elemStr)
		buffer.WriteString("<TR><TD>")
	}
	buffer.WriteString("</TABLE>")

	p := loadTopPage(buffer.Bytes())
	renderTemplate(rw, "top", p)

}
开发者ID:ev46,项目名称:dnastat,代码行数:26,代码来源:server.go


示例2: newRedisDriver

func newRedisDriver(c *config) (*redisDriver, error) {
	r, err := redis.Dial("tcp", c.RedisAddress)
	if err != nil {
		return nil, err
	}
	return &redisDriver{c: r}, nil
}
开发者ID:nickwales,项目名称:proxym,代码行数:7,代码来源:driver.go


示例3: TestFailover

// Test a basic manual failover
func TestFailover(t *T) {
	s := getSentinel(t)
	sc, err := redis.Dial("tcp", "127.0.0.1:28000")
	require.Nil(t, err)

	k := randStr()

	c, err := s.GetMaster("test")
	require.Nil(t, err)
	require.Nil(t, c.Cmd("SET", k, "foo").Err)
	s.PutMaster("test", c)

	require.Nil(t, sc.Cmd("SENTINEL", "FAILOVER", "test").Err)

	c, err = s.GetMaster("test")
	require.Nil(t, err)
	foo, err := c.Cmd("GET", k).Str()
	require.Nil(t, err)
	assert.Equal(t, "foo", foo)
	require.Nil(t, c.Cmd("SET", k, "bar").Err)
	s.PutMaster("test", c)

	time.Sleep(10 * time.Second)
	require.Nil(t, sc.Cmd("SENTINEL", "FAILOVER", "test").Err)

	c, err = s.GetMaster("test")
	require.Nil(t, err)
	bar, err := c.Cmd("GET", k).Str()
	require.Nil(t, err)
	assert.Equal(t, "bar", bar)
	s.PutMaster("test", c)
}
开发者ID:MStoykov,项目名称:radix.v2,代码行数:33,代码来源:sentinel_test.go


示例4: TestPublishEvent

func TestPublishEvent(t *testing.T) {
	db := redisdb.New(dburl)
	if err := db.Connect(); err != nil {
		t.Fatalf("Failed to connect to redis: %s", err.Error())
	}

	c, err := redis.Dial("tcp", dburl)
	if err != nil {
		t.Fatalf("Failed to connect to redis2: %s", err.Error())
	}

	ps := pubsub.NewSubClient(c)
	ps.Subscribe("thechan")

	wg := sync.WaitGroup{}
	wg.Add(1)

	go func() {
		resp := ps.Receive()
		if resp.Message != "moar" {
			t.Errorf("Expected moar but got %s", resp.Message)
		}
		wg.Done()
	}()

	go func() {
		buf := bytes.NewBufferString("moar")
		if err := db.PublishEvent("thechan", buf); err != nil {
			t.Fatalf("Should have have errored publishing: %s", err.Error())
		}
	}()
	wg.Wait()
}
开发者ID:zclark,项目名称:wdead,代码行数:33,代码来源:integration_test.go


示例5: TestLuaEval

func TestLuaEval(t *T) {
	c1, err := redis.Dial("tcp", "127.0.0.1:6379")
	require.Nil(t, err)
	c2, err := cluster.New("127.0.0.1:7000")
	require.Nil(t, err)

	cs := []Cmder{c1, c2}

	for _, c := range cs {
		script, key, val := randTestScript()
		s, err := LuaEval(c, script, 1, key, val).Str()
		require.Nil(t, err)
		assert.Equal(t, "OK", s)

		// The second time the command will be hashed
		script, key, val = randTestScript()
		s, err = LuaEval(c, script, 1, key, val).Str()
		require.Nil(t, err)
		assert.Equal(t, "OK", s)

		s, err = c.Cmd("GET", key).Str()
		require.Nil(t, err)
		assert.Equal(t, val, s)
	}
}
开发者ID:MStoykov,项目名称:radix.v2,代码行数:25,代码来源:lua_test.go


示例6: NewRedisStorage

func NewRedisStorage(address string, db int, pass, mrshlerStr string, maxConns int) (*RedisStorage, error) {
	df := func(network, addr string) (*redis.Client, error) {
		client, err := redis.Dial(network, addr)
		if err != nil {
			return nil, err
		}
		if len(pass) != 0 {
			if err = client.Cmd("AUTH", pass).Err; err != nil {
				client.Close()
				return nil, err
			}
		}
		if db != 0 {
			if err = client.Cmd("SELECT", db).Err; err != nil {
				client.Close()
				return nil, err
			}
		}
		return client, nil
	}
	p, err := pool.NewCustom("tcp", address, maxConns, df)
	if err != nil {
		return nil, err
	}
	var mrshler Marshaler
	if mrshlerStr == utils.MSGPACK {
		mrshler = NewCodecMsgpackMarshaler()
	} else if mrshlerStr == utils.JSON {
		mrshler = new(JSONMarshaler)
	} else {
		return nil, fmt.Errorf("Unsupported marshaler: %v", mrshlerStr)
	}
	return &RedisStorage{db: p, ms: mrshler}, nil
}
开发者ID:bhepp,项目名称:cgrates,代码行数:34,代码来源:storage_redis.go


示例7: NewRedisProvider

func NewRedisProvider(network, addr, auth string, idleConns int) (*RedisProvider, error) {
	df := func(n, a string) (*redis.Client, error) {
		c, err := redis.Dial(n, a)
		if err != nil {
			return nil, err
		}

		if err = c.Cmd("AUTH", auth).Err; err != nil {
			c.Close()
			return nil, err
		}

		return c, nil
	}

	p, err := rPool.NewCustom(network, addr, idleConns, df)
	if err != nil {
		return nil, err
	}

	rp := &RedisProvider{
		Pool: p,
	}

	return rp, nil
}
开发者ID:codemodus,项目名称:sessctrl,代码行数:26,代码来源:provider_redis.go


示例8: setupRedis

func setupRedis() {

	c, err := redis.Dial("tcp", "localhost:6379")
	client = c
	if err != nil {
		//handle error
	}
}
开发者ID:gauravkm,项目名称:goshort,代码行数:8,代码来源:main.go


示例9: NewConnection

// NewConnection establishes a new connection to a Redis instance
func NewConnection(host, port string) *redis.Client {

	client, err := redis.Dial("tcp", host+":"+port)
	if err != nil {
		log.Println("Error while connecting:" + err.Error())
		return nil
	}
	return client
}
开发者ID:JKolios,项目名称:GoMarkov,代码行数:10,代码来源:myRedis.go


示例10: Connect

// Connect establishes a redis client connection.
func (db *DB) Connect() error {
	c, err := redis.Dial("tcp", db.addr)
	if err != nil {
		return err
	}

	db.client = c
	return nil
}
开发者ID:zclark,项目名称:wdead,代码行数:10,代码来源:redisdb.go


示例11: initConnection

func initConnection(connDetails connectionDetails) *redis.Client {
	// Connection Establishment

	client, err := redis.Dial("tcp", connDetails.host+":"+connDetails.port)
	if err != nil {
		fmt.Println("Error while connecting:" + err.Error())
		return nil
	}
	return client
}
开发者ID:JKolios,项目名称:GoRed,代码行数:10,代码来源:gored.go


示例12: pingRedis

// Redis is needed to run this test
func pingRedis() *redis.Client {

	redisClient, err := redis.Dial("tcp", getRedisHost())
	if err != nil {
		log.Warnln("Cannot find Redis (standalone) at 'localhost:6379'.")
		log.Infoln("Trying to connect to Redis sentinel ...")

		redisClient2, err2 := redis.Dial("tcp", getRedisSentinelHost())
		if err2 != nil {
			log.Infoln("Please start a local Redis or Redis sentinel.")
			log.Infoln("Please specify TEST_REDIS_HOST or TEST_REDIS_SENTINEL_HOST (and optionally TEST_REDIS_MASTER)")
			panic("Cannot find Redis server.")
		}
		log.Infof("Successfully connected to Redis Sentinel '%s'", redisClient2.Addr)
		return redisClient2
	}
	log.Infof("Successfully connected to Redis '%s'", redisClient.Addr)
	return redisClient
}
开发者ID:frosenberg,项目名称:go-cloud-stream,代码行数:20,代码来源:redis_test.go


示例13: init

func init() {
	flag.IntVar(&redisPort, "port", 0, "Port of the redis server.")
	flag.StringVar(&zset, "zset", "", "Name of the ZSET to store into redis.")

	flag.Parse()

	client, err = redis.Dial("tcp", fmt.Sprintf("localhost:%d", redisPort))
	if err != nil {
		log.Fatal("Couldn't open a connection to the Redis Server, please check that redis-server is running.")
	}
}
开发者ID:simar7,项目名称:crdt,代码行数:11,代码来源:crdt_redis_test.go


示例14: Client

func (r *rutil) Client() *redis.Client {
	if r.cli == nil {
		cli, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", r.Host, r.Port))
		checkErr(err)
		if r.Auth != "" {
			res := cli.Cmd("AUTH", r.Auth)
			checkErr(res.Err)
		}
		r.cli = cli
	}
	return r.cli
}
开发者ID:slavaGanzin,项目名称:rutil,代码行数:12,代码来源:rutil.go


示例15: main

func main() {
	client, err := redis.Dial("tcp", "localhost:6379")
	if err != nil {
		log.Fatalf("Couldn't connect to Redis server")
	}
	defer client.Close()

	val, err := client.Cmd("ZCARD", "myxyht").Int64()
	if err != nil {
		log.Fatalf("Error retrieving value: %s", err)
	}
	fmt.Printf("value = %d", val)
}
开发者ID:rendon,项目名称:gosnippets,代码行数:13,代码来源:radix.go


示例16: main

func main() {
	args := os.Args[1:]

	if len(args) == 0 {
		log.Fatal("Towns db file path is not specified")
	}

	if len(args) == 1 {
		log.Fatal("Cashpoints db file path is not specified")
	}

	if len(args) == 2 {
		log.Fatal("Banks db file path is not specified")
	}

	if len(args) == 3 {
		log.Fatal("Redis database url is not specified")
	}

	townsDbPath := args[0]
	cashpointsDbPath := args[1]
	banksDbPath := args[2]
	redisUrl := args[3]

	townsDb, err := sql.Open("sqlite3", townsDbPath)
	if err != nil {
		log.Fatal(err)
	}
	defer townsDb.Close()

	cashpointsDb, err := sql.Open("sqlite3", cashpointsDbPath)
	if err != nil {
		log.Fatal(err)
	}
	defer cashpointsDb.Close()

	banksDb, err := sql.Open("sqlite3", banksDbPath)
	if err != nil {
		log.Fatal(err)
	}
	defer banksDb.Close()

	redisCli, err := redis.Dial("tcp", redisUrl)
	if err != nil {
		log.Fatal(err)
	}
	defer redisCli.Close()

	migrate(townsDb, cashpointsDb, banksDb, redisCli)
}
开发者ID:RasPavel,项目名称:android_cash_points,代码行数:50,代码来源:server_sqlite_to_redis.go


示例17: NewClientCustom

// NewClientCustom is the same as NewClient, except it takes in a DialFunc which
// will be used to create all new connections to the master instances. This can
// be used to implement authentication, custom timeouts, etc...
func NewClientCustom(
	network, address string, poolSize int, df DialFunc, names ...string,
) (
	*Client, error,
) {

	// We use this to fetch initial details about masters before we upgrade it
	// to a pubsub client
	client, err := redis.Dial(network, address)
	if err != nil {
		return nil, &ClientError{err: err}
	}

	masterPools := map[string]*pool.Pool{}
	for _, name := range names {
		r := client.Cmd("SENTINEL", "MASTER", name)
		l, err := r.List()
		if err != nil {
			return nil, &ClientError{err: err, SentinelErr: true}
		}
		addr := l[3] + ":" + l[5]
		pool, err := pool.NewCustom("tcp", addr, poolSize, (pool.DialFunc)(df))
		if err != nil {
			return nil, &ClientError{err: err}
		}
		masterPools[name] = pool
	}

	subClient := pubsub.NewSubClient(client)
	r := subClient.Subscribe("+switch-master")
	if r.Err != nil {
		return nil, &ClientError{err: r.Err, SentinelErr: true}
	}

	c := &Client{
		poolSize:       poolSize,
		masterPools:    masterPools,
		subClient:      subClient,
		dialFunc:       (pool.DialFunc)(df),
		getCh:          make(chan *getReq),
		putCh:          make(chan *putReq),
		closeCh:        make(chan struct{}),
		alwaysErrCh:    make(chan *ClientError),
		switchMasterCh: make(chan *switchMaster),
	}

	go c.subSpin()
	go c.spin()
	return c, nil
}
开发者ID:zhaozhi406,项目名称:radix.v2,代码行数:53,代码来源:sentinel.go


示例18: main

func main() {

	flag.Parse()

	var err error

	redis_client, err = redis.Dial(*redis_prot, *redis_addr)

	if err != nil {
		fatal("Unable to connect to redis", err)
	}

	go cleaner(redis_client)
	startListening(*address, *port)
}
开发者ID:arsham-f,项目名称:Ceres,代码行数:15,代码来源:main.go


示例19: main

func main() {
	conn, err := redis.Dial("tcp", "localhost:6379")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	keys, err := conn.Cmd("KEYS", "image:cache:*").List()
	if err != nil {
		log.Fatal(err)
	}

	for _, key := range keys {
		conn.Cmd("DEL", key)
	}
}
开发者ID:ququzone,项目名称:image-server,代码行数:16,代码来源:deletecache.go


示例20: jsonTop

// returns the top x addresses, responds to GET <statserver>/topip?limit=x
func jsonTop(w http.ResponseWriter, req *http.Request) {

	number := req.FormValue("limit") // read the limit from the request
	client, err := redis.Dial("tcp", "localhost:6379")
	if err != nil {
		log.Fatal(err)
	} else {
		defer client.Close()
	}

	response := client.Cmd("ZREVRANGEBYSCORE", "popularity", "+inf", "-inf", "WITHSCORES", "LIMIT", "0", number)
	list, _ := response.List()

	encoder := json.NewEncoder(w)
	encoder.Encode(list)
}
开发者ID:ev46,项目名称:dnastat,代码行数:17,代码来源:server.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang redis.Client类代码示例发布时间:2022-05-23
下一篇:
Golang pool.Pool类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap