Example #1
0
func GetLastLogs(userId string, serverId int, channel string, lastLogId int64, count int) ([]*IRCLog, error) {
	r := pool.Get()
	defer r.Close()

	key := fmt.Sprintf("log:%s:%d:%s", userId, serverId, strings.ToLower(channel))

	if count == 0 {
		count = 30
	}
	var min string
	if lastLogId != -1 {
		min = "-inf"
	} else {
		min = fmt.Sprintf("(%d", lastLogId)
	}
	reply, err := redis.Values(r.Do("ZREVRANGEBYSCORE", key, "+inf", min, "LIMIT", 0, count))
	if err != nil {
		return nil, err
	}

	logs := make([]*IRCLog, len(reply))
	for i := 0; i < len(reply); i++ {
		data, err := redis.Bytes(reply[i], nil)
		if err != nil {
			return nil, err
		}
		var log IRCLog
		err = GobDecode(data, &log)
		if err != nil {
			return nil, err
		}
		logs[len(logs)-1-i] = &log
	}
	return logs, nil
}
func RedisLoadWithConn(r redis.Conn, v interface{}) (err error) {
	switch l := v.(type) {
	case RedisLoader:
		err = l.RedisLoad(r)
	case RedisInterface:
		data, err := redis.Bytes(r.Do("GET", l.GetKey()))
		if err != nil {
			return err
		}
		err = GobDecode(data, l)
	default:
		err = fmt.Errorf("Unsupperted Type!")
	}
	return
}
func (v *RedisSlice) RedisLoad(r redis.Conn) error {
	elemType := reflect.Indirect(reflect.New(v.eType))
	switch elemType.Interface().(type) {
	case RedisInterface:
		reply, err := redis.Values(r.Do("SORT", v.key, "GET", "*"))
		if err != nil {
			return err
		}
		newVal := reflect.MakeSlice(v.sType, len(reply), len(reply))
		for i, data := range reply {
			elem := reflect.New(v.eType.Elem())
			data, err := redis.Bytes(data, nil)
			if err != nil {
				return nil
			}
			err = GobDecode(data, elem.Interface())
			if err != nil {
				return err
			}
			newVal.Index(i).Set(elem)
		}
		v.slice.Set(newVal)
	default:
		reply, err := redis.Values(r.Do("SMEMBERS", v.key))
		if err != nil {
			return err
		}
		newVal := reflect.MakeSlice(v.sType, len(reply), len(reply))
		eType := v.eType
		if eType.Kind() == reflect.Ptr {
			eType = eType.Elem()
		}
		for i := 0; len(reply) > 0; i++ {
			elem := reflect.New(v.eType)
			reply, err = redis.Scan(reply, elem.Interface())
			if err != nil {
				return err
			}
			if eType.Kind() != reflect.Ptr {
				elem = elem.Elem()
			}
			newVal.Index(i).Set(elem)
		}
		v.slice.Set(newVal)
	}
	return nil
}