Class: Redstruct::Struct

Inherits:
Factory::Object
  • Object
show all
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

Direct Known Subclasses

Hash, List, Set, SortedSet, String

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, **options) ⇒ Struct

Returns a new instance of Struct

Parameters:

  • key (String)

    the key used to identify the struct on redis, already namespaced



15
16
17
18
# File 'lib/redstruct/struct.rb', line 15

def initialize(key:, **options)
  super(**options)
  @key = key
end

Instance Attribute Details

#keyString (readonly)

Returns the key used to identify the struct on redis

Returns:

  • (String)

    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

#deleteBoolean

Returns false if nothing was deleted in the DB, true if it was

Returns:

  • (Boolean)

    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

#dumpString?

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

Returns:

  • (String, nil)

    nil if the struct does not exist, otherwise serialized representation



68
69
70
# File 'lib/redstruct/struct.rb', line 68

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

#exists?Boolean

Returns true if it exists in redis, false otherwise

Returns:

  • (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

Parameters:

  • ttl (#to_f)

    the time to live in seconds (where 0.001 = 1ms)

Returns:

  • (Boolean)

    true if expired, false otherwise



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.

Parameters:

  • time (#to_f)

    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

Returns:

  • (Boolean)

    true if expired, false otherwise



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_attributesObject

# @!visibility private



83
84
85
# File 'lib/redstruct/struct.rb', line 83

def inspectable_attributes
  super.merge(key: @key)
end

#persistBoolean

Removes the expiry time from a key

Returns:

  • (Boolean)

    true if persisted, false otherwise



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

Parameters:

  • serialized (String)

    serialized representation of the value

  • ttl (#to_f)

    the time to live for the struct; defaults to 0 (meaning no expiry). 0.001 == 1ms

Returns:

  • (Boolean)

    true if restored, false otherwise

Raises:

  • (Redis::CommandError)

    raised if the serialized value is incompatible or the key already exists



77
78
79
80
# File 'lib/redstruct/struct.rb', line 77

def restore(serialized, ttl: 0)
  ttl = (ttl.to_f * 1000).floor
  return self.connection.restore(@key, ttl, serialized)
end

#ttlFloat

Returns the time to live of the key

Returns:

  • (Float)

    time to live in seconds as a float where 0.001 == 1 ms



59
60
61
# File 'lib/redstruct/struct.rb', line 59

def ttl
  return self.connection.pttl(@key) / 1000.0
end

#typeString

Returns the underlying redis type

Returns:

  • (String)

    the underlying redis type



53
54
55
# File 'lib/redstruct/struct.rb', line 53

def type
  self.connection.type(@key)
end