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.

67 lines
1.4 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|
r.pipelined do
subjects.each do |subject|
results[subject.cache_key] = new(subject).read
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
Gitlab::Redis::Cache.with do |r|
r.mapped_hmset(markdown_cache_key, updates)
r.expire(markdown_cache_key, EXPIRES_IN)
end
end
def read
@loaded = true
2020-07-28 23:09:34 +05:30
Gitlab::Redis::Cache.with do |r|
2019-09-04 21:01:54 +05:30
r.mapped_hmget(markdown_cache_key, *fields)
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
end
end
end
end