Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #11796 from thedarkone/time-zone-thread-safety

AS::TimeZone's cache thread safety
  • Loading branch information...
commit e8523be4ac9074dc05dbd3f4dd5ac705bfd3b9a6 2 parents d30cf96 + 2a8c258
@guilleiguaran guilleiguaran authored
Showing with 7 additions and 13 deletions.
  1. +7 −13 activesupport/lib/active_support/values/time_zone.rb
View
20 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 @@ class TimeZone
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 @@ def all
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 @@ def [](arg)
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 @@ def require_tzinfo
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
Please sign in to comment.
Something went wrong with that request. Please try again.