jeudi 29 juin 2017

Slower while generating the XML from the bunch of model object

class GenericFormatter < Formatter
 attr_accessor :tag_name,:objects

 def generate_xml
   builder = Nokogiri::XML::Builder.new do |xml|
   xml.send(tag_name.pluralize) {
   objects.each do |obj|
        xml.send(tag_name.singularize){

            self.generate_obj_row obj,xml
        }                
    end
    }
   end
   builder.to_xml
 end


def initialize tag_name,objects
  self.tag_name = tag_name
  self.objects = objects
end


def generate_obj_row obj,xml
   obj.attributes.except("updated_at").map do |key,value|
     xml.send(key, value)
   end
   xml.updated_at obj.updated_at.try(:strftime,"%m/%d/%Y %H:%M:%S") if obj.attributes.key?('updated_at')
end
 end 

In the above code i have implemented the formatter where I have used the nokogiri XML Builder to generate the XML by manipulating the objects passing out inside the code.It's generated the faster XML when the data is not too large if data is largers like more than 10,000 records then It's slow down the XML to generate and takes atleast 50-60 seconds.

Problem: Is there any way to generate the XML faster,I have tried XML Builders on view as well but did n't work.How i can generate the XML Faster ? Solution should be application on rails 3 and suggestions to optimized above code?

Aucun commentaire:

Enregistrer un commentaire