Remove native caching. (#415)

diff --git a/README.md b/README.md
index 2d8810c..4cee7ee 100644
--- a/README.md
+++ b/README.md
@@ -46,30 +46,6 @@ preview = Onebox.preview(url)
 "#{preview}" == preview.to_s #=> true
 `‍``
 
-Onebox has its own caching system but you can also provide (or turn off) your own system:
-
-`‍`` ruby
-require "onebox"
-
-url = "http://www.amazon.com/gp/product/B005T3GRNW/ref=s9_simh_gw_p147_d0_i2"
-preview = Onebox.preview(url, cache: Rails.cache)
-"#{preview}" == preview.to_s #=> true
-`‍``
-
-In addition you can set your own options with this handy interface:
-
-`‍`` ruby
-require "onebox"
-
-Onebox.options = {
-  cache: Rails.cache
-}
-
-url = "http://www.amazon.com/gp/product/B005T3GRNW/ref=s9_simh_gw_p147_d0_i2"
-preview = Onebox.preview(url)
-"#{preview}" == preview.to_s #=> true
-`‍``
-
 Ruby Support
 ------------
 
diff --git a/lib/onebox.rb b/lib/onebox.rb
index a955a07..bf9bc93 100644
--- a/lib/onebox.rb
+++ b/lib/onebox.rb
@@ -6,7 +6,6 @@ require "multi_json"
 require "nokogiri"
 require "mustache"
 require "ostruct"
-require "moneta"
 require "cgi"
 require "net/http"
 require "digest"
@@ -15,7 +14,6 @@ require_relative "onebox/sanitize_config"
 
 module Onebox
   DEFAULTS = {
-    cache: Moneta.new(:Memory, expires: true, serializer: :json),
     connect_timeout: 5,
     timeout: 10,
     max_download_kb: (10 * 1024), # 10MB
@@ -29,6 +27,11 @@ module Onebox
   @@options = DEFAULTS
 
   def self.preview(url, options = Onebox.options)
+    # onebox does not have native caching
+    unless Onebox::Helpers.blank?(options[:cache])
+      warn "Onebox no longer has inbuilt caching so `cache` option will be ignored."
+    end
+
     Preview.new(url, options)
   end
 
diff --git a/lib/onebox/engine.rb b/lib/onebox/engine.rb
index eda5767..87d9cb0 100644
--- a/lib/onebox/engine.rb
+++ b/lib/onebox/engine.rb
@@ -13,7 +13,6 @@ module Onebox
     end
 
     attr_reader :url, :uri
-    attr_reader :cache
     attr_reader :timeout
 
     DEFAULT = {}
@@ -28,10 +27,12 @@ module Onebox
       @options
     end
 
-    def initialize(link, cache = nil, timeout = nil)
+    def initialize(link, timeout = nil)
       @options = DEFAULT
       class_name = self.class.name.split("::").last.to_s
-      self.options = Onebox.options[class_name] || {} #Set the engine options extracted from global options.
+
+      # Set the engine options extracted from global options.
+      self.options = Onebox.options[class_name] || {}
 
       @url = link
       @uri = URI(link)
@@ -39,7 +40,6 @@ module Onebox
         @uri.scheme = 'https'
         @url = @uri.to_s
       end
-      @cache = cache || Onebox.options.cache
       @timeout = timeout || Onebox.options.timeout
     end
 
@@ -63,13 +63,6 @@ module Onebox
 
     private
 
-    def record
-      url_result = url
-      result = cache.fetch(url_result) { data }
-      cache[url_result] = result if cache.respond_to?(:key?)
-      result
-    end
-
     # raises error if not defined in onebox engine
     # in each onebox, uses either Nokogiri or StandardEmbed to get raw HTML from url
     def raw
diff --git a/lib/onebox/engine/google_maps_onebox.rb b/lib/onebox/engine/google_maps_onebox.rb
index cc04e73..f724403 100644
--- a/lib/onebox/engine/google_maps_onebox.rb
+++ b/lib/onebox/engine/google_maps_onebox.rb
@@ -38,7 +38,7 @@ module Onebox
       # Matches URLs for the old Google Maps domain which we occasionally get redirected to
       matches_regexp :canonical,  %r"^(?:https?:)?//maps\.google(?:\.(?:\w{2,}))+/maps\?"
 
-      def initialize(url, cache = nil, timeout = nil)
+      def initialize(url, timeout = nil)
         super
         resolve_url!
       rescue Net::HTTPServerException, Timeout::Error, Net::HTTPError, Errno::ECONNREFUSED, RuntimeError => err
diff --git a/lib/onebox/engine/reddit_image_onebox.rb b/lib/onebox/engine/reddit_image_onebox.rb
index d7f0497..ab19023 100644
--- a/lib/onebox/engine/reddit_image_onebox.rb
+++ b/lib/onebox/engine/reddit_image_onebox.rb
@@ -26,7 +26,7 @@ module Onebox
             </aside>
           HTML
         else
-          html = Onebox::Engine::WhitelistedGenericOnebox.new(@url, @cache, @timeout).to_html
+          html = Onebox::Engine::WhitelistedGenericOnebox.new(@url, @timeout).to_html
           return if Onebox::Helpers.blank?(html)
           html
         end
diff --git a/lib/onebox/engine/youtube_onebox.rb b/lib/onebox/engine/youtube_onebox.rb
index 5131aa7..7d25f39 100644
--- a/lib/onebox/engine/youtube_onebox.rb
+++ b/lib/onebox/engine/youtube_onebox.rb
@@ -45,7 +45,7 @@ module Onebox
           HTML
         else
           # for channel pages
-          html = Onebox::Engine::WhitelistedGenericOnebox.new(@url, @cache, @timeout).to_html
+          html = Onebox::Engine::WhitelistedGenericOnebox.new(@url, @timeout).to_html
           return if Onebox::Helpers.blank?(html)
           html.gsub!(/['"]\/\//, "https://")
           html
diff --git a/lib/onebox/layout.rb b/lib/onebox/layout.rb
index 646d95d..ccc6936 100644
--- a/lib/onebox/layout.rb
+++ b/lib/onebox/layout.rb
@@ -8,12 +8,10 @@ module Onebox
 
     VERSION = "1.0.0"
 
-    attr_reader :cache
     attr_reader :record
     attr_reader :view
 
-    def initialize(name, record, cache)
-      @cache = cache
+    def initialize(name, record)
       @record = Onebox::Helpers.symbolize_keys(record)
 
       # Fix any relative paths
@@ -28,9 +26,7 @@ module Onebox
     end
 
     def to_html
-      result = cache.fetch(checksum) { render(details) }
-      cache[checksum] = result if cache.respond_to?(:key?)
-      result
+      render(details)
     end
 
     private
diff --git a/lib/onebox/layout_support.rb b/lib/onebox/layout_support.rb
index 3548ac1..dcec7cf 100644
--- a/lib/onebox/layout_support.rb
+++ b/lib/onebox/layout_support.rb
@@ -8,7 +8,7 @@ module Onebox
     end
 
     def layout
-      @layout ||= Layout.new(self.class.onebox_name, record, @cache)
+      @layout ||= Layout.new(self.class.onebox_name, data)
     end
 
     def to_html
diff --git a/lib/onebox/mixins/git_blob_onebox.rb b/lib/onebox/mixins/git_blob_onebox.rb
index 381c533..98a385a 100644
--- a/lib/onebox/mixins/git_blob_onebox.rb
+++ b/lib/onebox/mixins/git_blob_onebox.rb
@@ -25,9 +25,9 @@ module Onebox
       }
 
       module InstanceMethods
-        def initialize(link, cache = nil, timeout = nil)
-          super link, cache , timeout
-          #merge engine options from global Onebox.options interface
+        def initialize(link, timeout = nil)
+          super link, timeout
+          # merge engine options from global Onebox.options interface
           # self.options = Onebox.options["GithubBlobOnebox"] #  self.class.name.split("::").last.to_s
           # self.options = Onebox.options[self.class.name.split("::").last.to_s] #We can use this a more generic approach. extract the engine class name automatically
 
diff --git a/lib/onebox/preview.rb b/lib/onebox/preview.rb
index b96e0ab..b94bf1f 100644
--- a/lib/onebox/preview.rb
+++ b/lib/onebox/preview.rb
@@ -2,7 +2,6 @@
 
 module Onebox
   class Preview
-    attr_reader :cache
 
     # see https://bugs.ruby-lang.org/issues/14688
     client_exception = defined?(Net::HTTPClientException) ? Net::HTTPClientException : Net::HTTPServerException
@@ -11,7 +10,6 @@ module Onebox
     def initialize(link, parameters = Onebox.options)
       @url = link
       @options = parameters
-      @cache = options.cache
       @engine_class = Matcher.new(@url).oneboxed
     end
 
@@ -72,7 +70,7 @@ module Onebox
       return nil unless @engine_class
       return @engine if @engine
 
-      @engine = @engine_class.new(@url, cache)
+      @engine = @engine_class.new(@url)
       @engine.options = @options
       @engine
     end
diff --git a/onebox.gemspec b/onebox.gemspec
index dc5ddc4..172d5c0 100644
--- a/onebox.gemspec
+++ b/onebox.gemspec
@@ -23,7 +23,6 @@ Gem::Specification.new do |spec|
   spec.add_runtime_dependency 'multi_json', '~> 1.11'
   spec.add_runtime_dependency 'mustache'

[... diff too long, it was truncated ...]

GitHub sha: 8c61a33a

2 Likes

This commit has been mentioned on Discourse Meta. There might be relevant details there:

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

1 Like