2013-04-23 22:00:40 -04:00
|
|
|
# review per rails release, this speeds up the inflector, we are not inflecting too much at the moment, except in dev
|
|
|
|
#
|
|
|
|
# note: I am working with the rails team on including this in official rails
|
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
module Inflector
|
|
|
|
|
|
|
|
LRU_CACHE_SIZE = 200
|
|
|
|
LRU_CACHES = []
|
|
|
|
|
|
|
|
def self.clear_memoize!
|
|
|
|
LRU_CACHES.each(&:clear)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.memoize(*args)
|
|
|
|
args.each do |method_name|
|
|
|
|
cache = LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
|
|
|
|
LRU_CACHES << cache
|
|
|
|
|
|
|
|
uncached = "#{method_name}_without_cache"
|
|
|
|
alias_method uncached, method_name
|
|
|
|
|
2018-09-03 22:16:21 -04:00
|
|
|
define_method(method_name) do |*arguments|
|
2013-04-23 22:00:40 -04:00
|
|
|
# this avoids recursive locks
|
|
|
|
found = true
|
2018-09-03 22:16:21 -04:00
|
|
|
data = cache.fetch(arguments) { found = false }
|
2013-04-23 22:00:40 -04:00
|
|
|
unless found
|
2018-09-03 22:16:21 -04:00
|
|
|
cache[arguments] = data = send(uncached, *arguments)
|
2013-04-23 22:00:40 -04:00
|
|
|
end
|
2013-04-23 22:26:39 -04:00
|
|
|
# so cache is never corrupted
|
|
|
|
data.dup
|
2013-04-23 22:00:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
memoize :pluralize, :singularize, :camelize, :underscore, :humanize,
|
|
|
|
:titleize, :tableize, :classify, :foreign_key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
module Inflector
|
|
|
|
class Inflections
|
|
|
|
def self.clear_memoize(*args)
|
|
|
|
args.each do |method_name|
|
|
|
|
orig = "#{method_name}_without_clear_memoize"
|
|
|
|
alias_method orig, method_name
|
2018-09-03 22:16:21 -04:00
|
|
|
|
|
|
|
define_method(method_name) do |*arguments|
|
2013-04-23 22:00:40 -04:00
|
|
|
ActiveSupport::Inflector.clear_memoize!
|
2018-09-03 22:16:21 -04:00
|
|
|
send(orig, *arguments)
|
2013-04-23 22:00:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
clear_memoize :acronym, :plural, :singular, :irregular, :uncountable, :human, :clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|