2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-04 13:23:08 -04:00
|
|
|
module CacheableAttributes
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
after_commit { self.class.expire }
|
2019-08-07 14:40:36 -04:00
|
|
|
|
|
|
|
private_class_method :request_store_cache_key
|
2018-05-04 13:23:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
2018-05-29 11:49:52 -04:00
|
|
|
def cache_key
|
2019-08-31 15:57:00 -04:00
|
|
|
"#{name}:#{Gitlab::VERSION}:#{Rails.version}"
|
2018-05-29 11:49:52 -04:00
|
|
|
end
|
|
|
|
|
2018-10-30 06:53:01 -04:00
|
|
|
# Can be overridden
|
2018-05-04 13:23:08 -04:00
|
|
|
def current_without_cache
|
|
|
|
last
|
|
|
|
end
|
|
|
|
|
2018-10-30 06:53:01 -04:00
|
|
|
# Can be overridden
|
2018-05-04 13:23:08 -04:00
|
|
|
def defaults
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_from_defaults(attributes = {})
|
2018-12-14 09:42:07 -05:00
|
|
|
final_attributes = defaults
|
|
|
|
.merge(attributes)
|
|
|
|
.stringify_keys
|
|
|
|
.slice(*column_names)
|
|
|
|
|
|
|
|
new(final_attributes)
|
2018-05-04 13:23:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cached
|
2019-08-07 14:40:36 -04:00
|
|
|
Gitlab::SafeRequestStore[request_store_cache_key] ||= retrieve_from_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_store_cache_key
|
|
|
|
:"#{name}_cached_attributes"
|
2018-05-29 11:49:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_from_cache
|
2019-07-01 15:17:37 -04:00
|
|
|
record = cache_backend.read(cache_key)
|
2018-05-29 11:49:52 -04:00
|
|
|
ensure_cache_setup if record.present?
|
2018-05-04 13:23:08 -04:00
|
|
|
|
2018-05-29 11:49:52 -04:00
|
|
|
record
|
2018-05-04 13:23:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def current
|
|
|
|
cached_record = cached
|
|
|
|
return cached_record if cached_record.present?
|
|
|
|
|
|
|
|
current_without_cache.tap { |current_record| current_record&.cache! }
|
2018-05-29 11:49:52 -04:00
|
|
|
rescue => e
|
|
|
|
if Rails.env.production?
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger.warn("Cached record for #{name} couldn't be loaded, falling back to uncached record: #{e}") # rubocop:disable Gitlab/RailsLogger
|
2018-05-29 11:49:52 -04:00
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
2018-05-04 13:23:08 -04:00
|
|
|
# Fall back to an uncached value if there are any problems (e.g. Redis down)
|
|
|
|
current_without_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def expire
|
2019-08-07 14:40:36 -04:00
|
|
|
Gitlab::SafeRequestStore.delete(request_store_cache_key)
|
2019-07-01 15:17:37 -04:00
|
|
|
cache_backend.delete(cache_key)
|
2018-05-04 13:23:08 -04:00
|
|
|
rescue
|
|
|
|
# Gracefully handle when Redis is not available. For example,
|
|
|
|
# omnibus may fail here during gitlab:assets:compile.
|
|
|
|
end
|
2018-05-29 11:49:52 -04:00
|
|
|
|
|
|
|
def ensure_cache_setup
|
|
|
|
# This is a workaround for a Rails bug that causes attribute methods not
|
|
|
|
# to be loaded when read from cache: https://github.com/rails/rails/issues/27348
|
|
|
|
define_attribute_methods
|
|
|
|
end
|
2019-07-01 15:17:37 -04:00
|
|
|
|
|
|
|
def cache_backend
|
|
|
|
Rails.cache
|
|
|
|
end
|
2018-05-04 13:23:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache!
|
2019-07-01 15:17:37 -04:00
|
|
|
self.class.cache_backend.write(self.class.cache_key, self, expires_in: 1.minute)
|
2018-05-04 13:23:08 -04:00
|
|
|
end
|
|
|
|
end
|