Class: Shugoi::GuardCache

Inherits:
Object
  • Object
show all
Defined in:
lib/shugoi/guard_cache.rb

Overview

Cache mémoire des guards (guard-detect) fetchés depuis l'API. Parité avec _guardCaches (render.ts).

Constant Summary collapse

TTL_MS =
300_000

Instance Method Summary collapse

Constructor Details

#initialize(api_client) ⇒ GuardCache

Returns a new instance of GuardCache.



11
12
13
14
15
16
# File 'lib/shugoi/guard_cache.rb', line 11

def initialize(api_client)
  @api_client = api_client
  @detect = nil
  @fetched_at = 0
  @mutex = Mutex.new
end

Instance Method Details

#detectObject



18
19
20
# File 'lib/shugoi/guard_cache.rb', line 18

def detect
  @detect
end

#ensure_ready(site_key, secret = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/shugoi/guard_cache.rb', line 22

def ensure_ready(site_key, secret = nil)
  return if @detect && Utils.now_ms - @fetched_at < TTL_MS

  @mutex.synchronize do
    return if @detect && Utils.now_ms - @fetched_at < TTL_MS
    code = @api_client.fetch_guard_detect(site_key, secret)
    @detect = code && !code.empty? ? code : 'console.error("Shugoi guard-detect unavailable")'
    @fetched_at = Utils.now_ms
  end
end