Class: Redstruct::Types::String
- Includes:
- Utils::Coercion, Utils::Scriptable
- Defined in:
- lib/redstruct/types/string.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#delete_if_equals(value) ⇒ Boolean
True if deleted, false otherwise.
- #delete_if_equals_script ⇒ Fixnum
-
#get ⇒ ::String
The string value stored in the database.
-
#getset(value) ⇒ ::String
The old value before setting it.
-
#length ⇒ Fixnum
The length of the string.
-
#set(value, expiry: nil, nx: nil, xx: nil) ⇒ Boolean
True if set, false otherwise.
-
#slice(start = 0, length = -1)) ⇒ Array<::String>
The requested slice from <start> with length <length>.
Methods included from Utils::Scriptable
Methods included from Utils::Coercion
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.
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_script ⇒ Fixnum
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.
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.
33 34 35 |
# File 'lib/redstruct/types/string.rb', line 33 def getset(value) self.connection.getset(@key, value) end |
#length ⇒ Fixnum
Returns 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.
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) = {} [:ex] = expiry.to_i unless expiry.nil? [:nx] = nx unless nx.nil? [:xx] = xx unless xx.nil? self.connection.set(@key, value, ) == 'OK' end |
#slice(start = 0, length = -1)) ⇒ Array<::String>
Returns 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 |