本文整理汇总了Golang中github.com/mediocregopher/radix/v2/redis.Dial函数的典型用法代码示例。如果您正苦于以下问题:Golang Dial函数的具体用法?Golang Dial怎么用?Golang Dial使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Dial函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
}
示例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
}
示例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)
}
示例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()
}
示例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)
}
}
示例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
}
示例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
}
示例8: setupRedis
func setupRedis() {
c, err := redis.Dial("tcp", "localhost:6379")
client = c
if err != nil {
//handle error
}
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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.")
}
}
示例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
}
示例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)
}