Class: Redstruct::Types::String

Inherits:
Struct show all
Includes:
Utils::Coercion, Utils::Scriptable
Defined in:
lib/redstruct/types/string.rb

Direct Known Subclasses

Counter

Instance Attribute Summary

Attributes inherited from Base

#key

Instance Method Summary collapse

Methods included from Utils::Scriptable

included

Methods included from Utils::Coercion

coerce_array, coerce_bool

Methods inherited from Struct

#delete, #exists?, #expire, #expire_at, #inspectable_attributes, #persist, #type

Methods included from Utils::Inspectable

#inspect, #inspectable_attributes, #to_s

Methods inherited from Base

#initialize, #inspectable_attributes, #to_h, #with

Constructor Details

This class inherits a constructor from Redstruct::Types::Base

Instance Method Details

#delete_if_equals(value) ⇒ Boolean

Returns True if deleted, false otherwise.

Parameters:

  • value (::String)

    The value to compare with

Returns:

  • (Boolean)

    True if deleted, false otherwise



27
28
29
# File 'lib/redstruct/types/string.rb', line 27

def delete_if_equals(value)
  coerce_bool(delete_if_equals_script(keys: @key, argv: value))
end

#delete_if_equals_scriptFixnum

Deletes the key (keys) iff the value is equal to argv.

Parameters:

  • keys (Array<(::String)>)

    The key to delete

  • argv (Array<(::String)>)

    The value to compare with

Returns:

  • (Fixnum)

    1 if deleted, 0 otherwise



54
55
56
57
58
59
60
61
# File 'lib/redstruct/types/string.rb', line 54

defscript :delete_if_equals_script, <<~LUA
  local deleted = false
  if redis.call("get", KEYS[1]) == ARGV[1] then
    deleted = redis.call("del", KEYS[1])
  end

  return deleted
LUA

#get::String

Returns The string value stored in the database.

Returns:

  • (::String)

    The string value stored in the database



7
8
9
# File 'lib/redstruct/types/string.rb', line 7

def get
  return self.connection.get(@key)
end

#getset(value) ⇒ ::String

Returns The old value before setting it.

Parameters:

  • value (Object)

    The object to store; note, it will be stored using a string representation

Returns:

  • (::String)

    The old value before setting it



33
34
35
# File 'lib/redstruct/types/string.rb', line 33

def getset(value)
  self.connection.getset(@key, value)
end

#lengthFixnum

Returns The length of the string.

Returns:

  • (Fixnum)

    The length of the string



38
39
40
# File 'lib/redstruct/types/string.rb', line 38

def length
  self.connection.strlen(@key)
end

#set(value, expiry: nil, nx: nil, xx: nil) ⇒ Boolean

Returns True if set, false otherwise.

Parameters:

  • value (Object)

    The object to store; note, it will be stored using a string representation

  • expiry (Integer) (defaults to: nil)

    The expiry time in seconds; if nil, will never expire

  • nx (Boolean) (defaults to: nil)

    Not Exists: if true, will not set the key if it already existed

  • xx (Boolean) (defaults to: nil)

    Already Exists: if true, will set the key only if it already existed

Returns:

  • (Boolean)

    True if set, false otherwise



16
17
18
19
20
21
22
23
# File 'lib/redstruct/types/string.rb', line 16

def set(value, expiry: nil, nx: nil, xx: nil)
  options = {}
  options[:ex] = expiry.to_i unless expiry.nil?
  options[:nx] = nx unless nx.nil?
  options[:xx] = xx unless xx.nil?

  self.connection.set(@key, value, options) == 'OK'
end

#slice(start = 0, length = -1)) ⇒ Array<::String>

Returns The requested slice from <start> with length <length>.

Parameters:

  • start (Fixnum) (defaults to: 0)

    Starting index of the slice

  • length (Fixnum) (defaults to: -1))

    Length of the slice; negative numbers start counting from the right (-1 = end)

Returns:

  • (Array<::String>)

    The requested slice from <start> with length <length>



45
46
47
48
# File 'lib/redstruct/types/string.rb', line 45

def slice(start = 0, length = -1)
  length = start + length if length >= 0
  return self.connection.getrange(@key, start, length)
end