I'm using Rails 5 to use a Rails cache to store Nokogiri objects.
I created this in config/initializers/cache.rb:
$cache = ActiveSupport::Cache::MemoryStore.new
and I wanted to store documents like:
$cache.fetch(url) { result = get_content(url, headers, follow_redirects) }
but I'm getting this error:
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'
What do I need to do in order to be able to store these objects in a cache?
2 Answers
Answers 1
Store the xml as string, not the object and parse them once you get them out of the cache.
Edit: response to comment
Cache this instead
nokogiri_object.to_xml
Edit2: response to comment. Something along this lines. You will need to post more code if you want more specific help.
nokogiri_object = Nokogiri::XML(cache.fetch('xml_doc'))
Edit3: Response to 'Thanks but what is the code for "Store serialized object in cache"? I thought the body of the "$cache.fetch(url) {" would take care of storing and then retrieiving things?'
cache.write('url', xml_or_serialized_nokogiri_string)
Answers 2
User Nokogiri's Serialize functionality:
$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 :)
0 comments:
Post a Comment