Module: Valkey::Commands::StringCommands
- Included in:
- Valkey::Commands
- Defined in:
- lib/valkey/commands/string_commands.rb
Overview
This module contains commands on the String data type.
Instance Method Summary collapse
-
#append(key, value) ⇒ Integer
Append a value to a key.
-
#decr(key) ⇒ Integer
Decrement the integer value of a key by one.
-
#decrby(key, decrement) ⇒ Integer
Decrement the integer value of a key by the given number.
-
#get(key) ⇒ String
Get the value of a key.
-
#getdel(key) ⇒ String?
Get the value of key and delete it.
-
#getex(key, ex: nil, px: nil, exat: nil, pxat: nil, persist: false) ⇒ String?
Get the value of key and optionally set its expiration.
-
#getrange(key, start, stop) ⇒ String
Get a substring of the string stored at key.
-
#getset(key, value) ⇒ String?
Atomically set
keytovalueand return the previous value. -
#incr(key) ⇒ Integer
Increment the integer value of a key by one.
-
#incrby(key, increment) ⇒ Integer
Increment the integer value of a key by the given integer number.
-
#incrbyfloat(key, increment) ⇒ Float
Increment the numeric value of a key by the given float number.
-
#lcs(key1, key2, len: nil, idx: nil, min_match_len: nil, with_match_len: nil) ⇒ String, ...
Find the longest common subsequence between two strings.
-
#mapped_mget(*keys) ⇒ Hash
Get the values of all the given keys.
-
#mapped_mset(hash) ⇒ String
Set one or more values.
-
#mapped_msetnx(hash) ⇒ Boolean
Set one or more values, only if none of the keys exist.
-
#mget(*keys, &blk) ⇒ Array<String>
Get the values of all the given keys.
-
#mset(*args) ⇒ String
Set one or more values.
-
#msetnx(*args) ⇒ Boolean
Set one or more values, only if none of the keys exist.
-
#psetex(key, ttl, value) ⇒ String
Set the time to live in milliseconds of a key.
-
#set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil) ⇒ String, Boolean
Set the string value of a key.
-
#setex(key, ttl, value) ⇒ String
Set the time to live in seconds of a key.
-
#setnx(key, value) ⇒ Boolean
Set the value of a key, only if the key does not exist.
-
#setrange(key, offset, value) ⇒ Integer
Overwrite part of a string at key starting at the specified offset.
-
#strlen(key) ⇒ Integer
Get the length of the value stored in a key.
Instance Method Details
#append(key, value) ⇒ Integer
Append a value to a key.
273 274 275 |
# File 'lib/valkey/commands/string_commands.rb', line 273 def append(key, value) send_command(RequestType::APPEND, [key, value]) end |
#decr(key) ⇒ Integer
Decrement the integer value of a key by one.
18 19 20 |
# File 'lib/valkey/commands/string_commands.rb', line 18 def decr(key) send_command(RequestType::DECR, [key]) end |
#decrby(key, decrement) ⇒ Integer
Decrement the integer value of a key by the given number.
31 32 33 |
# File 'lib/valkey/commands/string_commands.rb', line 31 def decrby(key, decrement) send_command(RequestType::DECR_BY, [key, decrement]) end |
#get(key) ⇒ String
Get the value of a key.
195 196 197 198 199 |
# File 'lib/valkey/commands/string_commands.rb', line 195 def get(key) result = send_command(RequestType::GET, [key]) result = handle_transaction_isolation_get(key, result) if should_intercept_get?(result) result end |
#getdel(key) ⇒ String?
Get the value of key and delete it.
281 282 283 |
# File 'lib/valkey/commands/string_commands.rb', line 281 def getdel(key) send_command(RequestType::GET_DEL, [key]) end |
#getex(key, ex: nil, px: nil, exat: nil, pxat: nil, persist: false) ⇒ String?
Get the value of key and optionally set its expiration.
295 296 297 298 299 300 301 302 303 304 |
# File 'lib/valkey/commands/string_commands.rb', line 295 def getex(key, ex: nil, px: nil, exat: nil, pxat: nil, persist: false) args = [key] args << "EX" << ex if ex args << "PX" << px if px args << "EXAT" << exat if exat args << "PXAT" << pxat if pxat args << "PERSIST" if persist send_command(RequestType::GET_EX, args) end |
#getrange(key, start, stop) ⇒ String
Get a substring of the string stored at key.
264 265 266 |
# File 'lib/valkey/commands/string_commands.rb', line 264 def getrange(key, start, stop) send_command(RequestType::GET_RANGE, [key, start, stop]) end |
#getset(key, value) ⇒ String?
Atomically set key to value and return the previous value.
Implemented as SET key value GET (Redis 6.2+ / Valkey 7+). This matches GETSET and avoids RequestType::GET_SET, which the Glide FFI layer does not implement yet.
209 210 211 |
# File 'lib/valkey/commands/string_commands.rb', line 209 def getset(key, value) set(key, value, get: true) end |
#incr(key) ⇒ Integer
Increment the integer value of a key by one.
43 44 45 |
# File 'lib/valkey/commands/string_commands.rb', line 43 def incr(key) send_command(RequestType::INCR, [key]) end |
#incrby(key, increment) ⇒ Integer
Increment the integer value of a key by the given integer number.
56 57 58 |
# File 'lib/valkey/commands/string_commands.rb', line 56 def incrby(key, increment) send_command(RequestType::INCR_BY, [key, increment]) end |
#incrbyfloat(key, increment) ⇒ Float
Increment the numeric value of a key by the given float number.
69 70 71 |
# File 'lib/valkey/commands/string_commands.rb', line 69 def incrbyfloat(key, increment) send_command(RequestType::INCR_BY_FLOAT, [key, increment]) end |
#lcs(key1, key2, len: nil, idx: nil, min_match_len: nil, with_match_len: nil) ⇒ String, ...
Find the longest common subsequence between two strings.
324 325 326 327 328 329 330 331 332 |
# File 'lib/valkey/commands/string_commands.rb', line 324 def lcs(key1, key2, len: nil, idx: nil, min_match_len: nil, with_match_len: nil) args = [key1, key2] args << "LEN" if len args << "IDX" if idx args << "MINMATCHLEN" << min_match_len if min_match_len args << "WITHMATCHLEN" if with_match_len send_command(RequestType::LCS, args) end |
#mapped_mget(*keys) ⇒ Hash
Get the values of all the given keys.
238 239 240 241 242 243 244 245 246 |
# File 'lib/valkey/commands/string_commands.rb', line 238 def mapped_mget(*keys) mget(*keys) do |reply| if reply.is_a?(Array) keys.zip(reply).to_h else reply end end end |
#mapped_mset(hash) ⇒ String
Set one or more values.
159 160 161 |
# File 'lib/valkey/commands/string_commands.rb', line 159 def mapped_mset(hash) mset(*hash.flatten) end |
#mapped_msetnx(hash) ⇒ Boolean
Set one or more values, only if none of the keys exist.
187 188 189 |
# File 'lib/valkey/commands/string_commands.rb', line 187 def mapped_msetnx(hash) msetnx(*hash.flatten) end |
#mget(*keys, &blk) ⇒ Array<String>
Get the values of all the given keys.
223 224 225 226 |
# File 'lib/valkey/commands/string_commands.rb', line 223 def mget(*keys, &blk) keys.flatten!(1) send_command(RequestType::MGET, keys, &blk) end |
#mset(*args) ⇒ String
Set one or more values.
145 146 147 |
# File 'lib/valkey/commands/string_commands.rb', line 145 def mset(*args) send_command(RequestType::MSET, args) end |
#msetnx(*args) ⇒ Boolean
Set one or more values, only if none of the keys exist.
173 174 175 |
# File 'lib/valkey/commands/string_commands.rb', line 173 def msetnx(*args) send_command(RequestType::MSET_NX, args) end |
#psetex(key, ttl, value) ⇒ String
Set the time to live in milliseconds of a key.
122 123 124 |
# File 'lib/valkey/commands/string_commands.rb', line 122 def psetex(key, ttl, value) send_command(RequestType::PSET_EX, [key, Integer(ttl), value]) end |
#set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil) ⇒ String, Boolean
Set the string value of a key.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/valkey/commands/string_commands.rb', line 87 def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil) args = [key, value] args << "EX" << ex if ex args << "PX" << px if px args << "EXAT" << exat if exat args << "PXAT" << pxat if pxat args << "NX" if nx args << "XX" if xx args << "KEEPTTL" if keepttl args << "GET" if get send_command(RequestType::SET, args) # if nx || xx # send_command(RequestType::SET, &Utils::BoolifySet)) # else # send_command(RequestType::SET, args) # end end |
#setex(key, ttl, value) ⇒ String
Set the time to live in seconds of a key.
112 113 114 |
# File 'lib/valkey/commands/string_commands.rb', line 112 def setex(key, ttl, value) send_command(RequestType::SET_EX, [key, ttl, value]) end |
#setnx(key, value) ⇒ Boolean
Set the value of a key, only if the key does not exist.
131 132 133 |
# File 'lib/valkey/commands/string_commands.rb', line 131 def setnx(key, value) send_command(RequestType::SET_NX, [key, value]) end |
#setrange(key, offset, value) ⇒ Integer
Overwrite part of a string at key starting at the specified offset.
254 255 256 |
# File 'lib/valkey/commands/string_commands.rb', line 254 def setrange(key, offset, value) send_command(RequestType::SET_RANGE, [key, offset, value]) end |
#strlen(key) ⇒ Integer
Get the length of the value stored in a key.
310 311 312 |
# File 'lib/valkey/commands/string_commands.rb', line 310 def strlen(key) send_command(RequestType::STRLEN, [key]) end |