david
(David Taylor)
March 23, 2017, 12:53pm
4
Yep, Discourse also caches any onebox requests for 24 hours, to prevent repeated requests to third party sites like amazon.
def self.blank_onebox
{ preview: "", onebox: "" }
end
def self.onebox_cache_key(url)
"onebox__#{url}"
end
def self.onebox_raw(url)
Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day) do
uri = URI(url) rescue nil
return blank_onebox if uri.blank? || SiteSetting.onebox_domains_blacklist.include?(uri.hostname)
options = { cache: {}, max_width: 695, sanitize_config: Sanitize::Config::DISCOURSE_ONEBOX }
r = Onebox.preview(url, options)
{ onebox: r.to_s, preview: r.try(:placeholder_html).to_s }
end
rescue => e
# no point warning here, just cause we have an issue oneboxing a url
# we can later hunt for failed oneboxes by searching logs if needed
Rails.logger.info("Failed to onebox #{url} #{e} #{e.backtrace}")
If you’re in a development environment and want to purge the cache, there are some suggestions in this thread. Do not purge the redis cache on a production site - it will mess with lots of things:
I’m developing a small Onebox plugin and testing it in my production install.
It appears that Discourse is caching the Oneboxes: When I paste a link I once saw oneboxed into a new post, the preview is still looking as if it was generated by an older version of the plugin, while newer links appear as they should. All this is happening in the preview, I’m not actually posting the link.
How can I clear this cache?
7 Likes