Method: Redis::Commands::Strings#set

Defined in:
lib/redis/commands/strings.rb

#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


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/redis/commands/strings.rb', line 83

def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil)
  args = [:set, 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(args, &BoolifySet)
  else
    send_command(args)
  end
end