Beispiel #1
0
func noteIDs(db redis.Conn) []int64 {
	var ids []int64
	rawIDs, err := redis.Values(db.Do("LRANGE", "notable:notes", 0, -1))
	check(err)

	redis.ScanSlice(rawIDs, &ids)

	return ids
}
// zpop pops a value from the ZSET key using WATCH/MULTI/EXEC commands.
func zpop(c redis.Conn, key string) (result string, err error) {

	defer func() {
		// Return connection to normal state on error.
		if err != nil {
			c.Do("DISCARD")
		}
	}()

	// Loop until transaction is successful.
	for {
		if _, err := c.Do("WATCH", key); err != nil {
			return "", err
		}

		members, err := redis.Strings(c.Do("ZRANGE", key, 0, 0))
		if err != nil {
			return "", err
		}
		if len(members) != 1 {
			return "", redis.ErrNil
		}

		c.Send("MULTI")
		c.Send("ZREM", key, members[0])
		queued, err := c.Do("EXEC")
		if err != nil {
			return "", err
		}

		if queued != nil {
			result = members[0]
			break
		}
	}

	return result, nil
}
Beispiel #3
0
func fetchNote(db redis.Conn, id int64) Note {
	noteAsJSON, err := redis.String(db.Do("GET", noteKey(id)))
	check(err)

	return deserialize(noteAsJSON)
}