diff --git a/app/models/post_analyzer.rb b/app/models/post_analyzer.rb index 77b1b906a5e..ce6689e601d 100644 --- a/app/models/post_analyzer.rb +++ b/app/models/post_analyzer.rb @@ -11,18 +11,9 @@ class PostAnalyzer def cook(*args) cooked = PrettyText.cook(*args) - # cook all oneboxes, in the past we used to defer this - # for some reason we decided to do this inline now - # TODO: discuss on http://meta.discourse.org what the correct thing is to do - # keep in mind some oneboxes may take a long time to build result = Oneboxer.apply(cooked) do |url, elem| Oneboxer.invalidate(url) if args.last[:invalidate_oneboxes] - begin - Oneboxer.onebox url - rescue => e - Rails.logger.warn("Failed to cook onebox: #{e.message} #{e.backtrace}") - nil - end + Oneboxer.cached_onebox url end cooked = result.to_html if result.changed? diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index 7c9b3923719..16b8281e600 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -22,13 +22,24 @@ module Oneboxer def self.preview(url, options=nil) options ||= {} Oneboxer.invalidate(url) if options[:invalidate_oneboxes] - Onebox.preview(url, cache: Rails.cache).placeholder_html + onebox_raw(url).placeholder_html end def self.onebox(url, options=nil) options ||= {} Oneboxer.invalidate(url) if options[:invalidate_oneboxes] - Onebox.preview(url, cache: Rails.cache).to_s + onebox_raw(url).to_s + end + + def self.cached_onebox(url) + Rails.cache.read(onebox_cache_key(url)) + .to_s + end + + def self.cached_preview(url) + Rails.cache.read(onebox_cache_key(url)) + .try(:placeholder_html) + .to_s end def self.oneboxer_exists_for_url?(url) @@ -36,7 +47,7 @@ module Oneboxer end def self.invalidate(url) - Rails.cache.delete(url) + Rails.cache.delete(onebox_cache_key(url)) end # Parse URLs out of HTML, returning the document when finished. @@ -82,5 +93,16 @@ module Oneboxer Result.new(doc, changed) end + private + def self.onebox_cache_key(url) + "onebox_#{url}" + end + + def self.onebox_raw(url) + Rails.cache.fetch(onebox_cache_key(url)){ + Onebox.preview(url, cache: {}) + } + end + end