Пример #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
}
Пример #2
0
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
}
func LoadDb() {
	r := common.DefaultRedisPool().Get()
	defer r.Close()

	// 서버 목록들을 읽어와서 IRCClient를 생성하고 접속
	reply, err := redis.Values(r.Do("KEYS", "servers:*"))
	if err != nil {
		log.Println("LoadDb: ", err)
		os.Exit(1)
	}
	for _, key := range reply {
		key, _ := redis.String(key, nil)
		var servers []*common.IRCServer
		err = common.RedisSliceLoad(key, &servers)
		if err != nil {
			log.Println("LoadDb: ", err)
			os.Exit(1)
		}
		for _, server := range servers {
			server.Active = false
			ircMgr.register <- NewClient(server)
		}
	}

	// 채널 목록을 읽어와서 채널을 추가
	reply, err = redis.Values(r.Do("KEYS", "channels:*"))
	if err != nil {
		log.Println("LoadDb: ", err)
		os.Exit(1)
	}
	for _, key := range reply {
		key, _ := redis.String(key, nil)
		var channels []*common.IRCChannel
		err = common.RedisSliceLoad(key, &channels)
		if err != nil {
			log.Println("LoadDb: ", err)
			os.Exit(1)
		}
		for _, channel := range channels {
			c := ircMgr.GetClient(channel.UserId, channel.ServerId)
			channel.Joined = false
			c.AddChannel(channel.Name)
		}
	}
}