Class: Redstruct::Struct
- Inherits:
-
Factory::Object
- Object
- Factory::Object
- Redstruct::Struct
- Includes:
- Utils::Coercion
- Defined in:
- lib/redstruct/struct.rb
Overview
Base class for all redis structures which have a particular value for a given key
Instance Attribute Summary collapse
-
#key ⇒ String
readonly
The key used to identify the struct on redis.
Attributes inherited from Factory::Object
Instance Method Summary collapse
-
#delete ⇒ Boolean
False if nothing was deleted in the DB, true if it was.
-
#dump ⇒ String?
Returns a serialized representation of the key, which can be used to store a value externally, and restored to redis using #restore NOTE: This does not capture the TTL of the struct.
-
#exists? ⇒ Boolean
Returns true if it exists in redis, false otherwise.
-
#expire(ttl) ⇒ Boolean
Sets the key to expire after ttl seconds.
-
#expire_at(time) ⇒ Boolean
Sets the key to expire at the given timestamp.
-
#initialize(key:, **options) ⇒ Struct
constructor
A new instance of Struct.
-
#inspectable_attributes ⇒ Object
# @!visibility private.
-
#persist ⇒ Boolean
Removes the expiry time from a key.
-
#restore(serialized, ttl: 0) ⇒ Boolean
Restores the struct to its serialized value as given.
-
#ttl ⇒ Float, ...
Returns the time to live of the key.
-
#type ⇒ String
The underlying redis type.
Methods included from Utils::Coercion
Methods inherited from Factory::Object
Methods included from Utils::Inspectable
Constructor Details
#initialize(key:, **options) ⇒ Struct
Returns a new instance of Struct.
15 16 17 18 |
# File 'lib/redstruct/struct.rb', line 15 def initialize(key:, **) super(**) @key = key end |
Instance Attribute Details
#key ⇒ String (readonly)
Returns the key used to identify the struct on redis.
12 13 14 |
# File 'lib/redstruct/struct.rb', line 12 def key @key end |
Instance Method Details
#delete ⇒ Boolean
Returns false if nothing was deleted in the DB, true if it was.
26 27 28 |
# File 'lib/redstruct/struct.rb', line 26 def delete return coerce_bool(self.connection.del(@key)) end |
#dump ⇒ String?
Returns a serialized representation of the key, which can be used to store a value externally, and restored to redis using #restore NOTE: This does not capture the TTL of the struct. If there arises a need for this, we can always modify it, but for now this is a pure proxy of the redis dump command
73 74 75 |
# File 'lib/redstruct/struct.rb', line 73 def dump return self.connection.dump(@key) end |
#exists? ⇒ Boolean
Returns true if it exists in redis, false otherwise
21 22 23 |
# File 'lib/redstruct/struct.rb', line 21 def exists? return self.connection.exists(@key) end |
#expire(ttl) ⇒ Boolean
Sets the key to expire after ttl seconds
33 34 35 36 |
# File 'lib/redstruct/struct.rb', line 33 def expire(ttl) ttl = (ttl.to_f * 1000).floor return coerce_bool(self.connection.pexpire(@key, ttl)) end |
#expire_at(time) ⇒ Boolean
Sets the key to expire at the given timestamp.
41 42 43 44 |
# File 'lib/redstruct/struct.rb', line 41 def expire_at(time) time = (time.to_f * 1000).floor return coerce_bool(self.connection.pexpireat(@key, time)) end |
#inspectable_attributes ⇒ Object
# @!visibility private
88 89 90 |
# File 'lib/redstruct/struct.rb', line 88 def inspectable_attributes super.merge(key: @key) end |
#persist ⇒ Boolean
Removes the expiry time from a key
48 49 50 |
# File 'lib/redstruct/struct.rb', line 48 def persist coerce_bool(self.connection.persist(@key)) end |
#restore(serialized, ttl: 0) ⇒ Boolean
Restores the struct to its serialized value as given
82 83 84 85 |
# File 'lib/redstruct/struct.rb', line 82 def restore(serialized, ttl: 0) ttl = (ttl.to_f * 1000).floor return self.connection.restore(@key, ttl, serialized) end |
#ttl ⇒ Float, ...
Returns the time to live of the key
61 62 63 64 65 66 |
# File 'lib/redstruct/struct.rb', line 61 def ttl value = self.connection.pttl(@key) return nil if [-1, -2].include?(value) return value.to_f / 1000 end |
#type ⇒ String
Returns the underlying redis type.
53 54 55 56 57 |
# File 'lib/redstruct/struct.rb', line 53 def type name = self.connection.type(@key) return nil if name == 'none' return name end |