Class: Zm::Support::Cache::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/support/cache/entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, version: 1, expires_in: nil, expires_at: nil) ⇒ Entry

Returns a new instance of Entry.



27
28
29
30
31
# File 'lib/zm/support/cache/entry.rb', line 27

def initialize(value, version: 1, expires_in: nil, expires_at: nil)
  @value      = value
  @version    = version.to_i
  @expires_at = expires_at&.to_f || (expires_in && (expires_in.to_f + Time.now.to_f))
end

Instance Attribute Details

#expires_atObject (readonly)

Returns the value of attribute expires_at.



25
26
27
# File 'lib/zm/support/cache/entry.rb', line 25

def expires_at
  @expires_at
end

#valueObject (readonly)

Returns the value of attribute value.



25
26
27
# File 'lib/zm/support/cache/entry.rb', line 25

def value
  @value
end

#versionObject (readonly)

Returns the value of attribute version.



25
26
27
# File 'lib/zm/support/cache/entry.rb', line 25

def version
  @version
end

Class Method Details

.factoryObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zm/support/cache/entry.rb', line 10

def factory
  @factory ||= MessagePack::Factory.new.tap do |msgpack_factory|
    msgpack_factory.register_type(
      0x01,
      self,
      packer: ->(entry) { entry.pack.to_msgpack },
      unpacker: lambda { |data|
        value, expires_at, version = MessagePack.unpack(data)
        new(Marshal.load(value), version: version, expires_at: expires_at)
      }
    )
  end
end

Instance Method Details

#dumpObject



57
58
59
# File 'lib/zm/support/cache/entry.rb', line 57

def dump
  self.class.factory.dump self
end

#expired?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/zm/support/cache/entry.rb', line 45

def expired?
  return false unless @expires_at

  @expires_at <= Time.now.to_f
end

#inspectObject

:nodoc:



37
38
39
# File 'lib/zm/support/cache/entry.rb', line 37

def inspect # :nodoc:
  "#<#{self.class.name} value=#{@value}, version=#{@version}, expires_at=#{@expires_at}>"
end

#mismatched?(version) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/zm/support/cache/entry.rb', line 41

def mismatched?(version)
  version && @version != version
end

#packObject



51
52
53
54
55
# File 'lib/zm/support/cache/entry.rb', line 51

def pack
  members = [Marshal.dump(@value), @expires_at, @version]
  members.pop while !members.empty? && members.last.nil?
  members
end

#to_sObject



33
34
35
# File 'lib/zm/support/cache/entry.rb', line 33

def to_s
  inspect
end