Class: Redstruct::Types::Struct

Inherits:
Base
  • Object
show all
Includes:
Utils::Inspectable
Defined in:
lib/redstruct/types/struct.rb

Direct Known Subclasses

Hash, List, Set, SortedSet, String

Instance Attribute Summary

Attributes inherited from Base

#key

Instance Method Summary collapse

Methods included from Utils::Inspectable

#inspect, #to_s

Methods inherited from Base

#initialize, #to_h

Constructor Details

This class inherits a constructor from Redstruct::Types::Base

Instance Method Details

#deleteFixnum

Returns 0 if nothing was deleted in the DB, 1 if it was.

Returns:

  • (Fixnum)

    0 if nothing was deleted in the DB, 1 if it was



14
15
16
# File 'lib/redstruct/types/struct.rb', line 14

def delete
  self.connection.del(@key)
end

#exists?Boolean

Returns true if it exists in redis, false otherwise

Returns:

  • (Boolean)

    Returns true if it exists in redis, false otherwise



9
10
11
# File 'lib/redstruct/types/struct.rb', line 9

def exists?
  return self.connection.exists(@key)
end

#expire(ttl, ms: false) ⇒ Object

Sets the key to expire after ttl seconds

Parameters:

  • ttl (Integer, #to_i)

    the time to live in seconds (or milliseconds if ms is true)

  • ms (Boolean) (defaults to: false)

    if true, assumes ttl is in milliseconds



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

def expire(ttl, ms: false)
  if ms
    self.connection.pexpire(@key, ttl.to_i)
  else
    self.connection.expire(@key, ttl.to_i)
  end
end

#expire_at(time, ms: false) ⇒ Object

Sets the key to expire at the given timestamp.

Parameters:

  • time (Time, Integer, #to_i)

    time or unix timestamp at which the key should expire

  • ms (Boolean) (defaults to: false)

    if true, assumes the timestamp is in milliseconds



32
33
34
35
36
37
38
39
# File 'lib/redstruct/types/struct.rb', line 32

def expire_at(time, ms: false)
  if ms
    time = (time.to_f * 1000) if time.is_a?(Time)
    self.connection.pexpire_at(@key, time.to_i)
  else
    self.connection.expire_at(@key, time.to_i)
  end
end

#inspectable_attributesObject

:nocov:



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

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

#persistObject

Removes the expiry time from a key



42
43
44
# File 'lib/redstruct/types/struct.rb', line 42

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

#typeString

Returns the underlying redis type.

Returns:

  • (String)

    the underlying redis type



47
48
49
# File 'lib/redstruct/types/struct.rb', line 47

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