Module: Motor::Configs::LoadFromCache

Defined in:
lib/motor/configs/load_from_cache.rb

Constant Summary collapse

CACHE_HASH =
HashWithIndifferentAccess.new

Class Method Summary collapse

Class Method Details

.cache_keys_sqlObject


91
92
93
94
95
96
97
98
99
100
# File 'lib/motor/configs/load_from_cache.rb', line 91

def cache_keys_sql
  [
    Motor::Config.select("'configs', MAX(updated_at)").to_sql,
    Motor::Resource.select("'resources', MAX(updated_at)").to_sql,
    Motor::Dashboard.select("'dashboards', MAX(updated_at)").to_sql,
    Motor::Alert.select("'alerts', MAX(updated_at)").to_sql,
    Motor::Query.select("'queries', MAX(updated_at)").to_sql,
    Motor::Form.select("'forms', MAX(updated_at)").to_sql
  ].join(' UNION ')
end

.callObject


10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/motor/configs/load_from_cache.rb', line 10

def call
  cache_keys = load_cache_keys

  {
    configs: load_configs(cache_key: cache_keys[:configs]),
    resources: load_resources(cache_key: cache_keys[:resources]),
    queries: load_queries(cache_key: cache_keys[:queries]),
    dashboards: load_dashboards(cache_key: cache_keys[:dashboards]),
    alerts: load_alerts(cache_key: cache_keys[:alerts]),
    forms: load_forms(cache_key: cache_keys[:forms])
  }
end

.load_alerts(cache_key: nil, current_ability: nil) ⇒ Object


53
54
55
56
57
58
59
60
# File 'lib/motor/configs/load_from_cache.rb', line 53

def load_alerts(cache_key: nil, current_ability: nil)
  maybe_fetch_from_cache('alerts', cache_key) do
    rel = Motor::Alert.all.active.preload(:tags)
    rel = rel.accessible_by(current_ability) if current_ability

    rel.load
  end
end

.load_cache_keysObject


85
86
87
88
89
# File 'lib/motor/configs/load_from_cache.rb', line 85

def load_cache_keys
  result = ActiveRecord::Base.connection.exec_query(cache_keys_sql).rows

  result.to_h.with_indifferent_access
end

.load_configs(cache_key: nil) ⇒ Object


23
24
25
26
27
# File 'lib/motor/configs/load_from_cache.rb', line 23

def load_configs(cache_key: nil)
  maybe_fetch_from_cache('configs', cache_key) do
    Motor::Config.all.load
  end
end

.load_dashboards(cache_key: nil, current_ability: nil) ⇒ Object


44
45
46
47
48
49
50
51
# File 'lib/motor/configs/load_from_cache.rb', line 44

def load_dashboards(cache_key: nil, current_ability: nil)
  maybe_fetch_from_cache('dashboards', cache_key) do
    rel = Motor::Dashboard.all.active.preload(:tags)
    rel = rel.accessible_by(current_ability) if current_ability

    rel.load
  end
end

.load_forms(cache_key: nil, current_ability: nil) ⇒ Object


62
63
64
65
66
67
68
69
# File 'lib/motor/configs/load_from_cache.rb', line 62

def load_forms(cache_key: nil, current_ability: nil)
  maybe_fetch_from_cache('forms', cache_key) do
    rel = Motor::Form.all.active.preload(:tags)
    rel = rel.accessible_by(current_ability) if current_ability

    rel.load
  end
end

.load_queries(cache_key: nil, current_ability: nil) ⇒ Object


35
36
37
38
39
40
41
42
# File 'lib/motor/configs/load_from_cache.rb', line 35

def load_queries(cache_key: nil, current_ability: nil)
  maybe_fetch_from_cache('queries', cache_key) do
    rel = Motor::Query.all.active.preload(:tags)
    rel = rel.accessible_by(current_ability) if current_ability

    rel.load
  end
end

.load_resources(cache_key: nil) ⇒ Object


29
30
31
32
33
# File 'lib/motor/configs/load_from_cache.rb', line 29

def load_resources(cache_key: nil)
  maybe_fetch_from_cache('resources', cache_key) do
    Motor::Resource.all.load
  end
end

.maybe_fetch_from_cache(type, cache_key) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/motor/configs/load_from_cache.rb', line 71

def maybe_fetch_from_cache(type, cache_key)
  return yield unless cache_key

  if CACHE_HASH[type] && CACHE_HASH[type][:key] == cache_key
    CACHE_HASH[type][:value]
  else
    result = yield

    CACHE_HASH[type] = { key: cache_key, value: result }

    result
  end
end