레일 캐쉬를 사용하여 Nokogiri 개체를 저장하려면 레일즈 5를 사용하고 있습니다. 레일 캐쉬를 사용하여 Nokogiri 개체를 저장하려면 어떻게해야합니까?
제가 설정/초기화/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'
을
캐시에 이러한 개체를 저장하려면 무엇을해야합니까?
분명히 그렇지 않습니다. 캐시는 문자열 저장에 좋습니다. –
왜 개체를 저장 하시겠습니까? Nokogiri를 사용하여 HTML 또는 XML에서 긁어 낸 정보가 포함 된 직렬화 된 해시 또는 배열을 저장합니다. 개체를 저장해야하는 경우 메모 작성을 살펴보십시오. –
이것에 대해 더 생각해 보면, 메모리 캐시는 즉시 액세스해야하는 경우에 유용하지만 컴퓨터가 다운 된 경우 신속하게 다시 만들 수 있습니다. Nokogiri를 사용하고 있다면, 페이지를 긁어 내고있는 것을 알 수있는 확률이 높습니다. 페이지를로드하는 중임을 의미합니다.로드, 구문 분석, 긁기 프로세스로 원하지 않는 대기 시간이 추가됩니다 (그러므로 캐시를 사용하는 아이디어) 대신 (메타) 데이터를 수집하여 영구적으로 사용할 수있는 데이터베이스에 저장해야합니다. DBM은 내부적으로 캐시됩니다. 메모리 내 캐시만큼 빠르지는 않지만 요청시 또는 앱 시작시 재 작성하는 것보다 낫습니다. –