Permalink
Browse files

Allow users to choose the timestamp format in the cache key

This can be done using the class attribute cache_timestamp_format

Conflicts:
	railties/guides/source/configuring.textile
  • Loading branch information...
1 parent 3142bf5 commit 9cce1ea2fd02e8050350c4657e7cc7f1902b8694 @rafaelfranca rafaelfranca committed Dec 10, 2012
@@ -1,5 +1,12 @@
## Rails 4.0.0 (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*
+
* Fix decorating columns for serialized attributes. Fixes #8441
*itzki*
@@ -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 = :nsec
+ 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.
@@ -37,7 +48,7 @@ def cache_key
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}"
@@ -1444,6 +1444,11 @@ def test_cache_key_format_for_existing_record_with_updated_at
assert_equal "developers/#{dev.id}-#{dev.updated_at.utc.to_s(:nsec)}", dev.cache_key
end
+ def test_cache_key_format_for_existing_record_with_updated_at_and_cache_timestamp_format_set
+ dev = CachedDeveloper.first
+ assert_equal "cached_developers/#{dev.id}-#{dev.updated_at.utc.to_s(:number)}", dev.cache_key
+ end
+
def test_cache_key_changes_when_child_touched
car = Car.create
Bulb.create(car: car)
@@ -234,3 +234,8 @@ def self.default_scope
limit(1)
end
end
+
+class CachedDeveloper < ActiveRecord::Base
+ self.table_name = "developers"
+ self.cache_timestamp_format = :number
+end
@@ -276,6 +276,8 @@ config.middleware.delete ActionDispatch::BestStandardsSupport
* `config.active_record.auto_explain_threshold_in_seconds` configures the threshold for automatic EXPLAINs (`nil` disables this feature). Queries exceeding the threshold get their query plan logged. Default is 0.5 in development mode.
+* +config.active_record.cache_timestamp_format+ controls the format of the timestamp value in the cache key. Default is +:number+.
+
The MySQL adapter adds one additional configuration option:
* `ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans` controls whether Active Record will consider all `tinyint(1)` columns in a MySQL database to be booleans and is true by default.

0 comments on commit 9cce1ea

Please sign in to comment.