Class: Parse::Webhooks::ReplayProtection::LruCache

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/parse/webhooks/replay_protection.rb

Overview

Bounded, thread-safe LRU keyed on a digest string with per-entry insertion timestamps. Used only by ReplayProtection; intentionally private to avoid leaking another caching primitive into the public API. Ruby Hashes preserve insertion order, so a delete+insert on touch is enough to maintain LRU ordering.

Instance Method Summary collapse

Constructor Details

#initializeLruCache

Returns a new instance of LruCache.



150
151
152
153
# File 'lib/parse/webhooks/replay_protection.rb', line 150

def initialize
  super()
  @entries = {}
end

Instance Method Details

#clearObject



179
180
181
# File 'lib/parse/webhooks/replay_protection.rb', line 179

def clear
  synchronize { @entries.clear }
end

#record(key, max_size) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/parse/webhooks/replay_protection.rb', line 169

def record(key, max_size)
  synchronize do
    @entries.delete(key)
    @entries[key] = Time.now.to_i
    while @entries.size > max_size
      @entries.shift
    end
  end
end

#seen?(key, window_seconds) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/parse/webhooks/replay_protection.rb', line 155

def seen?(key, window_seconds)
  synchronize do
    ts = @entries[key]
    return false unless ts
    if Time.now.to_i - ts > window_seconds
      @entries.delete(key)
      return false
    end
    @entries.delete(key)
    @entries[key] = ts # touch
    true
  end
end

#sizeObject



183
184
185
# File 'lib/parse/webhooks/replay_protection.rb', line 183

def size
  synchronize { @entries.size }
end