Method: Redis#set

Defined in:
lib/redis.rb

#set(key, value, ex: nil, px: nil, nx: nil, xx: nil, keepttl: 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.
    • :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.

Returns:

  • (String, Boolean)

    "OK" or true, false if :nx => true or :xx => true


836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'lib/redis.rb', line 836

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

  synchronize do |client|
    if nx || xx
      client.call(args, &BoolifySet)
    else
      client.call(args)
    end
  end
end