Module: Legion::Extensions::Github::Helpers::Cache

Constant Summary collapse

DEFAULT_TTLS =
{
  repo: 600, issue: 120, pull_request: 60, commit: 86_400,
  branch: 120, user: 3600, org: 3600, search: 60
}.freeze
DEFAULT_TTL =
300

Instance Method Summary collapse

Instance Method Details

#cache_connected?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/legion/extensions/github/helpers/cache.rb', line 63

def cache_connected?
  ::Legion::Cache.connected?
rescue StandardError => _e
  false
end

#cache_invalidate(cache_key) ⇒ Object



43
44
45
46
# File 'lib/legion/extensions/github/helpers/cache.rb', line 43

def cache_invalidate(cache_key)
  cache_delete(cache_key) if cache_connected?
  local_cache_delete(cache_key) if local_cache_connected?
end

#cache_write(cache_key, value, ttl: nil) ⇒ Object



37
38
39
40
41
# File 'lib/legion/extensions/github/helpers/cache.rb', line 37

def cache_write(cache_key, value, ttl: nil)
  effective_ttl = ttl || github_ttl_for(cache_key)
  cache_set(cache_key, value, ttl: effective_ttl) if cache_connected?
  local_cache_set(cache_key, value, ttl: effective_ttl) if local_cache_connected?
end

#cached_get(cache_key, ttl: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/legion/extensions/github/helpers/cache.rb', line 19

def cached_get(cache_key, ttl: nil)
  if cache_connected?
    result = cache_get(cache_key)
    return result if result
  end

  if local_cache_connected?
    result = local_cache_get(cache_key)
    return result if result
  end

  result = yield
  effective_ttl = ttl || github_ttl_for(cache_key)
  cache_set(cache_key, result, ttl: effective_ttl) if cache_connected?
  local_cache_set(cache_key, result, ttl: effective_ttl) if local_cache_connected?
  result
end

#github_ttl_for(cache_key) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/legion/extensions/github/helpers/cache.rb', line 48

def github_ttl_for(cache_key)
  configured_ttls = github_cache_ttls
  case cache_key
  when /:commits:/ then configured_ttls[:commit]
  when /:pulls:/   then configured_ttls[:pull_request]
  when /:issues:/  then configured_ttls[:issue]
  when /:branches:/ then configured_ttls[:branch]
  when /\Agithub:user:/ then configured_ttls[:user]
  when /\Agithub:org:/  then configured_ttls[:org]
  when /\Agithub:repo:[^:]+\z/ then configured_ttls[:repo]
  when /:search:/ then configured_ttls[:search]
  else configured_ttls.fetch(:default, DEFAULT_TTL)
  end
end

#local_cache_connected?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/legion/extensions/github/helpers/cache.rb', line 69

def local_cache_connected?
  false
end

#local_cache_delete(_key) ⇒ Object



81
82
83
# File 'lib/legion/extensions/github/helpers/cache.rb', line 81

def local_cache_delete(_key)
  nil
end

#local_cache_get(_key) ⇒ Object



73
74
75
# File 'lib/legion/extensions/github/helpers/cache.rb', line 73

def local_cache_get(_key)
  nil
end

#local_cache_set(_key, _value, ttl: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



77
78
79
# File 'lib/legion/extensions/github/helpers/cache.rb', line 77

def local_cache_set(_key, _value, ttl: nil) # rubocop:disable Lint/UnusedMethodArgument
  nil
end