当前位置: 首页>>代码示例>>Golang>>正文


Golang redis.Scan函数代码示例

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


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

示例1: ScanMap

func ScanMap(values []interface{}) (map[string]string, error) {
	results := make(map[string]string)
	var err error

	for len(values) > 0 {
		var key string
		var value string

		values, err = redis.Scan(values, &key)

		if err != nil {
			return nil, err
		}

		if len(values) > 0 {
			values, err = redis.Scan(values, &value)

			if err != nil {
				return nil, err
			}

			results[key] = value
		} else {
			fmt.Println("Unable to find value for %s.", key)
			results[key] = ""
		}

	}

	return results, nil
}
开发者ID:nicklenstra-wf,项目名称:trajectory,代码行数:31,代码来源:redis.go

示例2: SlotsInfo

// get redis's slot size
func SlotsInfo(addr string, fromSlot, toSlot int) (map[int]int, error) {
	c, err := redis.DialTimeout("tcp", addr, defaultTimeout, defaultTimeout, defaultTimeout)
	if err != nil {
		return nil, err
	}
	defer c.Close()

	var reply []interface{}
	var val []interface{}

	reply, err = redis.Values(c.Do("SLOTSINFO", fromSlot, toSlot-fromSlot+1))
	if err != nil {
		return nil, err
	}

	ret := make(map[int]int)
	for {
		if reply == nil || len(reply) == 0 {
			break
		}
		if reply, err = redis.Scan(reply, &val); err != nil {
			return nil, err
		}
		var slot, keyCount int
		_, err := redis.Scan(val, &slot, &keyCount)
		if err != nil {
			return nil, err
		}
		ret[slot] = keyCount
	}
	return ret, nil
}
开发者ID:luxuan,项目名称:codis-pkg-comment,代码行数:33,代码来源:redis_utils.go

示例3: SlotsInfo

// get redis's slot size
func SlotsInfo(addr string, fromSlot int, toSlot int, auth string) (map[int]int, error) {
	c, err := newRedisConn(addr, auth)
	if err != nil {
		return nil, errors.Trace(err)
	}
	defer c.Close()

	var (
		reply []interface{}
		val   []interface{}
	)

	reply, err = redis.Values(c.Do("SLOTSINFO", fromSlot, toSlot-fromSlot+1))
	if err != nil {
		return nil, errors.Trace(err)
	}

	ret := map[int]int{}
	for {
		if reply == nil || len(reply) == 0 {
			break
		}
		if reply, err = redis.Scan(reply, &val); err != nil {
			return nil, errors.Trace(err)
		}
		var slot, keyCount int
		_, err := redis.Scan(val, &slot, &keyCount)
		if err != nil {
			return nil, errors.Trace(err)
		}
		ret[slot] = keyCount
	}

	return ret, nil
}
开发者ID:XMAN-WOMBAT,项目名称:reborn,代码行数:36,代码来源:redis_utils.go

示例4: SendOfflineMsg

func SendOfflineMsg(client *Client, appid string) {
	// connect to Redis
	redisConn, err := redis.Dial("tcp", ":6379")
	if err != nil {
		log.Printf("Dial redix error: %s", err.Error())
		return
	}

	// get the timestamp
	var reply []interface{}
	reply, err = redis.Values(redisConn.Do("TIME"))
	if err != nil {
		log.Printf("Error on TIME: %s", err.Error())
		return
	}
	var current_time int64
	_, err = redis.Scan(reply, &current_time)

	// get offline message for each App on this device
	key := "broadcast_msg:" + appid
	reply, err = redis.Values(redisConn.Do("ZRANGE", key, 0, -1))
	if err != nil {
		log.Printf("Error on ZRANGE: %s", err.Error())
		goto Out
	}
	for len(reply) > 0 {
		var msg_id int64
		reply, err = redis.Scan(reply, &msg_id)
		if err != nil {
			log.Printf("Error on Scan ZRANGE reply: %s", err.Error())
			goto Out
		}
		log.Printf("offline msg_id: %d", msg_id)
		key = "msg:" + strconv.FormatInt(msg_id, 10)
		var reply_msg []interface{}
		reply_msg, err = redis.Values(redisConn.Do("HMGET", key, "msg", "expire_time"))
		if err != nil {
			log.Printf("Error on HMGET: %s", err.Error())
			goto Out
		}
		var msg string
		var expire_time int64
		_, err = redis.Scan(reply_msg, &msg, &expire_time)
		if err != nil {
			log.Printf("Error on Scan HMGET reply: %s", err.Error())
			goto Out
		}
		//log.Printf("expire_time: %d, msg: %s", expire_time, msg)
		if expire_time > current_time {
			// message hasn't expired, need to send it
			client.SendMsg(msg, appid)
		}
	}

Out:
	redisConn.Close()
}
开发者ID:zhengzhiren,项目名称:pushserver,代码行数:57,代码来源:handlers.go

示例5: getGroupMembers

func getGroupMembers(gid uint32) []uint64 {
	members := make([]uint64, 0, 3)
	if userRedisPool != nil {
		var err error
		var reply []interface{}
		conn := userRedisPool.Get()
		defer conn.Close()
		if reply, err = redis.Values(conn.Do("sdump", fmt.Sprintf("%dFT", gid))); err != nil {
			ERROR.Println(err)
			return members
		}
		if len(reply) == 0 {
			return members
		}
		var l int
		var bs []byte
		if _, err = redis.Scan(reply, &l, &bs); err != nil {
			ERROR.Println(err)
			return members
		}
		for i := 0; i < len(bs); i += l {
			var val uint64
			val = uint64(binary.LittleEndian.Uint16(bs[i:]))
			members = append(members, val)
		}
	}
	return members
}
开发者ID:Joinhack,项目名称:peony,代码行数:28,代码来源:user.go

示例6: ValidateCurrentMaster

func ValidateCurrentMaster() error {
	c, err := redis.Dial("tcp", GetSentinel())
	if err != nil {
		return err
	}

	reply, err := redis.Values(c.Do("SENTINEL", "masters"))

	if err != nil {
		return err
	}

	var sentinel_info []string

	reply, err = redis.Scan(reply, &sentinel_info)
	if err != nil {
		return err
	}
	master_name := sentinel_info[1]
	ip := sentinel_info[3]
	port := sentinel_info[5]

	err = SwitchMaster(master_name, ip, port)

	return err
}
开发者ID:huayl,项目名称:smitty,代码行数:26,代码来源:agent.go

示例7: Incr

// Incr increments the specified key. If the key did not exist, it sets it to 1
// and sets it to expire after the number of seconds specified by window.
//
// It returns the new count value and the number of remaining seconds, or an error
// if the operation fails.
func (r *redisStore) Incr(key string, window time.Duration) (int, int, error) {
	conn := r.pool.Get()
	defer conn.Close()
	if err := selectDB(r.db, conn); err != nil {
		return 0, 0, err
	}
	// Atomically increment and read the TTL.
	conn.Send("MULTI")
	conn.Send("INCR", r.prefix+key)
	conn.Send("TTL", r.prefix+key)
	vals, err := redis.Values(conn.Do("EXEC"))
	if err != nil {
		conn.Do("DISCARD")
		return 0, 0, err
	}
	var cnt, ttl int
	if _, err = redis.Scan(vals, &cnt, &ttl); err != nil {
		return 0, 0, err
	}
	// If there was no TTL set, then this is a newly created key (INCR creates the key
	// if it didn't exist), so set it to expire.
	if ttl == -1 {
		ttl = int(window.Seconds())
		_, err = conn.Do("EXPIRE", r.prefix+key, ttl)
		if err != nil {
			return 0, 0, err
		}
	}
	return cnt, ttl, nil
}
开发者ID:WIZARD-CXY,项目名称:golang-devops-stuff,代码行数:35,代码来源:redis.go

示例8: Scan

func (s *RedisStore) Scan(partition string) <-chan *bucket.Bucket {
	retBuckets := make(chan *bucket.Bucket)
	go func(out chan *bucket.Bucket) {
		rc := s.redisPool.Get()
		defer rc.Close()
		defer close(out)
		rc.Send("MULTI")
		rc.Send("SMEMBERS", partition)
		rc.Send("DEL", partition)
		reply, err := redis.Values(rc.Do("EXEC"))
		if err != nil {
			fmt.Printf("at=%q error=%s\n", "bucket-store-scan", err)
			return
		}
		var delCount int64
		var members []string
		redis.Scan(reply, &members, &delCount)
		for _, member := range members {
			id, err := bucket.ParseId(member)
			if err != nil {
				fmt.Printf("at=%q error=%s\n", "bucket-store-parse-key", err)
				continue
			}
			out <- &bucket.Bucket{Id: id}
		}
	}(retBuckets)
	return retBuckets
}
开发者ID:raphweiner,项目名称:l2met,代码行数:28,代码来源:redis_store.go

示例9: TestMget

func (s *testProxyRouterSuite) TestMget(c *C) {
	cc := s.testDialConn(c, proxyAddr, proxyAuth)
	defer cc.Close()

	const count = 20480
	keys := make([]interface{}, count)
	for i := 0; i < count; i++ {
		s := strconv.Itoa(i)
		keys[i] = s
		_, err := cc.Do("SET", s, s)
		c.Assert(err, IsNil)
	}

	reply, err := redis.Values(cc.Do("MGET", keys...))
	c.Assert(err, IsNil)

	temp := make([]string, count)
	values := make([]interface{}, count)

	for i := 0; i < count; i++ {
		values[i] = &temp[i]
	}

	_, err = redis.Scan(reply, values...)
	c.Assert(err, IsNil)

	for i := 0; i < count; i++ {
		c.Assert(keys[i], Equals, temp[i])
	}

	s.s1.store.Reset()
	s.s2.store.Reset()
}
开发者ID:cuiwm,项目名称:reborn,代码行数:33,代码来源:router_test.go

示例10: Work

// Work runs an infinite loop, watching its database for new requests, starting job as requested,
// moving stream data back and forth, and updating job status as it changes.
func (w *Worker) Work() error {
	conn := w.pool.Get()
	defer conn.Close()
	for {
		Debugf("Waiting for job")
		// Get the list of current jobs
		// Wait for next start event
		vals, err := redis.Values(conn.Do("BLPOP", w.KeyPath("start"), "0"))
		if err != nil {
			return err
		}
		var id string
		if _, err := redis.Scan(vals[1:], &id); err != nil {
			return err
		}
		Debugf("Received instruction to start job %s", id)
		// Acquire lock on the job
		acquired, err := redis.Bool(conn.Do("SETNX", w.KeyPath(id), "me"))
		if err != nil {
			return err
		}
		Debugf("Acquiring lock for job %s... -> %s", id, acquired)
		// FIXME: set a dead man's switch with TTL & a periodic refresh
		if acquired {
			Debugf("Spawning goroutine for job %s", id)
			go func(id string) {
				if err := w.startJob(id); err != nil {
					fmt.Fprintf(os.Stderr, "Error starting job %s: %s\n", id, err)
				}
			}(id)
		}
	}
}
开发者ID:pombredanne,项目名称:beam,代码行数:35,代码来源:worker.go

示例11: key2Mode

func (c *RedisCache) key2Mode(key string, typ reflect.Type, val reflect.Value) error {
	conn := c.ConnGet()
	defer conn.Close()
	conn.Send("MULTI")
	vals := []interface{}{}
	timeField := []int{}
	for i := 0; i < typ.NumField(); i++ {
		conn.Send("HGET", key, typ.Field(i).Name)
		switch val.Field(i).Interface().(type) {
		case time.Time:
			timeField = append(timeField, i)
			var str string
			vals = append(vals, &str)
		default:
			vals = append(vals, val.Field(i).Addr().Interface())
		}
	}

	reply, err := redis.Values(conn.Do("EXEC"))
	if err != nil {
		return err
	}
	if _, err := redis.Scan(reply, vals...); err == nil {
		var n int
		for _, n = range timeField {
			if time, e := time.Parse(time.RFC1123Z, string(reply[n].([]byte))); e == nil {
				val.Field(n).Set(reflect.ValueOf(time))
			}
		}
		return nil
	} else {
		return err
	}
}
开发者ID:hujunlong,项目名称:orm,代码行数:34,代码来源:cache_driver_redis.go

示例12: createStatus

// creates a status hash structure and returns it's status id.
func createStatus(message string, uid int, c redis.Conn) (int, error) {
	var login string
	var sid int
	defer c.Close()

	c.Do("MULTI")
	// get login name for user with id
	c.Do("HGET", "user:"+strconv.Itoa(uid), "login")
	// get the incremented global status count
	c.Do("INCR", "status:id")
	// reply contains both the login for the uid and the global status count
	reply, err := redis.Values(c.Do("EXEC"))

	if err != nil {
		return -1, err
	}
	// scan reply into the local variables
	if _, err := redis.Scan(reply, &login, &sid); err != nil {
		return -1, err
	}
	// set all the appropriate values in the hash store
	if _, err := c.Do("HMSET", "status:"+strconv.Itoa(sid), "message", message,
		"posted", time.Now().Unix(), "id", sid, "uid", uid, "login", login); err != nil {
		return -1, err
	}
	// increment the user's post count
	if _, err := c.Do("HINCRBY", "user:"+strconv.Itoa(uid), "posts", 1); err != nil {
		return -1, err
	}

	return sid, nil
}
开发者ID:yungyikim,项目名称:simple,代码行数:33,代码来源:db.go

示例13: GetWithTime

// GetWithTime returns the value of the key if it is in the store
// or -1 if it does not exist. It also returns the current time at
// the redis server to microsecond precision.
func (r *RedigoStore) GetWithTime(key string) (int64, time.Time, error) {
	var now time.Time

	key = r.prefix + key

	conn, err := r.getConn()
	if err != nil {
		return 0, now, err
	}
	defer conn.Close()

	conn.Send("TIME")
	conn.Send("GET", key)
	conn.Flush()
	timeReply, err := redis.Values(conn.Receive())
	if err != nil {
		return 0, now, err
	}

	var s, us int64
	if _, err := redis.Scan(timeReply, &s, &us); err != nil {
		return 0, now, err
	}
	now = time.Unix(s, us*int64(time.Microsecond))

	v, err := redis.Int64(conn.Receive())
	if err == redis.ErrNil {
		return -1, now, nil
	} else if err != nil {
		return 0, now, err
	}

	return v, now, nil
}
开发者ID:CometKim,项目名称:platform,代码行数:37,代码来源:redigostore.go

示例14: getChecks

func (sr *Runner) getChecks(maxChecks int, timeout int) []stalker.Check {
	log.Debugln("Getting checks off queue")
	checks := make([]stalker.Check, 0)
	expireTime := time.Now().Add(3 * time.Second).Unix()
	for len(checks) <= maxChecks {
		//we've exceeded our try time
		if time.Now().Unix() > expireTime {
			break
		}
		rconn := sr.rpool.Get()
		defer rconn.Close()
		res, err := redis.Values(rconn.Do("BLPOP", sr.conf.workerQueue, timeout))
		if err != nil {
			if err != redis.ErrNil {
				log.Errorln("Error grabbing check from queue:", err.Error())
				break
			} else {
				log.Debugln("redis result:", err)
				continue
			}
		}
		var rb []byte
		res, err = redis.Scan(res, nil, &rb)
		var check stalker.Check
		if err := json.Unmarshal(rb, &check); err != nil {
			log.Errorln("Error decoding check from queue to json:", err.Error())
			break
		}
		checks = append(checks, check)
	}
	return checks
}
开发者ID:pandemicsyn,项目名称:stalker,代码行数:32,代码来源:runner.go

示例15: Info

// Info returns information about the lock.
func (l *RedisLock) Info() (*LockInfo, error) {
	var owner, data string
	var expire int

	l.client.conn.Send("MULTI")
	l.client.conn.Send("GET", l.key())
	l.client.conn.Send("PTTL", l.key())
	l.client.conn.Send("GET", l.dataKey())
	reply, err := redis.Values(l.client.conn.Do("EXEC"))

	if err == redis.ErrNil {
		return &LockInfo{l.name, false, "", time.Duration(0), ""}, nil
	}
	if err != nil {
		return nil, err
	}

	_, err = redis.Scan(reply, &owner, &expire, &data)
	if err != nil {
		return nil, err
	}

	ttl := time.Duration(expire) * time.Millisecond

	return &LockInfo{
		Name:     l.name,
		Acquired: ttl > 0,
		Owner:    owner,
		TTL:      ttl,
		Data:     data,
	}, nil
}
开发者ID:gbagnoli,项目名称:glock,代码行数:33,代码来源:driver_redis.go


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