aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-12-10 16:22:30 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-12-10 17:43:26 -0300
commitb097652ef67142b7273b17dcfeba1710a5b5cadf (patch)
tree1ab4244ce0261c5da4e8c249787450c679c46961 /activerecord
parent660882bda42ffacc602758631c2a2e6ba09c1ee1 (diff)
downloadrails-b097652ef67142b7273b17dcfeba1710a5b5cadf.tar.gz
rails-b097652ef67142b7273b17dcfeba1710a5b5cadf.tar.bz2
rails-b097652ef67142b7273b17dcfeba1710a5b5cadf.zip
Allow users to choose the timestamp format in the cache key
This can be done using the class attribute cache_timestamp_format Closes #8195
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md7
-rw-r--r--activerecord/lib/active_record/integration.rb13
-rw-r--r--activerecord/test/cases/base_test.rb11
-rw-r--r--activerecord/test/models/developer.rb5
4 files changed, 34 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index f12094ab1e..e8ae999b33 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,12 @@
## Rails 3.2.10 (unreleased)
+* Add `ActiveRecord::Base.cache_timestamp_format` class attribute to control
+ the format of the timestamp value in the cache key.
+ This allows users to improve the precision of the cache key.
+ Fixes #8195.
+
+ *Rafael Mendonça França*
+
* Unscope `update_column(s)` query to ignore default scope.
When applying `default_scope` to a class with a where clause, using
diff --git a/activerecord/lib/active_record/integration.rb b/activerecord/lib/active_record/integration.rb
index 23c272ef12..f2ace18d44 100644
--- a/activerecord/lib/active_record/integration.rb
+++ b/activerecord/lib/active_record/integration.rb
@@ -1,5 +1,16 @@
module ActiveRecord
module Integration
+ extend ActiveSupport::Concern
+
+ included do
+ ##
+ # :singleton-method:
+ # Indicates the format used to generate the timestamp format in the cache key.
+ # This is +:number+, by default.
+ class_attribute :cache_timestamp_format, :instance_writer => false
+ self.cache_timestamp_format = :number
+ end
+
# Returns a String, which Action Pack uses for constructing an URL to this
# object. The default implementation returns this record's id as a String,
# or nil if this record's unsaved.
@@ -39,7 +50,7 @@ module ActiveRecord
when new_record?
"#{self.class.model_name.cache_key}/new"
when timestamp = self[:updated_at]
- timestamp = timestamp.utc.to_s(:nsec)
+ timestamp = timestamp.utc.to_s(cache_timestamp_format)
"#{self.class.model_name.cache_key}/#{id}-#{timestamp}"
else
"#{self.class.model_name.cache_key}/#{id}"
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 4e0f8054c7..2497b18e15 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -2153,10 +2153,17 @@ class BasicsTest < ActiveRecord::TestCase
def test_cache_key_format_for_existing_record_with_updated_at
dev = Developer.first
- assert_equal "developers/#{dev.id}-#{dev.updated_at.utc.to_s(:nsec)}", dev.cache_key
+ assert_equal "developers/#{dev.id}-#{dev.updated_at.utc.to_s(:number)}", dev.cache_key
+ end
+
+ def test_cache_key_format_for_existing_record_with_updated_at_1
+ dev = CachedDeveloper.first
+ assert_equal "cached_developers/#{dev.id}-#{dev.updated_at.utc.to_s(:nsec)}", dev.cache_key
end
def test_cache_key_changes_when_child_touched
+ old_timestamp_format = Car.cache_timestamp_format
+ Car.cache_timestamp_format = :nsec
car = Car.create
Bulb.create(car: car)
@@ -2164,6 +2171,8 @@ class BasicsTest < ActiveRecord::TestCase
car.bulb.touch
car.reload
assert_not_equal key, car.cache_key
+ ensure
+ Car.cache_timestamp_format = old_timestamp_format
end
def test_cache_key_format_for_existing_record_with_nil_updated_at
diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb
index a730d86586..f8b758b3c2 100644
--- a/activerecord/test/models/developer.rb
+++ b/activerecord/test/models/developer.rb
@@ -245,3 +245,8 @@ class ThreadsafeDeveloper < ActiveRecord::Base
limit(1)
end
end
+
+class CachedDeveloper < ActiveRecord::Base
+ self.table_name = "developers"
+ self.cache_timestamp_format = :nsec
+end