Module: ThreeD::ClassMethodCache::ClassMethods
- Defined in:
- lib/three_d/class_method_cache.rb
Instance Method Summary collapse
- #activate_method_cache(methods_list = {}) ⇒ Object
- #add_cache_methods(methods_list = {}) ⇒ Object
- #method_cache_enabled? ⇒ Boolean
- #setup_cache_method_aliasing ⇒ Object
Instance Method Details
#activate_method_cache(methods_list = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/three_d/class_method_cache.rb', line 12 def activate_method_cache(methods_list = {}) if !self.method_cache_enabled? cattr_accessor :cached_methods cattr_accessor :cache_storage # Setting up Cache Storage Variable cache_storage_name = "$#{self.name.underscore.upcase}_METHOD_CACHE" eval("#{cache_storage_name} = {}") self.cache_storage = eval(cache_storage_name) self.add_cache_methods(methods_list) # Instanzmethoden für den User laden self.send :include, InstanceMethods end end |
#add_cache_methods(methods_list = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/three_d/class_method_cache.rb', line 35 def add_cache_methods(methods_list = {}) # Check cached methods list if methods_list.is_a?(Array) methods_list = {:default => methods_list} end if methods_list.is_a?(Hash) self.cached_methods ||= {} methods_list.each do |group, methods| if self.cached_methods[group.to_sym].nil? self.cached_methods[group.to_sym] = methods else self.cached_methods[group.to_sym] << methods self.cached_methods[group.to_sym] = self.cached_methods[group.to_sym].flatten end end else raise ArgumentError, "Wrong definition for cached methods: Use either an array (self.cached_methods will become {:default => array}) or a hash" end setup_cache_method_aliasing end |
#method_cache_enabled? ⇒ Boolean
133 134 135 |
# File 'lib/three_d/class_method_cache.rb', line 133 def method_cache_enabled? self.included_modules.include?(InstanceMethods) end |
#setup_cache_method_aliasing ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/three_d/class_method_cache.rb', line 60 def setup_cache_method_aliasing # Setup des MethodAliasing self.instance_eval do self.cached_methods.each do |method_stack, methods| methods.each_with_index do |method, i| puts [self.name, method, self.instance_methods.include?(method.to_sym)].inspect # Methodennamen mit ?/! am ende fixen - Alias anlegen needs_alias = false if method.to_s.last == "?" fixed_name = method.to_s.gsub("?","_with_soi") needs_alias = true elsif method.to_s.last == "!" fixed_name = method.to_s.gsub("?","_with_bang") needs_alias = true end if needs_alias alias_method fixed_name, method method = fixed_name end define_method("#{method}_with_caching") do |*args| #begin start = Time.now # Key für den Cache erzeugen - Alle Parameter mit ablegen args_list = *args.flatten.map {|a| a.is_a?(ActiveRecord::Base) ? "#{a.class.name}-#{a.id}" : a} meth_name = [method,args_list].inspect.parameterize.to_s if cached?(meth_name) x = self.cached_attribute(meth_name) # => ActiveRecordArray disabled (03.08.2013, 18:54, Florian Eck) # if x.is_a?(ActiveRecordArray) # x = x.parse_record_data # end CachingLog.info " ... CALL cache_data #{meth_name} (#{Time.now-start})" return x else # Hier wird die Originalfunction aufgerufen orig = self.send("#{method}_without_caching", *args) # => ActiveRecordArray disabled #if orig.is_active_record_array? #parse = orig.parse_active_record_array #CachingLog.info "save ActiveRecordArray" #else parse = orig #end v = cache_attribute!(meth_name, parse) CachingLog.info " ... CREATE cache_data #{meth_name} (#{Time.now-start})" return orig end #rescue Exception => e # self.method_cache = {:last_error => e.backtrace, :last_error_class => e.class.inspect, :last_error_message => e.message} # return nil #self.send("#{method}_without_caching", *args) #end #__send__("#{method}_without_caching", *args) end alias_method_chain method, :caching end end end end |