debian-mirror-gitlab/app/models/concerns/cacheable_attributes.rb

89 lines
2.1 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
module CacheableAttributes
extend ActiveSupport::Concern
included do
after_commit { self.class.expire }
2019-10-12 21:52:04 +05:30
private_class_method :request_store_cache_key
2018-11-08 19:23:39 +05:30
end
class_methods do
def cache_key
2019-12-04 20:38:33 +05:30
"#{name}:#{Gitlab::VERSION}:#{Rails.version}"
2018-11-08 19:23:39 +05:30
end
2018-12-13 13:39:08 +05:30
# Can be overridden
2018-11-08 19:23:39 +05:30
def current_without_cache
last
end
2018-12-13 13:39:08 +05:30
# Can be overridden
2018-11-08 19:23:39 +05:30
def defaults
{}
end
def build_from_defaults(attributes = {})
2019-02-15 15:39:39 +05:30
final_attributes = defaults
.merge(attributes)
.stringify_keys
.slice(*column_names)
new(final_attributes)
2018-11-08 19:23:39 +05:30
end
def cached
2019-10-12 21:52:04 +05:30
Gitlab::SafeRequestStore[request_store_cache_key] ||= retrieve_from_cache
end
def request_store_cache_key
:"#{name}_cached_attributes"
2018-11-08 19:23:39 +05:30
end
def retrieve_from_cache
2019-09-30 21:07:59 +05:30
record = cache_backend.read(cache_key)
2018-11-08 19:23:39 +05:30
ensure_cache_setup if record.present?
record
end
def current
cached_record = cached
return cached_record if cached_record.present?
current_without_cache.tap { |current_record| current_record&.cache! }
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2018-11-08 19:23:39 +05:30
if Rails.env.production?
2020-06-23 00:09:42 +05:30
Gitlab::AppLogger.warn("Cached record for #{name} couldn't be loaded, falling back to uncached record: #{e}")
2018-11-08 19:23:39 +05:30
else
raise e
end
# Fall back to an uncached value if there are any problems (e.g. Redis down)
current_without_cache
end
def expire
2019-10-12 21:52:04 +05:30
Gitlab::SafeRequestStore.delete(request_store_cache_key)
2019-09-30 21:07:59 +05:30
cache_backend.delete(cache_key)
2021-06-08 01:23:25 +05:30
rescue StandardError
2018-11-08 19:23:39 +05:30
# Gracefully handle when Redis is not available. For example,
# omnibus may fail here during gitlab:assets:compile.
end
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-09-30 21:07:59 +05:30
def cache_backend
Rails.cache
end
2018-11-08 19:23:39 +05:30
end
def cache!
2021-03-11 19:13:27 +05:30
self.class.cache_backend.write(self.class.cache_key, self, expires_in: Gitlab.config.gitlab['application_settings_cache_seconds'] || 60)
2018-11-08 19:23:39 +05:30
end
end