本文整理匯總了Golang中github.com/garyburd/redigo/redis.String函數的典型用法代碼示例。如果您正苦於以下問題:Golang String函數的具體用法?Golang String怎麽用?Golang String使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了String函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestDiscardTransaction
func TestDiscardTransaction(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
s.Set("aap", "noot")
b, err := redis.String(c.Do("MULTI"))
ok(t, err)
equals(t, "OK", b)
b, err = redis.String(c.Do("SET", "aap", "mies"))
ok(t, err)
equals(t, "QUEUED", b)
// Not committed
s.CheckGet(t, "aap", "noot")
v, err := redis.String(c.Do("DISCARD"))
ok(t, err)
equals(t, "OK", v)
// TX didn't get executed
s.CheckGet(t, "aap", "noot")
}
示例2: Register
func (u *User) Register(args *User_RegisterArgs, reply *Reply) error {
if args.Psw == "" {
reply.Code = com.E_AgentPasswordCannotBeNull
} else {
accountkey := k_account_user_ + args.Account
account, _ := redis.String(u.c.Do("GET", accountkey))
// exists, _ := redis.Bool(u.c.Do("EXISTS", accountkey))
// if !exists {
if account == "" {
// create id
u.c.Do("INCR", k_account_count)
id, _ := redis.String(u.c.Do("GET", k_account_count))
// save account
u.c.Do("SET", accountkey, id)
u.c.Do("SADD", k_account_userlist, id)
// save user
userkey := k_user_ + string(id)
u.c.Do("HSET", userkey, k_psw, args.Psw)
u.c.Do("HSET", userkey, k_coin, 0)
u.c.Do("HSET", userkey, k_nickname, "nickname")
reply.Code = com.E_Success
log.Debug("Register success")
} else {
reply.Code = com.E_AgentAccountExist
log.Debug("E_AgentAccountExist")
}
}
return nil
}
示例3: TestLlen
func TestLlen(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
s.Push("l", "aap", "noot", "mies", "vuur")
{
el, err := redis.Int(c.Do("LLEN", "l"))
ok(t, err)
equals(t, 4, el)
}
// Non exising key
{
el, err := redis.Int(c.Do("LLEN", "nonexisting"))
ok(t, err)
equals(t, 0, el)
}
// Wrong type of key
{
_, err := redis.String(c.Do("SET", "str", "value"))
ok(t, err)
_, err = redis.Int(c.Do("LLEN", "str"))
assert(t, err != nil, "LLEN error")
// Too many arguments
_, err = redis.String(c.Do("LLEN", "too", "many"))
assert(t, err != nil, "LLEN error")
}
}
示例4: GetDeviceUsers
func GetDeviceUsers(deviceId int64) ([]int64, error) {
r := Redix[_GetDeviceUsers]
RedixMu[_GetDeviceUsers].Lock()
defer RedixMu[_GetDeviceUsers].Unlock()
user, err := redis.String(r.Do("hget", RedisDeviceUsers, deviceId))
if err != nil {
return nil, err
}
host, err2 := redis.String(r.Do("hget", "user:family", user))
//如果找不到host,說明此用戶是孤兒,那麽隻返回此設備的直接關聯用戶
//如果找到host,就返回此設備直接關聯用戶所屬家庭所有成員
if host == "" || err2 != nil {
bindedIds := make([]int64, 0, 1)
u_id, _ := strconv.ParseInt(user, 10, 64)
bindedIds = append(bindedIds, int64(u_id))
return bindedIds, nil
} else {
mems, _ := redis.Strings(r.Do("smembers", fmt.Sprintf("family:%v", host)))
bindedIds := make([]int64, 0, len(mems))
for _, m := range mems {
u_id, err := strconv.ParseInt(m, 10, 64)
if err == nil {
bindedIds = append(bindedIds, int64(u_id))
}
}
return bindedIds, nil
}
}
示例5: TestRpop
func TestRpop(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
s.Push("l", "aap", "noot", "mies")
// Simple pops.
{
el, err := redis.String(c.Do("RPOP", "l"))
ok(t, err)
equals(t, "mies", el)
el, err = redis.String(c.Do("RPOP", "l"))
ok(t, err)
equals(t, "noot", el)
el, err = redis.String(c.Do("RPOP", "l"))
ok(t, err)
equals(t, "aap", el)
// Last element has been popped. Key is gone.
i, err := redis.Int(c.Do("EXISTS", "l"))
ok(t, err)
equals(t, 0, i)
// Can pop non-existing keys just fine.
v, err := c.Do("RPOP", "l")
ok(t, err)
equals(t, nil, v)
}
}
示例6: AddUser
func AddUser(user *UserInfo) (rs uint32, err error) {
conn := userRedisPool.Get()
defer conn.Close()
var nkey string = fmt.Sprintf("n_%s", user.Name)
var exist bool
var seq int64
if seq, err = redis.Int64(conn.Do("incr", UserIdSeq)); err != nil {
return
}
if exist, err = redis.Bool(conn.Do("exists", nkey)); err != nil {
return
}
if exist {
err = UserExists
return
}
var buf = make([]byte, 4)
binary.LittleEndian.PutUint32(buf, uint32(seq))
if _, err = redis.String(conn.Do("set", nkey, buf)); err != nil {
return
}
rs = uint32(seq)
user.Id = rs
key := fmt.Sprintf("u%d", rs)
var val []byte
if val, err = json.Marshal(user); err != nil {
return
}
if _, err = redis.String(conn.Do("set", key, val)); err != nil {
return
}
return
}
示例7: getJsonChecks
// Return a JSON with the ids and names of the Pingdom checks
func getJsonChecks(redisResponse []interface{}) []byte {
conn := redisPool.Get() // Redis connection to get the names for the ids
checks := make([]CheckType, len(redisResponse))
for k, _ := range redisResponse {
v := ""
redisResponse, _ = redis.Scan(redisResponse, &v)
id, _ := strconv.ParseInt(v, 10, 64)
// Get the name corresponding to the id
n, err := redis.String(conn.Do("GET", "check:"+v))
for {
if err == nil {
break
} else {
log.Printf("Redis error in GET check: %s\n", err)
n, err = redis.String(conn.Do("GET", "check:"+v))
}
}
checks[k] = CheckType{id, n}
}
conn.Close()
b, _ := json.MarshalIndent(Checks{checks}, "", " ")
return b
}
示例8: main
func main() {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
log.Fatal(err)
}
defer c.Close()
// SET command only lets you have one value for the key
c.Do("SET", "MY_KEY", "MY_VALUE")
// HSET lets you have multiple fields and values
c.Do("HSET", "myhash", "field1", "Hello")
// myhash = { field1 : "Hello" }
val1, err := redis.String(c.Do("GET", "MY_KEY"))
if err != nil {
fmt.Println("key not found")
}
fmt.Println(val1)
// MY_VALUE
val2, err := redis.String(c.Do("HGET", "myhash", "field1"))
if err != nil {
fmt.Println("key not found")
}
fmt.Println(val2)
// Hello
}
示例9: QPeek
func (c *client) QPeek(queueName string, count int) ([]job, error) {
conn, err := c.get()
if err != nil {
return nil, err
}
reply, err := redis.Values(conn.Do("QPEEK", queueName, count))
if err != nil {
return nil, err
}
result := make([]job, 0, len(reply))
for _, v := range reply {
if value, err := redis.Values(v, nil); err != nil {
return nil, err
} else {
queueName, err := redis.String(value[0], nil)
id, err := redis.String(value[1], err)
data, err := redis.Bytes(value[2], err)
if err != nil {
return nil, err
}
result = append(result, job{QueueName: queueName, Id: id, Body: data})
}
}
return result, nil
}
示例10: Migrate
func Migrate(addr, toIp string, toPort int, key string, timeout int) (string, error) {
inner := func(addr, toIp string, toPort int, key string, timeout int) (string, error) {
conn, err := dial(addr)
if err != nil {
return "", ErrConnFailed
}
defer conn.Close()
resp, err := redis.String(conn.Do("migrate", toIp, toPort, key, 0, timeout))
if err != nil && strings.Contains(err.Error(), "BUSYKEY") {
log.Warningf("Migrate", "Found BUSYKEY '%s', will overwrite it.", key)
resp, err = redis.String(conn.Do("migrate", toIp, toPort, key, 0, timeout, "replace"))
}
if err != nil {
return "", err
}
return resp, nil
}
retry := NUM_RETRY
var err error
var resp string
for retry > 0 {
resp, err = inner(addr, toIp, toPort, key, timeout)
if err == nil {
return resp, nil
}
retry--
}
return "", err
}
示例11: TestSingleKeyRedisCmd
func TestSingleKeyRedisCmd(t *testing.T) {
InitEnv()
c, err := redis.Dial("tcp", "localhost:19000")
if err != nil {
t.Fatal(err)
}
defer c.Close()
_, err = c.Do("SET", "foo", "bar")
if err != nil {
t.Error(err)
}
if got, err := redis.String(c.Do("get", "foo")); err != nil || got != "bar" {
t.Error("'foo' has the wrong value")
}
_, err = c.Do("SET", "bar", "foo")
if err != nil {
t.Error(err)
}
if got, err := redis.String(c.Do("get", "bar")); err != nil || got != "foo" {
t.Error("'bar' has the wrong value")
}
}
示例12: ClusterFailover
func ClusterFailover(addr string, rs *topo.ReplicaSet) (string, error) {
conn, err := dial(addr)
if err != nil {
return "", ErrConnFailed
}
defer conn.Close()
// 先正常Failover試試,如果主掛了再試試Force
resp, err := redis.String(conn.Do("cluster", "failover"))
if err != nil {
if strings.HasPrefix(err.Error(), "ERR Master is down or failed") {
resp, err = redis.String(conn.Do("cluster", "failover", "force"))
}
if err != nil {
return "", err
}
}
// 30s
for i := 0; i < 30; i++ {
info, err := FetchInfo(addr, "Replication")
if err != nil {
return resp, err
}
if info.Get("role") == "slave" {
time.Sleep(1 * time.Second)
} else {
break
}
}
return resp, nil
}
示例13: RandomFortune
// Return a single random Fortune, from a random module
func RandomFortune(mod string) (*Fortune, error) {
conn := Pool.Get()
defer conn.Close()
// ensure the specified module exists
if mod != "" {
member, err := redis.Bool(conn.Do("SISMEMBER", MODS_KEY, mod))
if err != nil {
return nil, err
}
if member == false {
return nil, errors.New(fmt.Sprintf("module '%s' not found", mod))
}
}
if mod == "" {
mod2, err := redis.String(conn.Do("SRANDMEMBER", MODS_KEY))
if err != nil {
return nil, err
}
mod = mod2
}
fid, err := redis.Int(conn.Do("SRANDMEMBER", modKey(mod)))
if err != nil {
return nil, err
}
text, err := redis.String(conn.Do("GET", fortuneKey(fid)))
if err != nil {
return nil, err
}
return &Fortune{mod: mod, id: fid, text: text}, nil
}
示例14: TestSelect
func TestSelect(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
_, err = redis.String(c.Do("SET", "foo", "bar"))
ok(t, err)
_, err = redis.String(c.Do("SELECT", "5"))
ok(t, err)
_, err = redis.String(c.Do("SET", "foo", "baz"))
ok(t, err)
// Direct access.
got, err := s.Get("foo")
ok(t, err)
equals(t, "bar", got)
s.Select(5)
got, err = s.Get("foo")
ok(t, err)
equals(t, "baz", got)
// Another connection should have its own idea of the db:
c2, err := redis.Dial("tcp", s.Addr())
ok(t, err)
v, err := redis.String(c2.Do("GET", "foo"))
ok(t, err)
equals(t, "bar", v)
}
示例15: TestConnMux
func TestConnMux(t *testing.T) {
c, err := redistest.Dial()
if err != nil {
t.Fatalf("error connection to database, %v", err)
}
m := redisx.NewConnMux(c)
defer m.Close()
c1 := m.Get()
c2 := m.Get()
c1.Send("ECHO", "hello")
c2.Send("ECHO", "world")
c1.Flush()
c2.Flush()
s, err := redis.String(c1.Receive())
if err != nil {
t.Fatal(err)
}
if s != "hello" {
t.Fatalf("echo returned %q, want %q", s, "hello")
}
s, err = redis.String(c2.Receive())
if err != nil {
t.Fatal(err)
}
if s != "world" {
t.Fatalf("echo returned %q, want %q", s, "world")
}
c1.Close()
c2.Close()
}