Permalink
Browse files

Improved performance of flush_cache and prime_cache when parameters a…

  • Loading branch information...
1 parent b5bbbd0 commit 5fc598a35990c8aea04b626671fa623bb5129165 @jrafanie jrafanie committed Aug 15, 2012
Showing with 22 additions and 17 deletions.
  1. +22 −17 lib/memoist.rb
View
@@ -23,36 +23,41 @@ def freeze_with_memoizable
end
def memoize_all
- prime_cache ".*"
+ prime_cache
end
def unmemoize_all
- flush_cache ".*"
+ flush_cache
end
def prime_cache(*syms)
+ if syms.empty?
+ syms = methods.collect do |m|
+ m[12..-1] if m.to_s.start_with?("_unmemoized_")
+ end.compact
+ end
+
syms.each do |sym|
- methods.each do |m|
- if m.to_s =~ /^_unmemoized_(#{sym})/
- if method(m).arity == 0
- __send__($1)
- else
- ivar = Memoist.memoized_ivar_for($1)
- instance_variable_set(ivar, {})
- end
- end
+ m = method(:"_unmemoized_#{sym}") rescue next
+ if m.arity == 0
+ __send__(sym)
+ else
+ ivar = Memoist.memoized_ivar_for(sym)
+ instance_variable_set(ivar, {})
end
end
end
def flush_cache(*syms)
+ if syms.empty?
+ syms = (methods + private_methods + protected_methods).collect do |m|
+ m[12..-1] if m.to_s.start_with?("_unmemoized_")
+ end.compact
+ end
+
syms.each do |sym|
- (methods + private_methods + protected_methods).each do |m|
- if m.to_s =~ /^_unmemoized_(#{sym.to_s.gsub(/\?\Z/, '\?')})/
- ivar = Memoist.memoized_ivar_for($1)
- instance_variable_get(ivar).clear if instance_variable_defined?(ivar)
- end
- end
+ ivar = Memoist.memoized_ivar_for(sym)
+ instance_variable_get(ivar).clear if instance_variable_defined?(ivar)
end
end
end

0 comments on commit 5fc598a

Please sign in to comment.