Commit 56d52340 authored by Patrick Bajao's avatar Patrick Bajao

Use #cache_key of subject for generated redis key

This commit also includes some changes in specs to use
`Class.new` approach.
parent 2eecfd8f
...@@ -48,7 +48,7 @@ module Gitlab ...@@ -48,7 +48,7 @@ module Gitlab
"This class has no id to use for caching" "This class has no id to use for caching"
end end
"markdown_cache:#{@subject.class}:#{@subject.id}" "markdown_cache:#{@subject.cache_key}"
end end
end end
end end
......
...@@ -2,17 +2,19 @@ ...@@ -2,17 +2,19 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::MarkdownCache::ActiveRecord::Extension do describe Gitlab::MarkdownCache::ActiveRecord::Extension do
class ARThingWithMarkdownFields < ActiveRecord::Base let(:klass) do
self.table_name = 'issues' Class.new(ActiveRecord::Base) do
include CacheMarkdownField self.table_name = 'issues'
cache_markdown_field :title, whitelisted: true include CacheMarkdownField
cache_markdown_field :description, pipeline: :single_line cache_markdown_field :title, whitelisted: true
cache_markdown_field :description, pipeline: :single_line
attr_accessor :author, :project attr_accessor :author, :project
end
end end
let(:cache_version) { Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16 } let(:cache_version) { Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16 }
let(:thing) { ARThingWithMarkdownFields.new(title: markdown, title_html: html, cached_markdown_version: cache_version) } let(:thing) { klass.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }
let(:markdown) { '`Foo`' } let(:markdown) { '`Foo`' }
let(:html) { '<p data-sourcepos="1:1-1:5" dir="auto"><code>Foo</code></p>' } let(:html) { '<p data-sourcepos="1:1-1:5" dir="auto"><code>Foo</code></p>' }
...@@ -21,7 +23,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do ...@@ -21,7 +23,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do
let(:updated_html) { '<p data-sourcepos="1:1-1:5" dir="auto"><code>Bar</code></p>' } let(:updated_html) { '<p data-sourcepos="1:1-1:5" dir="auto"><code>Bar</code></p>' }
context 'an unchanged markdown field' do context 'an unchanged markdown field' do
let(:thing) { ARThingWithMarkdownFields.new(title: markdown) } let(:thing) { klass.new(title: markdown) }
before do before do
thing.title = thing.title thing.title = thing.title
...@@ -35,7 +37,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do ...@@ -35,7 +37,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do
end end
context 'a changed markdown field' do context 'a changed markdown field' do
let(:thing) { ARThingWithMarkdownFields.new(title: markdown, title_html: html, cached_markdown_version: cache_version) } let(:thing) { klass.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }
before do before do
thing.title = updated_markdown thing.title = updated_markdown
...@@ -67,7 +69,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do ...@@ -67,7 +69,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do
end end
context 'a non-markdown field changed' do context 'a non-markdown field changed' do
let(:thing) { ARThingWithMarkdownFields.new(title: markdown, title_html: html, cached_markdown_version: cache_version) } let(:thing) { klass.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }
before do before do
thing.state = 'closed' thing.state = 'closed'
...@@ -81,7 +83,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do ...@@ -81,7 +83,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do
end end
context 'version is out of date' do context 'version is out of date' do
let(:thing) { ARThingWithMarkdownFields.new(title: updated_markdown, title_html: html, cached_markdown_version: nil) } let(:thing) { klass.new(title: updated_markdown, title_html: html, cached_markdown_version: nil) }
before do before do
thing.save thing.save
...@@ -122,7 +124,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do ...@@ -122,7 +124,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do
end end
describe '#cached_html_up_to_date?' do describe '#cached_html_up_to_date?' do
let(:thing) { ARThingWithMarkdownFields.create(title: updated_markdown, title_html: html, cached_markdown_version: nil) } let(:thing) { klass.create(title: updated_markdown, title_html: html, cached_markdown_version: nil) }
subject { thing.cached_html_up_to_date?(:title) } subject { thing.cached_html_up_to_date?(:title) }
it 'returns false if markdown has been changed but html has not' do it 'returns false if markdown has been changed but html has not' do
......
...@@ -2,30 +2,34 @@ ...@@ -2,30 +2,34 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::MarkdownCache::Redis::Extension, :clean_gitlab_redis_cache do describe Gitlab::MarkdownCache::Redis::Extension, :clean_gitlab_redis_cache do
class ThingWithMarkdownFields let(:klass) do
include CacheMarkdownField Class.new do
include CacheMarkdownField
def initialize(title: nil, description: nil) def initialize(title: nil, description: nil)
@title, @description = title, description @title, @description = title, description
end end
attr_reader :title, :description
attr_reader :title, :description cache_markdown_field :title, pipeline: :single_line
cache_markdown_field :description
cache_markdown_field :title, pipeline: :single_line def id
cache_markdown_field :description "test-markdown-cache"
end
def id def cache_key
"test-markdown-cache" "cache-key"
end
end end
end end
let(:cache_version) { Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16 } let(:cache_version) { Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16 }
let(:thing) { ThingWithMarkdownFields.new(title: "`Hello`", description: "`World`") } let(:thing) { klass.new(title: "`Hello`", description: "`World`") }
let(:expected_cache_key) { "markdown_cache:ThingWithMarkdownFields:test-markdown-cache" } let(:expected_cache_key) { "markdown_cache:cache-key" }
it 'defines the html attributes' do it 'defines the html attributes' do
thing = ThingWithMarkdownFields.new
expect(thing).to respond_to(:title_html, :description_html, :cached_markdown_version) expect(thing).to respond_to(:title_html, :description_html, :cached_markdown_version)
end end
......
...@@ -16,10 +16,14 @@ describe Gitlab::MarkdownCache::Redis::Store, :clean_gitlab_redis_cache do ...@@ -16,10 +16,14 @@ describe Gitlab::MarkdownCache::Redis::Store, :clean_gitlab_redis_cache do
def id def id
'test-redisbacked-store' 'test-redisbacked-store'
end end
def cache_key
"cache-key"
end
end end
end end
let(:storable) { storable_class.new } let(:storable) { storable_class.new }
let(:cache_key) { "markdown_cache:#{storable_class}:#{storable.id}" } let(:cache_key) { "markdown_cache:#{storable.cache_key}" }
subject(:store) { described_class.new(storable) } subject(:store) { described_class.new(storable) }
......
...@@ -30,6 +30,10 @@ describe CacheMarkdownField, :clean_gitlab_redis_cache do ...@@ -30,6 +30,10 @@ describe CacheMarkdownField, :clean_gitlab_redis_cache do
def id def id
"test-markdown-cache" "test-markdown-cache"
end end
def cache_key
"cache-key"
end
end end
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