File: container.rb

package info (click to toggle)
ruby-circuitbox 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 140 kB
  • sloc: ruby: 533; makefile: 4
file content (30 lines) | stat: -rw-r--r-- 654 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

require_relative '../time_helper/monotonic'

class Circuitbox
  class MemoryStore
    class Container
      include TimeHelper::Monotonic

      attr_accessor :value

      def initialize(value:, expiry: 0)
        @value = value
        expires_after(expiry)
      end

      def expired?
        @expires_after.positive? && @expires_after < current_second
      end

      def expired_at?(clock_second)
        @expires_after.positive? && @expires_after < clock_second
      end

      def expires_after(seconds = 0)
        @expires_after = seconds.zero? ? seconds : current_second + seconds
      end
    end
  end
end