Class: Parse::Webhooks::ReplayProtection::LruCache
- Inherits:
-
Object
- Object
- Parse::Webhooks::ReplayProtection::LruCache
- 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
- #clear ⇒ Object
-
#initialize ⇒ LruCache
constructor
A new instance of LruCache.
- #record(key, max_size) ⇒ Object
- #seen?(key, window_seconds) ⇒ Boolean
- #size ⇒ Object
Constructor Details
#initialize ⇒ LruCache
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
#clear ⇒ Object
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
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 |
#size ⇒ Object
183 184 185 |
# File 'lib/parse/webhooks/replay_protection.rb', line 183 def size synchronize { @entries.size } end |