Class: Redstruct::String

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

Overview

Manipulation of redis strings

Direct Known Subclasses

Counter

Instance Attribute Summary

Attributes inherited from Struct

#key

Attributes inherited from Factory::Object

#factory

Instance Method Summary collapse

Methods included from Utils::Scriptable

included

Methods inherited from Struct

#delete, #dump, #exists?, #expire, #expire_at, #initialize, #inspectable_attributes, #persist, #restore, #ttl, #type

Methods included from Utils::Coercion

coerce_array, coerce_bool

Methods inherited from Factory::Object

#connection, #initialize, #inspectable_attributes

Methods included from Utils::Inspectable

#inspect, #inspectable_attributes

Constructor Details

This class inherits a constructor from Redstruct::Struct

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



30
31
32
# File 'lib/redstruct/string.rb', line 30

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

#delete_if_equals_scriptInteger

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:

  • (Integer)

    1 if deleted, 0 otherwise



57
58
59
60
61
62
63
64
# File 'lib/redstruct/string.rb', line 57

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

#getString?

Returns the string value stored in the database.

Returns:

  • (String, nil)

    the string value stored in the database



12
13
14
# File 'lib/redstruct/string.rb', line 12

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

#getset(value) ⇒ String

Returns The old value before setting it.

Parameters:

  • value (#to_s)

    The object to store

Returns:

  • (String)

    The old value before setting it



36
37
38
# File 'lib/redstruct/string.rb', line 36

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

#lengthInteger

Returns The length of the string.

Returns:

  • (Integer)

    The length of the string



41
42
43
# File 'lib/redstruct/string.rb', line 41

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

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

Returns true if set, false otherwise.

Parameters:

  • value (#to_s)

    the object to store

  • expiry (Integer) (defaults to: nil)

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

  • nx (Boolean) (defaults to: false)

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

  • xx (Boolean) (defaults to: false)

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

Returns:

  • (Boolean)

    true if set, false otherwise



21
22
23
24
25
26
# File 'lib/redstruct/string.rb', line 21

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

  coerce_bool(self.connection.set(@key, value, options))
end

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

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

Parameters:

  • start (Integer) (defaults to: 0)

    Starting index of the slice

  • length (Integer) (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>



48
49
50
51
# File 'lib/redstruct/string.rb', line 48

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