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 }
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 }
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 }
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 }
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 }
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.")) } }
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 }
/* 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 }
/** * 缓存一定时间 */ 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 }
// 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 }
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 }
// 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 }
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 }
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) }
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) }
func (b *redisBackend) Restore() { c := redisPool.Get() ctx := b.ctx defer c.Close() log.Info("redis: restoring ", b.queue) t0 := time.Now() for { if time.Since(t0).Seconds() > 5 { log.Info("redis: restoring ...") t0 = time.Now() } reply, err := redis.Bytes(c.Do("RPOP", b.queue)) if err != nil { ctx.restoreDone <- true return } for { if ctx.buffering { log.Debug("we're buffering, wait restoring..") time.Sleep(time.Second) } else { break } } ctx.parsedLines <- &reply } }
func GetService(key string) ([]byte, error) { pool := NewPool() c := pool.Get() result, err := redis.Bytes(c.Do("GET", key)) return result, err }
func (this *RedisStore) Poll(c chan []byte, queue string) error { go func() { consumer, _ := this.GetConn() defer this.CloseConn(consumer) var err error for { consumer, err = this.GetConn() if err != nil { time.Sleep(time.Millisecond * 100) continue } message, err := redis.Bytes(consumer.Do("LPOP", queue)) this.CloseConn(consumer) if err == nil && len(message) > 0 { c <- message } else { time.Sleep(this.pollingFreq) } } }() return nil }
func (store *RedisStore) Get(id string) (*Session, error) { var isNew bool = true var values = map[interface{}]interface{}{} if len(id) > 0 { c := store.pool.Get() buf, err := redis.Bytes(c.Do("GET", id)) c.Close() if err != nil && err != redis.ErrNil { return nil, err } if buf != nil { values, err = store.codec.Decode(buf) if err != nil { return nil, err } isNew = false } } if isNew { id = GenerateID() } s := NewSession(id, store, store.config.MaxAge) s.Values = values return s, nil }
// getGobItem searches cache by given key from redis and returns Item data func (c *RedisCache) getGobItem(key string) (*eurekache.Item, bool) { conn, err := c.conn() if err != nil { return nil, false } defer conn.Close() data, err := conn.Do("GET", c.prefix+key) if err != nil { return nil, false } b, err := redis.Bytes(data, err) if err != nil { return nil, false } var item eurekache.Item dec := gob.NewDecoder(bytes.NewBuffer(b)) err = dec.Decode(&item) if err != nil { return nil, false } return &item, true }
func findSession(w http.ResponseWriter, r *http.Request, rc redis.Conn) (*Session, error) { session := new(Session) usertokenCookie, err := r.Cookie("usertoken") if err != nil { return session, lwutil.NewErr(err) } usertoken := usertokenCookie.Value //redis if rc == nil { rc = redisPool.Get() defer rc.Close() } sessionBytes, err := redis.Bytes(rc.Do("get", fmt.Sprintf("sessions/%s", usertoken))) if err != nil { return session, lwutil.NewErr(err) } err = json.Unmarshal(sessionBytes, session) lwutil.CheckError(err, "") //update session dt := time.Now().Sub(session.Born) if dt > sessionUpdateSecond*time.Second { newSession(w, session.Userid, session.Username, session.Appid, rc) } return session, nil }
func (rs *redisBackend) Read(id string) (*Session, error) { conn := rs.db.Get() defer conn.Close() data, err := redis.Bytes(conn.Do("GET", rs.key(id))) if err != nil { return nil, err } s := new(Session) if err = json.Unmarshal(data, s); err != nil { // try to read profileID as string ss := new(sessionString) if err = json.Unmarshal(data, ss); err != nil { return nil, err } profileID, err := strconv.Atoi(ss.ProfileID) if err != nil { return nil, err } s.ProfileID = profileID } s.Id = id return s, nil }
// load sbf header from redis func LoadHeader(conn redis.Conn, refer string) (*SBFHeader, error) { if ret, err := redis.Bytes(conn.Do("GETRANGE", refer, 0, SBF_HEADER_SIZE-1)); err == nil { if len(ret) > 0 { header := new(SBFHeader) copy(header.Name[:], ret[0:3]) copy(header.Version[:], ret[3:8]) // from bytes to number if err := BytesToNumber(ret[8:10], &header.Count); err != nil { return nil, err } if err := BytesToNumber(ret[10:12], &header.FullRate); err != nil { return nil, err } if err := BytesToNumber(ret[12:14], &header.SliceCount); err != nil { return nil, err } if err := BytesToNumber(ret[14:18], &header.SliceSize); err != nil { return nil, err } header.Refer = refer return header, nil } else { return nil, errors.New(fmt.Sprintf("SBF %s NOT FOUND.", refer)) } } else { return nil, err } }
func (this *HashMap) GetObject(k string, clazz interface{}) error { b, err := redis.Bytes(Do("HGET", this.Name, k)) if err != nil { return err } return json.Unmarshal(b, clazz) }
// Get returns the response corresponding to key if present. func (c cache) Get(key string) (resp []byte, ok bool) { item, err := redis.Bytes(c.Do("GET", cacheKey(key))) if err != nil { return nil, false } return item, true }
// Pops a job off the queue. func (q *Queue) Pop(count int) ([]*Job, error) { if count == 0 { count = 1 } reply, err := redis.Bytes(q.cli.Do("qless", 0, "pop", timestamp(), q.Name, workerName(), count)) if err != nil { return nil, err } //"{}" if len(reply) == 2 { return nil, nil } //println(string(reply)) var jobs []*Job err = json.Unmarshal(reply, &jobs) if err != nil { return nil, err } for _, v := range jobs { v.cli = q.cli } return jobs, nil }
// Get returns a key value func Get(key string) ([]byte, error) { conn := redisPool.Get() defer conn.Close() channel := channel(key) return redis.Bytes(conn.Do("GET", channel.id())) }
// Http request handler for posting and getting high scores. func HandleScore(w http.ResponseWriter, req *http.Request) { var out string // Connect to the data source. conn := pool.Get() defer conn.Close() outVal, err := redis.Bytes(conn.Do("GET", "highScore:value")) if err != nil { return } highScore, err := strconv.ParseFloat(string(outVal), 64) if err != nil { return } if req.Method == "GET" { // Retrieve the high score from the server and format it as json. if err == nil { currentHigh := HighScore{float64(highScore), 0} marshaled, _ := json.Marshal(currentHigh) out = string(marshaled) } } else if req.Method == "POST" { postedScore, err := strconv.ParseFloat(req.FormValue("scorePosted"), 64) if err == nil && postedScore > highScore { conn.Do("SET", "highScore:value", postedScore) } } io.WriteString(w, out) }
func (task *FlushTask) readValuesFromRedis(items []dbproto.FlushItem) ([][]byte, error) { // TODO: // use MGET for performace count := len(items) values := make([][]byte, count) var err error var conn redis.Conn for i := 0; i < count; i++ { redis_key := items[i].GetMsgType() + "_" + items[i].GetKey() conn = task.getRedisConn(items[i].GetKey()) values[i], err = redis.Bytes(conn.Do("GET", redis_key)) if err != nil { logger.Printf("Read Key %s but ERROR: redis: %v", redis_key, err) values[i] = nil // if no data from redis, just ignore if err == redis.ErrNil { continue } // if other errors, return return nil, err } } return values, nil }
func TestSet(t *testing.T) { assert := assert.New(t) key := "key" pool := helper.TestGetPool() c := NewRedisCache(pool) c.SetPrefix(testRedisPrefix) err := c.Set(key, "valueTestSet") assert.NoError(err) // get data b, err := pool.Get().Do("GET", testRedisPrefix+key) assert.NoError(err) b, err = redis.Bytes(b, err) assert.NoError(err) buf := bytes.NewBuffer(b.([]byte)) dec := gob.NewDecoder(buf) item := &eurekache.Item{} err = dec.Decode(&item) assert.NoError(err) assert.Equal("valueTestSet", item.Value) // delete data err = c.Set(key, nil) assert.NoError(err) b, err = pool.Get().Do("GET", testRedisPrefix+key) assert.NoError(err) assert.Nil(b) }