2

我的 webapp on rails 将一些模型对象的参数存储在 XML 字符串中,所以每当我需要有关特定对象的一些信息时,我都必须解析它的 XML 字符串。XML 的长度很少超过 100 行。但由于我希望优化,我想知道我是否可以将解析的 XML 作为 Nokogiri 的对象存储在 db 中。这是个好主意吗?

4

1 回答 1

2

Though there are probably exceptions, in general, you should avoid storing marshalled objects directly in your database unless you have a very good reason. In the case of Nokogiri, as @mu-is-too-short mentioned, Nokogiri and Marshal don't play well together:

doc = Nokogiri::HTML(some_html)    
Marshal.dump doc
# => TypeError: no _dump_data is defined for class Nokogiri::HTML::Document

That said, Marshal#load and Marshal#dump are part of the core Ruby library and are quite fun to play with. Along with the docs, here is a quick code example showing how Marshal works, including a very basic benchmark comparing Marshal.load to Class.new:

require 'benchmark'

data_string = <<-DATA
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
DATA

class Example
  attr_reader :data

  def initialize(data)
    @data = data
  end
end

example = Example.new(data_string)

dumped = Marshal.dump example
loaded = Marshal.load dumped

puts "String Bytesize: #{data_string.bytesize} vs. Dump Bytesize: #{dumped.bytesize}"
puts "Marshalled object is larger by #{dumped.bytesize - data_string.bytesize} bytes"

Benchmark.bmbm do |x|
  x.report("Marshal.load: ")  { Marshal.load(dumped).data }
  x.report(" Example.new: ")  { Example.new(data_string).data }
end
于 2013-07-02T12:10:16.000 回答