本文整理匯總了Golang中github.com/garyburd/redigo/redis.Bytes函數的典型用法代碼示例。如果您正苦於以下問題:Golang Bytes函數的具體用法?Golang Bytes怎麽用?Golang Bytes使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Bytes函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Register_lua_db_CachePool_Do
func Register_lua_db_CachePool_Do(L *lua.LState) int {
ud := L.CheckUserData(1)
cmd := L.CheckString(2)
arg1 := L.CheckString(3)
var value []byte
var err error
if v, ok := ud.Value.(*db.CachePool); ok {
if L.GetTop() == 4 {
arg2 := L.CheckString(4)
value, err = redis.Bytes(v.Do(cmd, arg1, arg2))
} else {
value, err = redis.Bytes(v.Do(cmd, arg1))
}
}
if err == nil {
L.Push(lua.LString(string(value)))
L.Push(lua.LString(""))
} else {
if err != nil {
L.Push(lua.LString(""))
L.Push(lua.LString(err.Error()))
} else {
L.Push(lua.LString(""))
L.Push(lua.LString("not string type value"))
}
logger.Error("Register_lua_db_CachePool_Do Error : %v, %v", value, err)
}
return 2
}
示例2: GetUserByName
func GetUserByName(name string) (user *UserInfo, err error) {
conn := userRedisPool.Get()
defer conn.Close()
key := fmt.Sprintf("n_%s", name)
var val []byte
if val, err = redis.Bytes(conn.Do("get", key)); err != nil {
if err == redis.ErrNil {
err = UserNotExist
return
}
return
}
uid := binary.LittleEndian.Uint32(val)
idkey := fmt.Sprintf("u%d", uid)
if val, err = redis.Bytes(conn.Do("get", idkey)); err != nil {
if err == redis.ErrNil {
err = UserNotExist
return
}
return
}
var u UserInfo
if err = json.Unmarshal(val, &u); err != nil {
return
}
user = &u
return
}
示例3: ReadPairs
func ReadPairs(reply interface{}) (vals map[string]string, res interface{}, err error) {
rows, ok := reply.([]interface{})
if !ok {
fmt.Println("Result not an []interface{}.")
return vals, reply, err
}
ln := len(rows)
if ln%2 != 0 {
fmt.Println("len(rows) is not even: ", ln)
return vals, reply, err
}
count := ln / 2
vals = make(map[string]string)
err = nil
for i := 0; i < count; i++ {
n := i * 2
k, err := redis.Bytes(rows[n], err)
v, err := redis.Bytes(rows[n+1], err)
if err != nil {
fmt.Println("Couldn't convert via redis.Bytes(row)")
}
vals[string(k)] = string(v)
}
return vals, reply, err
}
示例4: Queues
func (c *Client) Queues(name string) ([]*Queue, error) {
args := []interface{}{0, "queues", timestamp()}
if name != "" {
args = append(args, name)
}
byts, err := redis.Bytes(c.Do("qless", args...))
if err != nil {
return nil, err
}
qr := []*Queue{NewQueue(c)}
if name == "" {
err = json.Unmarshal(byts, &qr)
for _, q := range qr {
q.cli = c
}
} else {
err = json.Unmarshal(byts, &qr[0])
}
if err != nil {
return nil, err
}
return qr, err
}
示例5: GetState
func (problem Problem) GetState(user models.User) (*ProblemState, error) {
redisC := models.RedisPool.Get()
defer redisC.Close()
log.WithFields(log.Fields{
"user_id": user.Id,
"problem_name": problem.Name,
}).Info("Getting state in Redis")
stateBytes, err := redis.Bytes(redisC.Do("GET", problem.GetKeyForUser(user)))
if err == redis.ErrNil {
return &ProblemState{Status: StatusStarting, StartedAt: time.Now(), EndingAt: time.Now().Add(problem.SolvingTime)}, nil
}
if err != nil {
return nil, err
}
var state ProblemState
pCache := bytes.NewBuffer(stateBytes)
decCache := gob.NewDecoder(pCache)
gob.Register(ReverseData{})
err = decCache.Decode(&state)
if err != nil {
return nil, err
}
return &state, nil
}
示例6: activateUser
func (p *portalAPI) activateUser(res http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
activationKey := vars["key"]
r := p.a.redis.Get()
actJson, err := redis.Bytes(r.Do("GET", "activation:"+activationKey))
if err != nil {
if err == redis.ErrNil {
abort(res, 403, "Invalid or expired activation code.")
return
} else {
abort(res, 500, err.Error())
}
}
act := activationData{}
json.Unmarshal(actJson, &act)
if act.Email == "" {
abort(res, 500, "Invalid activation data.")
return
}
if p.m.ActivateUser(act.Email) != nil {
abort(res, 500, "Could not activate account: %s", err.Error())
return
}
r.Do("DELETE", "activation:"+activationKey)
if act.After != "" {
http.Redirect(res, req, act.After, 302)
} else {
res.WriteHeader(http.StatusOK)
res.Write([]byte("Activation successful. Please return to the login page and log in."))
}
}
示例7: GetMaterial
func (this *ReqRedisModule) GetMaterial(ads *[]context.AdInfo) (err error) {
utils.DebugLog.Write("start to get material, ads len[%d]", len(*ads))
var ids []int64
for i := 0; i < len(*ads); i++ {
ids = append(ids, (*ads)[i].Adid)
}
var ans []interface{}
utils.DebugLog.Write("request is [%s]", ids)
ans, err = this.query(ids)
if err != nil {
utils.WarningLog.Write("request redis fail . err[%s]", err.Error())
return
}
var material_tmp jesgoo_protocol.Material
for i := 0; i < len(ans); i++ {
tp, err := redis.Bytes(ans[i], nil)
if err != nil {
utils.WarningLog.Write("read redis return fail err[%s]", err.Error())
continue
}
err = proto.Unmarshal(tp, &material_tmp)
if err != nil {
utils.FatalLog.Write("parse material fail . adid[%d]", (*ads)[i].Adid)
continue
}
err = this.fill_material(&(*ads)[i], &material_tmp)
if err != nil {
utils.FatalLog.Write("fill material fail . adid[%d] err[%s]", (*ads)[i].Adid, err.Error())
}
// utils.DebugLog.Write("get material . [%s]", tp)
}
err = nil
return
}
示例8: Get
/*
Get the session for this request from Redis
*/
func (sh *SessionHolder) Get(c web.C, r *http.Request) (*base.Session, error) {
sessionId := sh.GetSessionId(r)
if sessionId == "" {
return nil, base.ErrorSessionNotFound
}
conn := c.Env["redis"].(redigo.Conn)
sess, err := conn.Do("GET", sessionKey(sessionId))
if err != nil {
return nil, err
}
if sess == nil {
return nil, base.ErrorSessionNotFound
}
sessionBytes, err := redigo.Bytes(sess, err)
if err != nil {
return nil, err
}
dec := gob.NewDecoder(bytes.NewReader(sessionBytes))
var session base.Session
err = dec.Decode(&session)
if err == nil {
session.SetId(sessionId)
}
return &session, err
}
示例9: GetCachedEx
/**
* 緩存一定時間
*/
func (this *RedisPool) GetCachedEx(res interface{}, key string, expire int, f RedisCommondRunner) error {
c := this.RedisPool().Get()
// defer c.Close()
reply, err := redis.Bytes(c.Do("GET", key))
if err == nil && reply != nil {
c.Close()
return json.Unmarshal(reply, res)
}
if err = f(); err != nil {
c.Close()
return err
}
var buf []byte
if buf, err = json.Marshal(res); err != nil {
c.Close()
return err
}
_, err = c.Do("SETEX", key, expire, buf)
if err != nil {
log.Println("REDIS SETEX", key, "failed", err)
}
c.Close()
return nil
}
示例10: verifyMaster
// verifyMaster verifies that the decided master node has fully transitioned
func verifyMaster(addr, pass string) error {
// connect to redis in order to verify its state
r, err := redis.DialURL("redis://"+addr, redis.DialConnectTimeout(config.TimeoutNotReady), redis.DialPassword(pass))
if err != nil {
return fmt.Errorf("Failed to reach redis at: '%v'", addr)
}
// give redis some time to transition
timeout := time.After(config.TimeoutMasterWait)
for {
select {
case <-timeout:
return fmt.Errorf("Timed out waiting for redis to transition to master")
default:
// retrieve the redis node's role
info, err := redis.Bytes(r.Do("INFO", "replication"))
if err != nil {
return fmt.Errorf("Failed to get INFO - %v", err)
}
// check if node is master
if strings.Contains(string(info), "role:master") {
return nil
}
}
}
// cleanup after ourselves
r.Close()
return nil
}
示例11: Deq
func Deq(pool *redis.Pool, latch *utee.Throttle, uid interface{}) ([]byte, error) {
c := pool.Get()
defer c.Close()
defer latch.Release()
for {
name := qname(uid)
k, err := redis.String(c.Do("LPOP", name))
if err != nil && err != redis.ErrNil {
continue
}
if len(k) == 0 {
break
}
b, err := redis.Bytes(c.Do("GET", k))
if err != nil && err != redis.ErrNil {
continue
}
if b != nil {
c.Send("DEL", k)
continue
}
}
i++
if i%10000 == 0 {
log.Println("@success:", i)
}
return nil, nil
}
示例12: Do
// Do executes function f for each document in the database.
func (db *Database) Do(f func(*doc.Package, []Package) error) error {
c := db.Pool.Get()
defer c.Close()
keys, err := redis.Values(c.Do("KEYS", "pkg:*"))
if err != nil {
return err
}
for _, key := range keys {
p, err := redis.Bytes(c.Do("HGET", key, "gob"))
if err == redis.ErrNil {
continue
}
if err != nil {
return err
}
p, err = snappy.Decode(nil, p)
if err != nil {
return err
}
var pdoc doc.Package
if err := gob.NewDecoder(bytes.NewReader(p)).Decode(&pdoc); err != nil {
return err
}
pkgs, err := db.getSubdirs(c, pdoc.ImportPath, &pdoc)
if err != nil {
return err
}
if err := f(&pdoc, pkgs); err != nil {
return err
}
}
return nil
}
示例13: Read
func (engine *RedisPoolEngine) Read(key string) (data []byte, err error) {
c := pool.Get()
defer c.Close()
data, err = redis.Bytes(c.Do("GET", key))
return data, err
}
示例14: gobm
func gobm(c redis.Conn, m map[string]interface{}) {
gob.Register(time.Time{})
var buffer bytes.Buffer
encoder := gob.NewEncoder(&buffer)
err := encoder.Encode(m)
if err != nil {
panic(err)
}
c.Send("MULTI")
c.Send("SET", "drugs:1", buffer.String())
c.Send("EXPIRE", "drugs:1", "5")
r, err := c.Do("EXEC")
if err != nil {
panic(err)
}
r, err = c.Do("GET", "drugs:1")
v, err := redis.Bytes(r, err)
if err != nil {
panic(err)
}
m2 := make(map[string]interface{})
buffer2 := bytes.NewBuffer(v)
decoder := gob.NewDecoder(buffer2)
err = decoder.Decode(&m2)
if err != nil {
panic(err)
}
// fmt.Printf("%#+v", m2)
}
示例15: kmsgpackm
func kmsgpackm(c redis.Conn, m map[string]interface{}) {
mp := &kmsgpack.Coder{}
b, err := mp.Marshal(m)
if err != nil {
panic(err)
}
c.Send("MULTI")
c.Send("SET", "drugs:1", string(b))
c.Send("EXPIRE", "drugs:1", "5")
r, err := c.Do("EXEC")
if err != nil {
panic(err)
}
r, err = c.Do("GET", "drugs:1")
v, err := redis.Bytes(r, err)
if err != nil {
panic(err)
}
m2 := make(map[string]interface{})
err = mp.Unmarshal(v, &m2)
if err != nil {
panic(err)
}
// fmt.Printf("%#+v", m2)
}