github.com/therealbill/libredis@v0.0.0-20161227004305-7d50abda5ccf/client/hashes.go (about)

     1  package client
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  // HDel command:
     8  // Removes the specified fields from the hash stored at key.
     9  // Specified fields that do not exist within this hash are ignored.
    10  // If key does not exist, it is treated as an empty hash and this command returns 0.
    11  func (r *Redis) HDel(key string, fields ...string) (int64, error) {
    12  	args := packArgs("HDEL", key, fields)
    13  	rp, err := r.ExecuteCommand(args...)
    14  	if err != nil {
    15  		return 0, err
    16  	}
    17  	return rp.IntegerValue()
    18  }
    19  
    20  // HExists command:
    21  // Returns if field is an existing field in the hash stored at key.
    22  func (r *Redis) HExists(key, field string) (bool, error) {
    23  	rp, err := r.ExecuteCommand("HEXISTS", key, field)
    24  	if err != nil {
    25  		return false, err
    26  	}
    27  	return rp.BoolValue()
    28  }
    29  
    30  // HGet command:
    31  // Returns the value associated with field in the hash stored at key.
    32  // Bulk reply: the value associated with field,
    33  // or nil when field is not present in the hash or key does not exist.
    34  func (r *Redis) HGet(key, field string) ([]byte, error) {
    35  	rp, err := r.ExecuteCommand("HGET", key, field)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	return rp.BytesValue()
    40  }
    41  
    42  // HGetAll command:
    43  // Returns all fields and values of the hash stored at key.
    44  // In the returned value, every field name is followed by its value,
    45  // so the length of the reply is twice the size of the hash.
    46  func (r *Redis) HGetAll(key string) (map[string]string, error) {
    47  	rp, err := r.ExecuteCommand("HGETALL", key)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return rp.HashValue()
    52  }
    53  
    54  // HIncrBy command:
    55  // Increments the number stored at field in the hash stored at key by increment.
    56  // If key does not exist, a new key holding a hash is created.
    57  // If field does not exist the value is set to 0 before the operation is performed.
    58  // Integer reply: the value at field after the increment operation.
    59  func (r *Redis) HIncrBy(key, field string, increment int) (int64, error) {
    60  	rp, err := r.ExecuteCommand("HINCRBY", key, field, increment)
    61  	if err != nil {
    62  		return 0, err
    63  	}
    64  	return rp.IntegerValue()
    65  }
    66  
    67  // HIncrByFloat command:
    68  // Increment the specified field of an hash stored at key,
    69  // and representing a floating point number, by the specified increment.
    70  // If the field does not exist, it is set to 0 before performing the operation.
    71  // An error is returned if one of the following conditions occur:
    72  // The field contains a value of the wrong type (not a string).
    73  // The current field content or the specified increment are not parsable as a double precision floating point number.
    74  // Bulk reply: the value of field after the increment.
    75  func (r *Redis) HIncrByFloat(key, field string, increment float64) (float64, error) {
    76  	rp, err := r.ExecuteCommand("HINCRBYFLOAT", key, field, increment)
    77  	if err != nil {
    78  		return 0.0, err
    79  	}
    80  	s, err := rp.StringValue()
    81  	if err != nil {
    82  		return 0.0, err
    83  	}
    84  	return strconv.ParseFloat(s, 64)
    85  }
    86  
    87  // HKeys command:
    88  // Returns all field names in the hash stored at key.
    89  // Multi-bulk reply: list of fields in the hash, or an empty list when key does not exist.
    90  func (r *Redis) HKeys(key string) ([]string, error) {
    91  	rp, err := r.ExecuteCommand("HKEYS", key)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return rp.ListValue()
    96  }
    97  
    98  // HLen command:
    99  // Returns the number of fields contained in the hash stored at key.
   100  // Integer reply: number of fields in the hash, or 0 when key does not exist.
   101  func (r *Redis) HLen(key string) (int64, error) {
   102  	rp, err := r.ExecuteCommand("HLEN", key)
   103  	if err != nil {
   104  		return 0, err
   105  	}
   106  	return rp.IntegerValue()
   107  }
   108  
   109  // HMGet command:
   110  // Returns the values associated with the specified fields in the hash stored at key.
   111  // For every field that does not exist in the hash, a nil value is returned.
   112  // Because a non-existing keys are treated as empty hashes,
   113  // running HMGET against a non-existing key will return a list of nil values.
   114  // Multi-bulk reply: list of values associated with the given fields, in the same order as they are requested.
   115  func (r *Redis) HMGet(key string, fields ...string) ([][]byte, error) {
   116  	args := packArgs("HMGET", key, fields)
   117  	rp, err := r.ExecuteCommand(args...)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	return rp.BytesArrayValue()
   122  }
   123  
   124  // HMSet command:
   125  // Sets the specified fields to their respective values in the hash stored at key.
   126  // This command overwrites any existing fields in the hash.
   127  // If key does not exist, a new key holding a hash is created.
   128  func (r *Redis) HMSet(key string, pairs map[string]string) error {
   129  	args := packArgs("HMSET", key, pairs)
   130  	rp, err := r.ExecuteCommand(args...)
   131  	if err != nil {
   132  		return err
   133  	}
   134  	return rp.OKValue()
   135  }
   136  
   137  // HSet command:
   138  // Sets field in the hash stored at key to value.
   139  // If key does not exist, a new key holding a hash is created.
   140  // If field already exists in the hash, it is overwritten.
   141  func (r *Redis) HSet(key, field, value string) (bool, error) {
   142  	rp, err := r.ExecuteCommand("HSET", key, field, value)
   143  	if err != nil {
   144  		return false, err
   145  	}
   146  	return rp.BoolValue()
   147  }
   148  
   149  // HSetnx command:
   150  // Sets field in the hash stored at key to value, only if field does not yet exist.
   151  // If key does not exist, a new key holding a hash is created.
   152  // If field already exists, this operation has no effect.
   153  func (r *Redis) HSetnx(key, field, value string) (bool, error) {
   154  	rp, err := r.ExecuteCommand("HSETNX", key, field, value)
   155  	if err != nil {
   156  		return false, err
   157  	}
   158  	return rp.BoolValue()
   159  }
   160  
   161  // HVals command:
   162  // Returns all values in the hash stored at key.
   163  // Multi-bulk reply: list of values in the hash, or an empty list when key does not exist.
   164  func (r *Redis) HVals(key string) ([]string, error) {
   165  	rp, err := r.ExecuteCommand("HVALS", key)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  	return rp.ListValue()
   170  }
   171  
   172  // HScan command:
   173  // HSCAN key cursor [MATCH pattern] [COUNT count]
   174  func (r *Redis) HScan(key string, cursor uint64, pattern string, count int) (uint64, map[string]string, error) {
   175  	args := packArgs("HSCAN", key, cursor)
   176  	if pattern != "" {
   177  		args = append(args, "MATCH", pattern)
   178  	}
   179  	if count > 0 {
   180  		args = append(args, "COUNT", count)
   181  	}
   182  	rp, err := r.ExecuteCommand(args...)
   183  	if err != nil {
   184  		return 0, nil, err
   185  	}
   186  	first, err := rp.Multi[0].StringValue()
   187  	if err != nil {
   188  		return 0, nil, err
   189  	}
   190  	next, err := strconv.ParseUint(first, 10, 64)
   191  	if err != nil {
   192  		return 0, nil, err
   193  	}
   194  	hash, err := rp.Multi[1].HashValue()
   195  	return next, hash, err
   196  }