Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9046: cache_column_names.patch

File cache_column_names.patch, 2.3 kB (added by lifofifo, 2 years ago)

Cache SHOW FIELDS query

  • activerecord/lib/active_record/base.rb

    old new  
    838838 
    839839      # Returns an array of column objects for the table associated with this class. 
    840840      def columns 
    841         unless @columns 
    842           @columns = connection.columns(table_name, "#{name} Columns") 
    843           @columns.each {|column| column.primary = column.name == primary_key} 
     841        unless @@columns[table_name] 
     842          @@columns[table_name] = connection.columns(table_name, "#{name} Columns") 
     843          @@columns[table_name].each {|column| column.primary = column.name == primary_key} 
    844844        end 
    845         @columns 
     845        @@columns[table_name] 
    846846      end 
     847       
     848      @@columns = {} 
    847849 
    848850      # Returns a hash of column objects for the table associated with this class. 
    849851      def columns_hash 
     
    883885      # Resets all the cached information about columns, which will cause them to be reloaded on the next request. 
    884886      def reset_column_information 
    885887        read_methods.each { |name| undef_method(name) } 
    886         @column_names = @columns = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = @inheritance_column = nil 
     888        @column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = @inheritance_column = nil 
     889        @@columns.delete(table_name) 
    887890      end 
     891       
     892      def reset_column_cache #:nodoc: 
     893        @@columns = {} 
     894      end 
    888895 
    889896      def reset_column_information_and_inheritable_attributes_for_all_subclasses#:nodoc: 
    890897        subclasses.each { |klass| klass.reset_inheritable_attributes; klass.reset_column_information } 
  • railties/lib/dispatcher.rb

    old new  
    5959        Class.remove_class(*Reloadable.reloadable_classes) 
    6060      end 
    6161         
    62       ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord) 
     62      if defined?(ActiveRecord) 
     63        ActiveRecord::Base.clear_reloadable_connections!  
     64        ActiveRecord::Base.reset_column_cache 
     65      end 
    6366    end 
    6467     
    6568    # Add a preparation callback. Preparation callbacks are run before every