gitlab-org--gitlab-foss/lib/gitlab/metrics/dashboard/cache.rb
Sarah Yasonik 15c8d29bd6 Refactor for cleaner caching in dashboards
Opts to cache a full list of cached dashboards
to better manage removing items from the cache.
This also allows dashboards to be stored in the
cache that don't necessarily correspond to a
single dashboard yml.
2019-06-13 16:45:36 +00:00

44 lines
1 KiB
Ruby

# frozen_string_literal: true
require 'set'
module Gitlab
module Metrics
module Dashboard
class Cache
CACHE_KEYS = 'all_cached_metric_dashboards'
class << self
# Stores a dashboard in the cache, documenting the key
# so the cached can be cleared in bulk at another time.
def fetch(key)
register_key(key)
Rails.cache.fetch(key) { yield }
end
# Resets all dashboard caches, such that all
# dashboard content will be loaded from source on
# subsequent dashboard calls.
def delete_all!
all_keys.each { |key| Rails.cache.delete(key) }
Rails.cache.delete(CACHE_KEYS)
end
private
def register_key(key)
new_keys = all_keys.add(key).to_a.join('|')
Rails.cache.write(CACHE_KEYS, new_keys)
end
def all_keys
Set.new(Rails.cache.read(CACHE_KEYS)&.split('|'))
end
end
end
end
end
end