提交 8b64aba1 编写于 作者: S Sean Griffin

Merge pull request #21883 from tarzan/cache-key-too-precise

Fix precision on cache_key
......@@ -10,9 +10,9 @@ module Integration
# Indicates the format used to generate the timestamp in the cache key.
# Accepts any of the symbols in <tt>Time::DATE_FORMATS</tt>.
#
# This is +:nsec+, by default.
# This is +:usec+, by default.
class_attribute :cache_timestamp_format, :instance_writer => false
self.cache_timestamp_format = :nsec
self.cache_timestamp_format = :usec
end
# Returns a String, which Action Pack uses for constructing a URL to this
......
require "cases/helper"
module ActiveRecord
class CacheKeyTest < ActiveRecord::TestCase
self.use_transactional_tests = false
class CacheMe < ActiveRecord::Base; end
setup do
@connection = ActiveRecord::Base.connection
@connection.create_table(:cache_mes) { |t| t.timestamps }
end
teardown do
@connection.drop_table :cache_mes, if_exists: true
end
test "test_cache_key_format_is_not_too_precise" do
record = CacheMe.create
key = record.cache_key
assert_equal key, record.reload.cache_key
end
end
end
......@@ -53,7 +53,7 @@ class CollectionCacheKeyTest < ActiveRecord::TestCase
test "cache_key with custom timestamp column" do
topics = Topic.where("title like ?", "%Topic%")
last_topic_timestamp = topics(:fifth).written_on.utc.to_s(:nsec)
last_topic_timestamp = topics(:fifth).written_on.utc.to_s(:usec)
assert_match(last_topic_timestamp, topics.cache_key(:written_on))
end
......
......@@ -81,7 +81,7 @@ def test_cache_key_for_existing_record_is_not_timezone_dependent
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(:usec)}", dev.cache_key
end
def test_cache_key_format_for_existing_record_with_updated_at_and_custom_cache_timestamp_format
......@@ -111,19 +111,19 @@ def test_cache_key_format_for_existing_record_with_nil_updated_timestamps
def test_cache_key_for_updated_on
dev = Developer.first
dev.updated_at = nil
assert_equal "developers/#{dev.id}-#{dev.updated_on.utc.to_s(:nsec)}", dev.cache_key
assert_equal "developers/#{dev.id}-#{dev.updated_on.utc.to_s(:usec)}", dev.cache_key
end
def test_cache_key_for_newer_updated_at
dev = Developer.first
dev.updated_at += 3600
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(:usec)}", dev.cache_key
end
def test_cache_key_for_newer_updated_on
dev = Developer.first
dev.updated_on += 3600
assert_equal "developers/#{dev.id}-#{dev.updated_on.utc.to_s(:nsec)}", dev.cache_key
assert_equal "developers/#{dev.id}-#{dev.updated_on.utc.to_s(:usec)}", dev.cache_key
end
def test_cache_key_format_is_precise_enough
......@@ -134,8 +134,16 @@ def test_cache_key_format_is_precise_enough
assert_not_equal key, dev.cache_key
end
def test_cache_key_format_is_not_too_precise
skip("Subsecond precision is not supported") unless subsecond_precision_supported?
dev = Developer.first
dev.touch
key = dev.cache_key
assert_equal key, dev.reload.cache_key
end
def test_named_timestamps_for_cache_key
owner = owners(:blackbeard)
assert_equal "owners/#{owner.id}-#{owner.happy_at.utc.to_s(:nsec)}", owner.cache_key(:updated_at, :happy_at)
assert_equal "owners/#{owner.id}-#{owner.happy_at.utc.to_s(:usec)}", owner.cache_key(:updated_at, :happy_at)
end
end
......@@ -6,6 +6,7 @@ class Time
:db => '%Y-%m-%d %H:%M:%S',
:number => '%Y%m%d%H%M%S',
:nsec => '%Y%m%d%H%M%S%9N',
:usec => '%Y%m%d%H%M%S%6N',
:time => '%H:%M',
:short => '%d %b %H:%M',
:long => '%B %d, %Y %H:%M',
......
......@@ -534,6 +534,7 @@ def test_to_s
assert_equal "17:44", time.to_s(:time)
assert_equal "20050221174430", time.to_s(:number)
assert_equal "20050221174430123456789", time.to_s(:nsec)
assert_equal "20050221174430123456", time.to_s(:usec)
assert_equal "February 21, 2005 17:44", time.to_s(:long)
assert_equal "February 21st, 2005 17:44", time.to_s(:long_ordinal)
with_env_tz "UTC" do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册