Class: Zaikio::JWTAuth::DirectoryCache

Inherits:
Object
  • Object
show all
Defined in:
lib/zaikio/jwt_auth/directory_cache.rb

Defined Under Namespace

Classes: UpdateJob

Constant Summary collapse

BadResponseError =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.fetch(directory_path, options = {}) ⇒ Object

Retrieve some data from the Hub, reachable at directory_path. Attempts to retrieve data from a cache first (usually Redis, if configured). Caching can be skipped by setting an :invalidate option, or if the cached data is stale it will be refetched from the Hub anyway. Please note that this method can return nil if there is no cache available and the Hub is giving error responses or failures.

Examples:

Fetching revoked access token information


DirectoryCache.fetch("api/v1/revoked_access_tokens.json")


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zaikio/jwt_auth/directory_cache.rb', line 31

def fetch(directory_path, options = {})
  cache = begin
    Zaikio::JWTAuth.configuration.cache.read("zaikio::jwt_auth::#{directory_path}")
  rescue StandardError => e
    Zaikio::JWTAuth.configuration.logger
                   .warn("Error reading DirectoryCache(#{directory_path}) from Cache, falling "\
                         "back to API: #{e.inspect}")
    nil
  end

  return reload_or_enqueue(directory_path) unless cache

  json = Oj.load(cache)

  if options[:invalidate] || cache_expired?(json, options[:expires_after])
    reload_or_enqueue(directory_path)
  end || json["data"]
end

.reset(directory_path) ⇒ Object



59
60
61
# File 'lib/zaikio/jwt_auth/directory_cache.rb', line 59

def reset(directory_path)
  Zaikio::JWTAuth.configuration.cache.delete("zaikio::jwt_auth::#{directory_path}")
end

.update(directory_path, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/zaikio/jwt_auth/directory_cache.rb', line 50

def update(directory_path, options = {})
  data = fetch(directory_path, options)
  data = yield(data)
  Zaikio::JWTAuth.configuration.cache.write("zaikio::jwt_auth::#{directory_path}", {
    fetched_at: Time.now.to_i,
    data: data
  }.to_json)
end