Class: Zm::Support::Cache::Entry
- Inherits:
-
Object
- Object
- Zm::Support::Cache::Entry
- Defined in:
- lib/zm/support/cache/entry.rb
Instance Attribute Summary collapse
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #dump ⇒ Object
- #expired? ⇒ Boolean
-
#initialize(value, version: 1, expires_in: nil, expires_at: nil) ⇒ Entry
constructor
A new instance of Entry.
-
#inspect ⇒ Object
:nodoc:.
- #mismatched?(version) ⇒ Boolean
- #pack ⇒ Object
- #to_s ⇒ Object
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_at ⇒ Object (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 |
#value ⇒ Object (readonly)
Returns the value of attribute value.
25 26 27 |
# File 'lib/zm/support/cache/entry.rb', line 25 def value @value end |
#version ⇒ Object (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
.factory ⇒ Object
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
#dump ⇒ Object
57 58 59 |
# File 'lib/zm/support/cache/entry.rb', line 57 def dump self.class.factory.dump self end |
#expired? ⇒ 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 |
#inspect ⇒ Object
: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
41 42 43 |
# File 'lib/zm/support/cache/entry.rb', line 41 def mismatched?(version) version && @version != version end |
#pack ⇒ Object
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_s ⇒ Object
33 34 35 |
# File 'lib/zm/support/cache/entry.rb', line 33 def to_s inspect end |