func (r *RedisClient) IsUserOnline(u user.User) bool { key := genUserOnlineKey(u.GetUserInfo()) isExits, err := r.client.Exists(key).Result() if err != nil { return false } return isExits }
func (r *RedisClient) GetUserInfo(u user.User) (user.UserInfo, error) { var user user.UserInfo val, err := r.client.Get(genUserInfoKey(u.GetUserInfo())).Result() if err != nil { return user, err } err = json.Unmarshal([]byte(val), &user) return user, err }
func (r *RedisClient) GetSize(u user.User) (int64, error) { key := genUserFlowKey(u.GetUserInfo()) isExits, err := r.client.Exists(key).Result() if err != nil { return 0, err } if !isExits { return 0, nil } return r.client.Get(key).Int64() }
// traffic func (r *RedisClient) IncrSize(u user.User, size int) error { key := genUserFlowKey(u.GetUserInfo()) incrSize := int(float32(size)) isExits, err := r.client.Exists(key).Result() if err != nil { return err } if !isExits { return r.client.Set(key, incrSize, DefaultExpireTime).Err() } return r.client.IncrBy(key, int64(incrSize)).Err() }
func (r *RedisClient) MarkUserOnline(u user.User) error { key := genUserOnlineKey(u.GetUserInfo()) return r.client.Set(key, "1", DefaultOnlineKeyExpireTime).Err() }
func (r *RedisClient) SetSize(u user.User, size int) error { key := genUserFlowKey(u.GetUserInfo()) return r.client.Set(key, size, DefaultExpireTime).Err() }
func (r *RedisClient) Del(u user.User) error { return r.client.Del(genUserInfoKey(u.GetUserInfo())).Err() }
func (r *RedisClient) Exists(u user.User) (bool, error) { return r.client.Exists(genUserInfoKey(u.GetUserInfo())).Result() }