Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use less queries for storing taggings

  • Loading branch information...
commit 30cb0d212836aef2e5b942b6de0ebe7573a2b6c0 1 parent 9e6a9e7
@svenfuchs authored
View
7 lib/rack/cache/tags/store/active_record.rb
@@ -10,7 +10,8 @@ class Tagging < ::ActiveRecord::Base
end
def store(url, tags)
- tags.each { |tag| Tagging.find_or_create_by_url_and_tag(url, tag) }
+ tags -= taggings_by_url_and_tags(url, tags).map(&:tag)
+ tags.each { |tag| Tagging.create(:url => url, :tag => tag) }
end
def purge(tags)
@@ -25,6 +26,10 @@ def taggings_by_tags(tags)
sql = "tag IN (?) #{[' OR tag LIKE ?'] * tags.size}"
Tagging.where(sql, tags, *tags.map { |tag| "#{tag.split(':').first}%" })
end
+
+ def taggings_by_url_and_tags(url, tags)
+ Tagging.where("url = ? AND tag IN (?)", url, tags)
+ end
end
end
end
View
22 test/tags_test.rb
@@ -9,12 +9,22 @@ def setup
@store = Rack::Cache::Tags::Store::ActiveRecord.new
end
+ def create_taggings(taggings)
+ taggings.each { |url, tags| tags.each { |tag| create_tagging(url, tag) } }
+ end
+
+ def create_tagging(url, tag)
+ ActiveRecord::Tagging.create!(:url => url, :tag => tag)
+ end
+
test 'stores tags for the current url' do
+ create_taggings('http://example.com/' => %w(bar-2), 'http://example.com/bar' => %w(bar-2))
respond_with 200, { Rack::Cache::Tags::TAGS_HEADER => %w(foo-1 bar-2) }, ''
-
get('http://example.com/')
- actual = ActiveRecord::Tagging.all.map { |tagging| [tagging.url, tagging.tag] }
- assert_equal [%W(http://example.com/ foo-1), %W(http://example.com/ bar-2)], actual
+
+ actual = ActiveRecord::Tagging.all.map { |tagging| [tagging.url, tagging.tag] }
+ expected = [%W(http://example.com/ foo-1), %W(http://example.com/ bar-2), %W(http://example.com/bar bar-2)]
+ assert_equal expected.sort, actual.sort
end
test 'expands tags to urls for purge headers and deletes the tagging' do
@@ -41,5 +51,11 @@ def setup
create_tags('http://example.com/foo', tags)
assert_equal tags, store.taggings_by_tags(%w(foo-1)).map(&:tag)
end
+
+ test 'taggings_by_url_and_tags' do
+ create_taggings('/foo' => %w(tag-1 tag-2), '/bar' => %w(tag-1))
+ taggings = store.taggings_by_url_and_tags('/foo', %w(tag-1 tag-3))
+ assert_equal [['/foo', 'tag-1']], taggings.map { |tagging| [tagging.url, tagging.tag] }
+ end
end
View
3  test/test_helper.rb
@@ -15,7 +15,6 @@
FileUtils.touch(log) unless File.exists?(log)
ActiveRecord::Base.logger = Logger.new(log)
-ActiveRecord::LogSubscriber.attach_to(:active_record)
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
ActiveRecord::Migration.verbose = false
@@ -56,4 +55,4 @@ def get(url)
def env_for(*args)
Rack::MockRequest.env_for(*args)
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.