簡體   English   中英

如何使用 Rails 緩存來存儲 Nokogiri 對象?

[英]How do I use a Rails cache to store Nokogiri objects?

我正在使用 Rails 5 使用 Rails 緩存來存儲 Nokogiri 對象。

我在 config/initializers/cache.rb 中創建了這個:

$cache = ActiveSupport::Cache::MemoryStore.new

我想存儲如下文件:

$cache.fetch(url) {
  result = get_content(url, headers, follow_redirects)
}

但我收到此錯誤:

Error during processing: (TypeError) no _dump_data is defined for class Nokogiri::HTML::Document
/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/cache.rb:671:in `dump'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/cache.rb:671:in `dup_value!'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/cache/memory_store.rb:128:in `write_entry'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/cache.rb:398:in `block in write'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/cache.rb:562:in `block in instrument'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/notifications.rb:166:in `instrument'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/cache.rb:562:in `instrument'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/cache.rb:396:in `write'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/cache.rb:596:in `save_block_result_to_cache'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/cache.rb:300:in `fetch'
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:116:in `get_cached_content'
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:73:in `get_url'
/Users/davea/Documents/workspace/myproject/app/services/abstract_my_object_finder_service.rb:29:in `process_data'
/Users/davea/Documents/workspace/myproject/app/services/run_crawlers_service.rb:26:in `block (2 levels) in run_all_crawlers'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:348:in `run_task'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:337:in `block (3 levels) in create_worker'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `loop'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `block (2 levels) in create_worker'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `catch'
/Users/davea/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `block in create_worker'

我需要做什么才能將這些對象存儲在緩存中?

將 xml 存儲為字符串,而不是對象,並在將它們從緩存中取出后解析它們。

編輯:回復評論

緩存這個

nokogiri_object.to_xml

Edit2:回復評論。 沿着這條線的東西。 如果您需要更具體的幫助,則需要發布更多代碼。

nokogiri_object = Nokogiri::XML(cache.fetch('xml_doc'))

Edit3:對“謝謝,但“在緩存中存儲序列化對象”的代碼是什么? 我認為“$cache.fetch(url) {”的主體會負責存儲然后檢索東西?

cache.write('url', xml_or_serialized_nokogiri_string)

用戶 Nokogiri 的序列化功能:

$cache = ActiveSupport::Cache::MemoryStore.new 
noko_object = Nokogiri::HTML::Document.new

serial = noko_object.serialize
$cache.write(url, serial)
// Serialized Nokogiri document is now in store at the URL key.
result = $cache.read(url)

noko_object = Nokogiri::HTML::Document.new(result)
// noko_object is now the original document again :)

查看此處的文檔以獲取更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM