Module: Valkey::Commands::Strings

Included in:
Valkey::Commands
Defined in:
lib/valkey/commands/strings.rb

Overview

This module contains commands related to strings.

Instance Method Summary collapse

Instance Method Details

#get(key) ⇒ String

Get the value of a key.

Parameters:

  • key (String)

Returns:

  • (String)


43
44
45
# File 'lib/valkey/commands/strings.rb', line 43

def get(key)
  send_command(RequestType::GET, [key])
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.

Parameters:

  • key (String)
  • value (String)
  • options (Hash)
    • ‘:ex => Integer`: Set the specified expire time, in seconds.

    • ‘:px => Integer`: Set the specified expire time, in milliseconds.

    • ‘:exat => Integer` : Set the specified Unix time at which the key will expire, in seconds.

    • ‘:pxat => Integer` : Set the specified Unix time at which the key will expire, in milliseconds.

    • ‘:nx => true`: Only set the key if it does not already exist.

    • ‘:xx => true`: Only set the key if it already exist.

    • ‘:keepttl => true`: Retain the time to live associated with the key.

    • ‘:get => true`: Return the old string stored at key, or nil if key did not exist.

Returns:

  • (String, Boolean)

    ‘“OK”` or true, false if `:nx => true` or `:xx => true`



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/valkey/commands/strings.rb', line 21

def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil)
  args = [key, value.to_s]
  args << "EX" << Integer(ex) if ex
  args << "PX" << Integer(px) if px
  args << "EXAT" << Integer(exat) if exat
  args << "PXAT" << Integer(pxat) if pxat
  args << "NX" if nx
  args << "XX" if xx
  args << "KEEPTTL" if keepttl
  args << "GET" if get

  if nx || xx
    send_command(RequestType::SET(args, &BoolifySet))
  else
    send_command(RequestType::SET, args)
  end
end