Class: Phoenix::Inbox

Inherits:
Object
  • Object
show all
Includes:
Mutex_m
Defined in:
lib/yatapp/inbox.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttl:) ⇒ Inbox



8
9
10
11
12
13
# File 'lib/yatapp/inbox.rb', line 8

def initialize(ttl:)
  @ttl = ttl
  @bucket = Time.now.to_i / ttl
  @data = Hash.new { |h, k| h[k] = {} }
  super()
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/yatapp/inbox.rb', line 7

def data
  @data
end

#ttlObject (readonly)

Returns the value of attribute ttl.



7
8
9
# File 'lib/yatapp/inbox.rb', line 7

def ttl
  @ttl
end

Instance Method Details

#current_timestampObject



41
42
43
# File 'lib/yatapp/inbox.rb', line 41

def current_timestamp
  Time.now.to_i / ttl
end

#key?(key) ⇒ Boolean



37
38
39
# File 'lib/yatapp/inbox.rb', line 37

def key?(key)
  data.values.any? { |v| v.key?(key) }
end

#pop(key) ⇒ Object Also known as: delete



28
29
30
31
32
33
# File 'lib/yatapp/inbox.rb', line 28

def pop(key)
  synchronize do
    ts = current_timestamp
    data[ts - 1].delete(key) { data[ts].delete(key) { yield }}
  end
end

#push(key, val) ⇒ Object Also known as: []=



15
16
17
18
19
20
21
22
23
24
# File 'lib/yatapp/inbox.rb', line 15

def push(key, val)
  synchronize do
    ts = current_timestamp
    (data[ts][key] = val).tap do
      if data.keys.size >= 3
        data.delete_if { |key, _| key < (ts - 1) }
      end
    end
  end
end