Class: Philiprehberger::ExpiringMap::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/expiring_map/entry.rb

Overview

Internal entry storing a value with its expiration time

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, expires_at) ⇒ Entry

Returns a new instance of Entry.

Parameters:

  • value (Object)

    the stored value

  • expires_at (Float)

    monotonic time when the entry expires



9
10
11
12
# File 'lib/philiprehberger/expiring_map/entry.rb', line 9

def initialize(value, expires_at)
  @value = value
  @expires_at = expires_at
end

Instance Attribute Details

#expires_atFloat

Returns monotonic time when the entry expires.

Returns:

  • (Float)

    monotonic time when the entry expires



18
19
20
# File 'lib/philiprehberger/expiring_map/entry.rb', line 18

def expires_at
  @expires_at
end

#valueObject (readonly)

Returns the stored value.

Returns:

  • (Object)

    the stored value



15
16
17
# File 'lib/philiprehberger/expiring_map/entry.rb', line 15

def value
  @value
end

Instance Method Details

#expired?Boolean

Check if the entry has expired

Returns:

  • (Boolean)

    true if expired



23
24
25
# File 'lib/philiprehberger/expiring_map/entry.rb', line 23

def expired?
  Process.clock_gettime(Process::CLOCK_MONOTONIC) >= @expires_at
end

#ttlFloat

Return remaining TTL in seconds

Returns:

  • (Float)

    seconds until expiration, 0 if expired



30
31
32
33
# File 'lib/philiprehberger/expiring_map/entry.rb', line 30

def ttl
  remaining = @expires_at - Process.clock_gettime(Process::CLOCK_MONOTONIC)
  remaining.positive? ? remaining : 0.0
end