class Redstruct::Struct
Base class for all redis structures which have a particular value for a given key
Attributes
@return [String] the key used to identify the struct on redis
Public Class Methods
@param [String] key the key used to identify the struct on redis, already namespaced
Redstruct::Factory::Object::new
# File lib/redstruct/struct.rb, line 15 def initialize(key:, **options) super(**options) @key = key end
Public Instance Methods
@return [Boolean] false if nothing was deleted in the DB, true if it was
# File lib/redstruct/struct.rb, line 26 def delete return coerce_bool(self.connection.del(@key)) end
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 @return [String, nil] nil if the struct does not exist, otherwise serialized representation
# File lib/redstruct/struct.rb, line 73 def dump return self.connection.dump(@key) end
@return [Boolean] Returns true if it exists in redis, false otherwise
# File lib/redstruct/struct.rb, line 21 def exists? return self.connection.exists(@key) end
Sets the key to expire after ttl seconds @param [#to_f] ttl the time to live in seconds (where 0.001 = 1ms) @return [Boolean] true if expired, false otherwise
# 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
Sets the key to expire at the given timestamp. @param [#to_f] time time or unix timestamp at which the key should expire; once converted to float, assumes 1.0 is one second, 0.001 is 1 ms @return [Boolean] true if expired, false otherwise
# 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
@!visibility private
Redstruct::Factory::Object#inspectable_attributes
# File lib/redstruct/struct.rb, line 88 def inspectable_attributes super.merge(key: @key) end
Removes the expiry time from a key @return [Boolean] true if persisted, false otherwise
# File lib/redstruct/struct.rb, line 48 def persist coerce_bool(self.connection.persist(@key)) end
Restores the struct to its serialized value as given @param [String] serialized serialized representation of the value @param [#to_f] ttl the time to live (in seconds) for the struct; defaults to 0 (meaning no expiry) @raise [Redis::CommandError] raised if the serialized value is incompatible or the key already exists @return [Boolean] true if restored, false otherwise
# 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
Returns the time to live of the key @return [Float, nil time nil if the key does not exist or has no expiry, or the time to live in seconds
# 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
@return [String] the underlying redis type
# File lib/redstruct/struct.rb, line 53 def type name = self.connection.type(@key) return nil if name == 'none' return name end