lib/mize/global_clear.rb in mize-0.1.0 vs lib/mize/global_clear.rb in mize-0.2.0

- old
+ new

@@ -1,42 +1,24 @@ -require 'monitor' +require 'thread' module Mize + MUTEX = Mutex.new + class << self # Clear all memoization caches at once. def cache_clear - each_cache do |id, cache| - cache and cache.clear - cache_tracker.delete id - end + each_cache(&:clear) end private def each_cache - cache_tracker.synchronize do - cache_tracker.each_name do |id| - begin - cache = ObjectSpace._id2ref id - rescue RangeError - end - Mize::CacheProtocol === cache or cache = nil - yield id, cache + MUTEX.synchronize do + for cache in ObjectSpace.each_object(Mize::CacheProtocol) + yield cache end end + self end - - def cache_tracker - @cache_tracker ||= Mize.default_cache.dup - end - - def track_cache(cache_id) - cache_tracker.exist?(cache_id) and return - cache_tracker.synchronize do - cache_tracker.write cache_id, true - end - end end - - cache_tracker end