Class: Gitlab::Diff::HighlightCache
- Inherits:
-
Object
- Object
- Gitlab::Diff::HighlightCache
- Includes:
- Utils::Gzip, Utils::StrongMemoize
- Defined in:
- lib/gitlab/diff/highlight_cache.rb
Constant Summary collapse
- EXPIRATION =
1.week
- VERSION =
1
Instance Method Summary collapse
- #clear ⇒ Object
-
#decorate(diff_file) ⇒ Object
-
Reads from cache - Assigns DiffFile#highlighted_diff_lines for cached files.
-
-
#initialize(diff_collection) ⇒ HighlightCache
constructor
A new instance of HighlightCache.
- #key ⇒ Object
-
#write_if_empty ⇒ Object
For every file that isn't already contained in the redis hash, store the result of #highlighted_diff_lines, then submit the uncached content to #write_to_redis_hash to submit a single write.
Methods included from Utils::StrongMemoize
#clear_memoization, #strong_memoize, #strong_memoized?
Methods included from Utils::Gzip
#gzip_compress, #gzip_decompress
Constructor Details
#initialize(diff_collection) ⇒ HighlightCache
Returns a new instance of HighlightCache.
15 16 17 |
# File 'lib/gitlab/diff/highlight_cache.rb', line 15 def initialize(diff_collection) @diff_collection = diff_collection end |
Instance Method Details
#clear ⇒ Object
47 48 49 50 51 |
# File 'lib/gitlab/diff/highlight_cache.rb', line 47 def clear Gitlab::Redis::Cache.with do |redis| redis.del(key) end end |
#decorate(diff_file) ⇒ Object
-
Reads from cache
-
Assigns DiffFile#highlighted_diff_lines for cached files
22 23 24 25 26 27 28 |
# File 'lib/gitlab/diff/highlight_cache.rb', line 22 def decorate(diff_file) if content = read_file(diff_file) diff_file.highlighted_diff_lines = content.map do |line| Gitlab::Diff::Line.safe_init_from_hash(line) end end end |
#key ⇒ Object
53 54 55 56 57 |
# File 'lib/gitlab/diff/highlight_cache.rb', line 53 def key strong_memoize(:redis_key) do ['highlighted-diff-files', diffable.cache_key, VERSION, ].join(":") end end |
#write_if_empty ⇒ Object
For every file that isn't already contained in the redis hash, store the
result of #highlighted_diff_lines, then submit the uncached content
to #write_to_redis_hash to submit a single write. This avoids excessive
IO generated by N+1's (1 writing for each highlighted line or file).
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/gitlab/diff/highlight_cache.rb', line 35 def write_if_empty return if cacheable_files.empty? new_cache_content = {} cacheable_files.each do |diff_file| new_cache_content[diff_file.file_path] = diff_file.highlighted_diff_lines.map(&:to_hash) end write_to_redis_hash(new_cache_content) end |