debian-mirror-gitlab/lib/gitlab/markdown_cache/redis/store.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
1.8 KiB
Ruby
Raw Normal View History

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