aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorGuillermo Iguaran <guilleiguaran@gmail.com>2013-11-30 23:31:55 -0800
committerGuillermo Iguaran <guilleiguaran@gmail.com>2013-11-30 23:31:55 -0800
commite8523be4ac9074dc05dbd3f4dd5ac705bfd3b9a6 (patch)
treea2d56f8b4ea974475eabab7efa8f143bd530aa39 /activesupport
parentd30cf963ebd4c0d74353dcbd9b8061f58b1d32cd (diff)
parent2a8c2582c9023f4ac02fe9f09f61c4f9c88e4ebd (diff)
downloadrails-e8523be4ac9074dc05dbd3f4dd5ac705bfd3b9a6.tar.gz
rails-e8523be4ac9074dc05dbd3f4dd5ac705bfd3b9a6.tar.bz2
rails-e8523be4ac9074dc05dbd3f4dd5ac705bfd3b9a6.zip
Merge pull request #11796 from thedarkone/time-zone-thread-safety
AS::TimeZone's cache thread safety
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb20
1 files changed, 7 insertions, 13 deletions
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index 3cf82a24b9..b6d9257f00 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -1,3 +1,4 @@
+require 'thread_safe'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/object/try'
@@ -184,6 +185,8 @@ module ActiveSupport
UTC_OFFSET_WITH_COLON = '%s%02d:%02d'
UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.sub(':', '')
+ @lazy_zones_map = ThreadSafe::Cache.new
+
# Assumes self represents an offset from UTC in seconds (as returned from
# Time#utc_offset) and turns this into an +HH:MM formatted string.
#
@@ -362,10 +365,8 @@ module ActiveSupport
def zones_map
@zones_map ||= begin
- new_zones_names = MAPPING.keys - lazy_zones_map.keys
- new_zones = Hash[new_zones_names.map { |place| [place, create(place)] }]
-
- lazy_zones_map.merge(new_zones)
+ MAPPING.each_key {|place| self[place]} # load all the zones
+ @lazy_zones_map
end
end
@@ -378,7 +379,7 @@ module ActiveSupport
case arg
when String
begin
- lazy_zones_map[arg] ||= lookup(arg).tap { |tz| tz.utc_offset }
+ lazy_zones_map[arg] ||= create(arg).tap { |tz| tz.utc_offset }
rescue TZInfo::InvalidTimezoneIdentifier
nil
end
@@ -407,16 +408,9 @@ module ActiveSupport
private
- def lookup(name)
- (tzinfo = find_tzinfo(name)) && create(tzinfo.name.freeze)
- end
-
def lazy_zones_map
require_tzinfo
-
- @lazy_zones_map ||= Hash.new do |hash, place|
- hash[place] = create(place) if MAPPING.has_key?(place)
- end
+ @lazy_zones_map
end
end