func (v *IRCLog) RedisSave(r redis.Conn) error { data, err := GobEncode(v) if err != nil { return err } _, err = r.Do("ZADD", v.GetKey(), v.LogId, data) return err }
func RedisRemoveWithConn(r redis.Conn, v interface{}) (err error) { switch rm := v.(type) { case RedisRemover: err = rm.RedisRemove(r) case RedisInterface: _, err = r.Do("DEL", rm.GetKey()) default: err = fmt.Errorf("Unsupported Type!") } return }
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 RedisSaveWithConn(r redis.Conn, v interface{}) (err error) { switch s := v.(type) { case RedisStorer: err = s.RedisSave(r) case RedisInterface: data, err := GobEncode(s) if err != nil { return err } _, err = r.Do("SET", s.GetKey(), data) 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 }
func (v *RedisSlice) RedisRemove(r redis.Conn) error { if v.slice.Len() == 0 { return nil } keys := make([]interface{}, v.slice.Len()+1) keys[0] = v.key for i := 0; i < v.slice.Len(); i++ { switch ro := v.slice.Index(i).Interface().(type) { case RedisInterface: keys[i+1] = ro.GetKey() RedisRemoveWithConn(r, ro) default: keys[i+1] = ro } } _, err := r.Do("SREM", keys...) return err }