1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Rafael Mendonça França 2012-12-10 16:22:30 -03:00
parent 3142bf51ee
commit 9cce1ea2fd
5 changed files with 31 additions and 1 deletions

View file

@ -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*

View file

@ -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 @@ 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}"

View file

@ -1444,6 +1444,11 @@ class BasicsTest < ActiveRecord::TestCase
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)

View file

@ -234,3 +234,8 @@ class ThreadsafeDeveloper < ActiveRecord::Base
limit(1)
end
end
class CachedDeveloper < ActiveRecord::Base
self.table_name = "developers"
self.cache_timestamp_format = :number
end

View file

@ -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.