2019-06-05 00:59:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module MarkdownCache
|
|
|
|
module Redis
|
|
|
|
module Extension
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
attr_reader :cached_markdown_version
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def cache_markdown_field(markdown_field, context = {})
|
|
|
|
super
|
|
|
|
|
|
|
|
# define the `[field]_html` accessor
|
|
|
|
html_field = cached_markdown_fields.html_field(markdown_field)
|
|
|
|
define_method(html_field) do
|
|
|
|
load_cached_markdown unless markdown_data_loaded?
|
|
|
|
|
|
|
|
instance_variable_get("@#{html_field}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-01 08:09:08 -04:00
|
|
|
prepended do
|
|
|
|
def self.preload_markdown_cache!(objects)
|
|
|
|
fields = Gitlab::MarkdownCache::Redis::Store.bulk_read(objects)
|
2019-06-05 00:59:48 -04:00
|
|
|
|
2020-07-01 08:09:08 -04:00
|
|
|
objects.each do |object|
|
|
|
|
fields[object.cache_key].value.each do |field_name, value|
|
|
|
|
object.write_markdown_field(field_name, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-06-05 00:59:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def write_markdown_field(field_name, value)
|
2020-07-01 08:09:08 -04:00
|
|
|
# The value read from redis is a string, so we're converting it back
|
|
|
|
# to an int.
|
|
|
|
value = value.to_i if field_name == :cached_markdown_version
|
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
instance_variable_set("@#{field_name}", value)
|
|
|
|
end
|
|
|
|
|
2020-07-01 08:09:08 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def save_markdown(updates)
|
|
|
|
markdown_store.save(updates)
|
|
|
|
end
|
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
def markdown_field_changed?(field_name)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-07-03 19:12:02 -04:00
|
|
|
def changed_attributes
|
|
|
|
{}
|
2019-06-05 00:59:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cached_markdown
|
|
|
|
@cached_data ||= markdown_store.read
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_cached_markdown
|
|
|
|
cached_markdown.each do |field_name, value|
|
|
|
|
write_markdown_field(field_name, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def markdown_data_loaded?
|
|
|
|
cached_markdown_version.present? || markdown_store.loaded?
|
|
|
|
end
|
|
|
|
|
|
|
|
def markdown_store
|
|
|
|
@store ||= Gitlab::MarkdownCache::Redis::Store.new(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|