Commit 0c00fa10 authored by Kerri Miller's avatar Kerri Miller

Cache redis loading and streamline method calls

parent dc6522b8
...@@ -11,6 +11,7 @@ module Gitlab ...@@ -11,6 +11,7 @@ module Gitlab
def initialize(diff_collection, backend: Rails.cache) def initialize(diff_collection, backend: Rails.cache)
@backend = backend @backend = backend
@diff_collection = diff_collection @diff_collection = diff_collection
@redis_key = diffable.cache_key if Feature.enabled?(:redis_diff_caching)
end end
# - Reads from cache # - Reads from cache
...@@ -58,32 +59,30 @@ module Gitlab ...@@ -58,32 +59,30 @@ module Gitlab
# ...it will write/update a Redis hash (HSET) # ...it will write/update a Redis hash (HSET)
# #
def write_to_redis_hash(hash) def write_to_redis_hash(hash)
key = diffable.cache_key return unless @redis_key
if key Redis::Cache.with do |redis|
Redis::Cache.with do |redis| redis.multi do |multi|
redis.multi do |multi| hash.each do |diff_file_id, highlighted_diff_lines_hash|
hash.each do |diff_file_id, highlighted_diff_lines_hash| multi.hset(@redis_key, diff_file_id, highlighted_diff_lines_hash.to_json)
multi.hset(key, diff_file_id, highlighted_diff_lines_hash.to_json)
# HSETs have to have their expiration date manually updated
# HSETs have to have their expiration date manually updated #
# multi.expire(@redis_key, EXPIRATION)
multi.expire(key, EXPIRATION)
end
end end
end end
end end
end end
def read_entire_redis_hash(key) def read_entire_redis_hash
Redis::Cache.with do |redis| Redis::Cache.with do |redis|
redis.hgetall(key) redis.hgetall(@redis_key)
end end
end end
def read_single_entry_from_redis_hash(key, diff_file_id) def read_single_entry_from_redis_hash(diff_file_id)
Redis::Cache.with do |redis| Redis::Cache.with do |redis|
redis.hget(key, diff_file_id) redis.hget(@redis_key, diff_file_id)
end end
end end
...@@ -106,7 +105,15 @@ module Gitlab ...@@ -106,7 +105,15 @@ module Gitlab
end end
def cached_content def cached_content
@cached_content ||= cache.read(key) || {} @cached_content ||= populate_cached_content || {}
end
def populate_cached_content
if Feature.enabled?(:redis_diff_caching)
read_entire_redis_hash
else
cache.read(key)
end
end end
def cacheable?(diff_file) def cacheable?(diff_file)
......
...@@ -53,6 +53,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do ...@@ -53,6 +53,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
fallback_diff_refs: diffs.fallback_diff_refs) fallback_diff_refs: diffs.fallback_diff_refs)
end end
it 'does not calculate highlighting when reading from cache' do it 'does not calculate highlighting when reading from cache' do
cache.write_if_empty cache.write_if_empty
cache.decorate(diff_file) cache.decorate(diff_file)
...@@ -69,11 +70,18 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do ...@@ -69,11 +70,18 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
expect(diff_file.highlighted_diff_lines.size).to be > 5 expect(diff_file.highlighted_diff_lines.size).to be > 5
end end
it 'submits a single reading from the cache' do context 'when :redis_diff_caching is not enabled' do
cache.decorate(diff_file) before do
cache.decorate(diff_file) expect(Feature).to receive(:enabled?).with(:redis_diff_caching).and_return(false)
end
it 'submits a single reading from the cache' do
expect(Feature).to receive(:enabled?).with(:redis_diff_caching).at_least(:once).and_return(false)
2.times { cache.decorate(diff_file) }
expect(backend).to have_received(:read).with(cache.key).once expect(backend).to have_received(:read).with(cache.key).once
end
end end
end end
...@@ -94,7 +102,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do ...@@ -94,7 +102,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
context 'when :redis_diff_caching is not enabled' do context 'when :redis_diff_caching is not enabled' do
before do before do
expect(Feature).to receive(:enabled?).with(:redis_diff_caching).and_return(false) expect(Feature).to receive(:enabled?).with(:redis_diff_caching).at_least(:once).and_return(false)
end end
it 'submits a single writing to the cache' do it 'submits a single writing to the cache' do
...@@ -126,7 +134,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do ...@@ -126,7 +134,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
end end
it 'returns the entire contents of a Redis hash as JSON' do it 'returns the entire contents of a Redis hash as JSON' do
result = cache.read_entire_redis_hash(cache_key) result = cache.read_entire_redis_hash
expect(result.values.first).to eq(diff_hash.values.first.to_json) expect(result.values.first).to eq(diff_hash.values.first.to_json)
end end
...@@ -142,7 +150,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do ...@@ -142,7 +150,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
it 'returns highlighted diff content for a single file as JSON' do it 'returns highlighted diff content for a single file as JSON' do
diff_hash.each do |file_path, value| diff_hash.each do |file_path, value|
found = cache.read_single_entry_from_redis_hash(cache_key, file_path) found = cache.read_single_entry_from_redis_hash(file_path)
expect(found).to eq(value.to_json) expect(found).to eq(value.to_json)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment