github.com/therealbill/libredis@v0.0.0-20161227004305-7d50abda5ccf/client/strings.go (about) 1 package client 2 3 import ( 4 "strconv" 5 ) 6 7 // Append appends the value at the end of the string which stored at key 8 // If key does not exist it is created and set as an empty string. 9 // Return integer reply: the length of the string after the append operation. 10 func (r *Redis) Append(key, value string) (int64, error) { 11 rp, err := r.ExecuteCommand("APPEND", key, value) 12 if err != nil { 13 return 0, err 14 } 15 return rp.IntegerValue() 16 } 17 18 // BitCount counts the number of set bits (population counting) in a string. 19 func (r *Redis) BitCount(key string, start, end int) (int64, error) { 20 rp, err := r.ExecuteCommand("BITCOUNT", key, start, end) 21 if err != nil { 22 return 0, err 23 } 24 return rp.IntegerValue() 25 } 26 27 // BitOp performs a bitwise operation between multiple keys (containing string values) 28 // and store the result in the destination key. 29 // The BITOP command supports four bitwise operations: 30 // AND, OR, XOR and NOT, thus the valid forms to call the command are: 31 // BITOP AND destkey srckey1 srckey2 srckey3 ... srckeyN 32 // BITOP OR destkey srckey1 srckey2 srckey3 ... srckeyN 33 // BITOP XOR destkey srckey1 srckey2 srckey3 ... srckeyN 34 // BITOP NOT destkey srckey 35 // Return value: Integer reply 36 // The size of the string stored in the destination key, that is equal to the size of the longest input string. 37 func (r *Redis) BitOp(operation, destkey string, keys ...string) (int64, error) { 38 args := packArgs("BITOP", operation, destkey, keys) 39 rp, err := r.ExecuteCommand(args...) 40 if err != nil { 41 return 0, err 42 } 43 return rp.IntegerValue() 44 } 45 46 // Decr decrements the number stored at key by one. 47 // If the key does not exist, it is set to 0 before performing the operation. 48 // An error is returned if the key contains a value of the wrong type 49 // or contains a string that can not be represented as integer. 50 // This operation is limited to 64 bit signed integers. 51 // Integer reply: the value of key after the decrement 52 func (r *Redis) Decr(key string) (int64, error) { 53 rp, err := r.ExecuteCommand("DECR", key) 54 if err != nil { 55 return 0, err 56 } 57 return rp.IntegerValue() 58 } 59 60 // DecrBy decrements the number stored at key by decrement. 61 func (r *Redis) DecrBy(key string, decrement int) (int64, error) { 62 rp, err := r.ExecuteCommand("DECRBY", key, decrement) 63 if err != nil { 64 return 0, err 65 } 66 return rp.IntegerValue() 67 } 68 69 // Get gets the value of key. 70 // If the key does not exist the special value nil is returned. 71 // An error is returned if the value stored at key is not a string, 72 // because GET only handles string values. 73 func (r *Redis) Get(key string) ([]byte, error) { 74 rp, err := r.ExecuteCommand("GET", key) 75 if err != nil { 76 return nil, err 77 } 78 return rp.BytesValue() 79 } 80 81 // GetInt gets the integer value of key. 82 // If the key does not exist the special value nil is returned. 83 // An error is returned if the value stored at key is not a string, 84 // because GET only handles string values. 85 func (r *Redis) GetInt(key string) (int64, error) { 86 rp, err := r.ExecuteCommand("GET", key) 87 if err != nil { 88 return 0, err 89 } 90 return rp.IntegerValue() 91 } 92 93 // GetBit returns the bit value at offset in the string value stored at key. 94 // When offset is beyond the string length, 95 // the string is assumed to be a contiguous space with 0 bits. 96 // When key does not exist it is assumed to be an empty string, 97 // so offset is always out of range and the value is also assumed to be a contiguous space with 0 bits. 98 func (r *Redis) GetBit(key string, offset int) (int64, error) { 99 rp, err := r.ExecuteCommand("GETBIT", key, offset) 100 if err != nil { 101 return 0, err 102 } 103 return rp.IntegerValue() 104 } 105 106 // GetRange returns the substring of the string value stored at key, 107 // determined by the offsets start and end (both are inclusive). 108 // Negative offsets can be used in order to provide an offset starting from the end of the string. 109 // So -1 means the last character, -2 the penultimate and so forth. 110 // The function handles out of range requests by limiting the resulting range to the actual length of the string. 111 func (r *Redis) GetRange(key string, start, end int) (string, error) { 112 rp, err := r.ExecuteCommand("GETRANGE", key, start, end) 113 if err != nil { 114 return "", err 115 } 116 return rp.StringValue() 117 } 118 119 // GetSet atomically sets key to value and returns the old value stored at key. 120 // Returns an error when key exists but does not hold a string value. 121 func (r *Redis) GetSet(key, value string) ([]byte, error) { 122 rp, err := r.ExecuteCommand("GETSET", key, value) 123 if err != nil { 124 return nil, err 125 } 126 return rp.BytesValue() 127 } 128 129 // Incr increments the number stored at key by one. 130 // If the key does not exist, it is set to 0 before performing the operation. 131 // An error is returned if the key contains a value of the wrong type 132 // or contains a string that can not be represented as integer. 133 // Integer reply: the value of key after the increment 134 func (r *Redis) Incr(key string) (int64, error) { 135 rp, err := r.ExecuteCommand("INCR", key) 136 if err != nil { 137 return 0, err 138 } 139 return rp.IntegerValue() 140 } 141 142 // IncrBy increments the number stored at key by increment. 143 // If the key does not exist, it is set to 0 before performing the operation. 144 // An error is returned if the key contains a value of the wrong type 145 // or contains a string that can not be represented as integer. 146 // Integer reply: the value of key after the increment 147 func (r *Redis) IncrBy(key string, increment int) (int64, error) { 148 rp, err := r.ExecuteCommand("INCRBY", key, increment) 149 if err != nil { 150 return 0, err 151 } 152 return rp.IntegerValue() 153 } 154 155 // IncrByFloat increments the string representing a floating point number 156 // stored at key by the specified increment. 157 // If the key does not exist, it is set to 0 before performing the operation. 158 // An error is returned if one of the following conditions occur: 159 // The key contains a value of the wrong type (not a string). 160 // The current key content or the specified increment are not parsable 161 // as a double precision floating point number. 162 // Return bulk reply: the value of key after the increment. 163 func (r *Redis) IncrByFloat(key string, increment float64) (float64, error) { 164 rp, err := r.ExecuteCommand("INCRBYFLOAT", key, increment) 165 if err != nil { 166 return 0.0, err 167 } 168 s, err := rp.StringValue() 169 if err != nil { 170 return 0.0, err 171 } 172 return strconv.ParseFloat(s, 64) 173 } 174 175 // MGet returns the values of all specified keys. 176 // For every key that does not hold a string value or does not exist, 177 // the special value nil is returned. Because of this, the operation never fails. 178 // Multi-bulk reply: list of values at the specified keys. 179 func (r *Redis) MGet(keys ...string) ([][]byte, error) { 180 args := packArgs("MGET", keys) 181 rp, err := r.ExecuteCommand(args...) 182 if err != nil { 183 return nil, err 184 } 185 return rp.BytesArrayValue() 186 } 187 188 // MSet sets the given keys to their respective values. 189 // MSET replaces existing values with new values, just as regular SET. 190 // See MSETNX if you don't want to overwrite existing values. 191 func (r *Redis) MSet(pairs map[string]string) error { 192 args := packArgs("MSET", pairs) 193 _, err := r.ExecuteCommand(args...) 194 return err 195 } 196 197 // MSetnx sets the given keys to their respective values. 198 // MSETNX will not perform any operation at all even if just a single key already exists. 199 // True if the all the keys were set. 200 // False if no key was set (at least one key already existed). 201 func (r *Redis) MSetnx(pairs map[string]string) (bool, error) { 202 args := packArgs("MSETNX", pairs) 203 rp, err := r.ExecuteCommand(args...) 204 if err != nil { 205 return false, err 206 } 207 return rp.BoolValue() 208 } 209 210 // Set sets key to hold the string value. 211 // If key already holds a value, it is overwritten, regardless of its type. 212 // Any previous time to live associated with the key is discarded on successful SET operation. 213 func (r *Redis) Set(key, value string) error { 214 rp, err := r.ExecuteCommand("SET", key, value) 215 if err != nil { 216 return err 217 } 218 return rp.OKValue() 219 } 220 221 // PSetex works exactly like SETEX with the sole difference that 222 // the expire time is specified in milliseconds instead of seconds. 223 func (r *Redis) PSetex(key string, milliseconds int, value string) error { 224 _, err := r.ExecuteCommand("PSETEX", key, milliseconds, value) 225 return err 226 } 227 228 // SetMX sets key to hold the string value, andn only sets if the key already exists. 229 // Any previous time to live associated with the key is discarded on successful SET operation. 230 func (r *Redis) SetMX(key, value string) error { 231 rp, err := r.ExecuteCommand("SET", key, value, "XX") 232 if err != nil { 233 return err 234 } 235 return rp.OKValue() 236 } 237 238 // SetBit sets or clears the bit at offset in the string value stored at key. 239 // Integer reply: the original bit value stored at offset. 240 func (r *Redis) SetBit(key string, offset, value int) (int64, error) { 241 rp, err := r.ExecuteCommand("SETBIT", key, offset, value) 242 if err != nil { 243 return 0, err 244 } 245 return rp.IntegerValue() 246 } 247 248 // Setex sets key to hold the string value and set key to timeout after a given number of seconds. 249 func (r *Redis) Setex(key string, seconds int, value string) error { 250 rp, err := r.ExecuteCommand("SETEX", key, seconds, value) 251 if err != nil { 252 return err 253 } 254 return rp.OKValue() 255 } 256 257 // Setnx sets key to hold string value if key does not exist. 258 func (r *Redis) Setnx(key, value string) (bool, error) { 259 rp, err := r.ExecuteCommand("SETNX", key, value) 260 if err != nil { 261 return false, err 262 } 263 return rp.BoolValue() 264 } 265 266 // SetRange overwrites part of the string stored at key, starting at the specified offset, 267 // for the entire length of value. 268 // Integer reply: the length of the string after it was modified by the command. 269 func (r *Redis) SetRange(key string, offset int, value string) (int64, error) { 270 rp, err := r.ExecuteCommand("SETRANGE", key, offset, value) 271 if err != nil { 272 return 0, err 273 } 274 return rp.IntegerValue() 275 } 276 277 // StrLen returns the length of the string value stored at key. 278 // An error is returned when key holds a non-string value. 279 // Integer reply: the length of the string at key, or 0 when key does not exist. 280 func (r *Redis) StrLen(key string) (int64, error) { 281 rp, err := r.ExecuteCommand("STRLEN", key) 282 if err != nil { 283 return 0, err 284 } 285 return rp.IntegerValue() 286 }