2019-06-05 00:59:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module MarkdownCache
|
|
|
|
module Redis
|
|
|
|
class Store
|
|
|
|
EXPIRES_IN = 1.day
|
|
|
|
|
2020-07-01 08:09:08 -04:00
|
|
|
def self.bulk_read(subjects)
|
|
|
|
results = {}
|
|
|
|
|
|
|
|
Gitlab::Redis::Cache.with do |r|
|
2022-08-19 11:11:58 -04:00
|
|
|
r.pipelined do |pipeline|
|
2020-07-01 08:09:08 -04:00
|
|
|
subjects.each do |subject|
|
2022-08-19 11:11:58 -04:00
|
|
|
results[subject.cache_key] = new(subject).read(pipeline)
|
2020-07-01 08:09:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
def initialize(subject)
|
|
|
|
@subject = subject
|
|
|
|
@loaded = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def save(updates)
|
|
|
|
@loaded = false
|
|
|
|
|
2022-11-09 13:07:50 -05:00
|
|
|
with_redis do |r|
|
2019-06-05 00:59:48 -04:00
|
|
|
r.mapped_hmset(markdown_cache_key, updates)
|
|
|
|
r.expire(markdown_cache_key, EXPIRES_IN)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-19 11:11:58 -04:00
|
|
|
def read(pipeline = nil)
|
2019-06-05 00:59:48 -04:00
|
|
|
@loaded = true
|
|
|
|
|
2022-08-19 11:11:58 -04:00
|
|
|
if pipeline
|
|
|
|
pipeline.mapped_hmget(markdown_cache_key, *fields)
|
|
|
|
else
|
2022-11-09 13:07:50 -05:00
|
|
|
with_redis do |r|
|
2022-08-19 11:11:58 -04:00
|
|
|
r.mapped_hmget(markdown_cache_key, *fields)
|
|
|
|
end
|
2019-06-05 00:59:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def loaded?
|
|
|
|
@loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fields
|
|
|
|
@fields ||= @subject.cached_markdown_fields.html_fields + [:cached_markdown_version]
|
|
|
|
end
|
|
|
|
|
|
|
|
def markdown_cache_key
|
2019-06-05 08:06:37 -04:00
|
|
|
unless @subject.respond_to?(:cache_key)
|
2019-06-05 00:59:48 -04:00
|
|
|
raise Gitlab::MarkdownCache::UnsupportedClassError,
|
2019-06-05 08:06:37 -04:00
|
|
|
"This class has no cache_key to use for caching"
|
2019-06-05 00:59:48 -04:00
|
|
|
end
|
|
|
|
|
2019-06-05 01:50:37 -04:00
|
|
|
"markdown_cache:#{@subject.cache_key}"
|
2019-06-05 00:59:48 -04:00
|
|
|
end
|
2022-11-09 13:07:50 -05:00
|
|
|
|
|
|
|
def with_redis(&block)
|
|
|
|
Gitlab::Redis::Cache.with(&block) # rubocop:disable CodeReuse/ActiveRecord
|
|
|
|
end
|
2019-06-05 00:59:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|