Class: Doorkeeper::DocumentCache

Inherits:
Object
  • Object
show all
Defined in:
lib/doorkeeper/document_cache.rb

Overview

A small thread-safe, fixed-TTL, in-memory memo keyed by URL. It exists so one authorization flow (authorize GET, consent POST, token exchange) does not refetch the same URL several times within a few seconds; it deliberately implements no HTTP caching semantics.

Only successfully fetched and validated values may be stored — an error response or a malformed document must never be cached — which is guaranteed by callers never yielding anything but a validated value.

Constant Summary collapse

DEFAULT_TTL =
60
MAX_ENTRIES =
500

Instance Method Summary collapse

Constructor Details

#initialize(ttl: DEFAULT_TTL) ⇒ DocumentCache

Returns a new instance of DocumentCache.



16
17
18
19
20
# File 'lib/doorkeeper/document_cache.rb', line 16

def initialize(ttl: DEFAULT_TTL)
  @ttl = ttl
  @mutex = Mutex.new
  @store = {}
end

Instance Method Details

#clearObject



33
34
35
# File 'lib/doorkeeper/document_cache.rb', line 33

def clear
  @mutex.synchronize { @store.clear }
end

#fetch(url) ⇒ Object

Returns the cached document for the URL, or stores and returns the block's result. The block's failures (raises, nil) are not cached.



24
25
26
27
28
29
30
31
# File 'lib/doorkeeper/document_cache.rb', line 24

def fetch(url)
  cached = read(url)
  return cached if cached

  document = yield
  write(url, document) if document
  document
end