A platform for community discussion. Free, open, simple.
DEV: use Discourse.cache over Rails.cache
Sam Saffron0fb497eb237c
diff --git a/app/models/theme.rb b/app/models/theme.rbindex aae30f66128..0597cda86fe 100644--- a/app/models/theme.rb+++ b/app/models/theme.rb@@ -418,7 +418,7 @@ class Theme < ActiveRecord::Baseenddef cached_settings- Rails.cache.fetch("settings_for_theme_#{self.id}", expires_in: 30.minutes) do+ Discourse.cache.fetch("settings_for_theme_#{self.id}", expires_in: 30.minutes) dohash = {}self.settings.each do |setting|hash[setting.name] = setting.value@@ -438,7 +438,7 @@ class Theme < ActiveRecord::Baseenddef clear_cached_settings!- Rails.cache.delete("settings_for_theme_#{self.id}")+ Discourse.cache.delete("settings_for_theme_#{self.id}")enddef included_settingsdiff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rbindex a18e34c3ad2..12ff86a646f 100644--- a/app/models/topic_embed.rb+++ b/app/models/topic_embed.rb@@ -228,7 +228,7 @@ class TopicEmbed < ActiveRecord::Baseenddef self.expanded_for(post)- Rails.cache.fetch("embed-topic:#{post.topic_id}", expires_in: 10.minutes) do+ Discourse.cache.fetch("embed-topic:#{post.topic_id}", expires_in: 10.minutes) dourl = TopicEmbed.where(topic_id: post.topic_id).pluck_first(:embed_url)response = TopicEmbed.find_remote(url)diff --git a/lib/discourse.rb b/lib/discourse.rbindex c61a2f14e7d..07b7369a6b8 100644--- a/lib/discourse.rb+++ b/lib/discourse.rb@@ -591,6 +591,7 @@ module DiscourseSiteSetting.after_fork$redis._client.reconnectRails.cache.reconnect+ Discourse.cache.reconnectLogster.store.redis.reconnect# shuts down all connections in the poolSidekiq.redis_pool.shutdown { |c| nil }diff --git a/lib/inline_oneboxer.rb b/lib/inline_oneboxer.rbindex 1405edc234e..b76ebe848cf 100644--- a/lib/inline_oneboxer.rb+++ b/lib/inline_oneboxer.rb@@ -14,11 +14,11 @@ class InlineOneboxerenddef self.purge(url)- Rails.cache.delete(cache_key(url))+ Discourse.cache.delete(cache_key(url))enddef self.cache_lookup(url)- Rails.cache.read(cache_key(url))+ Discourse.cache.read(cache_key(url))enddef self.lookup(url, opts = nil)@@ -70,7 +70,7 @@ class InlineOneboxertitle: title && Emoji.gsub_emoji_to_unicode(title)}unless opts[:skip_cache]- Rails.cache.write(cache_key(url), onebox, expires_in: 1.day)+ Discourse.cache.write(cache_key(url), onebox, expires_in: 1.day)endoneboxdiff --git a/lib/oneboxer.rb b/lib/oneboxer.rbindex e726f716852..fe054f5b542 100644--- a/lib/oneboxer.rb+++ b/lib/oneboxer.rb@@ -51,7 +51,7 @@ module Oneboxerenddef self.cached_onebox(url)- if c = Rails.cache.read(onebox_cache_key(url))+ if c = Discourse.cache.read(onebox_cache_key(url))c[:onebox]endrescue => e@@ -61,7 +61,7 @@ module Oneboxerenddef self.cached_preview(url)- if c = Rails.cache.read(onebox_cache_key(url))+ if c = Discourse.cache.read(onebox_cache_key(url))c[:preview]endrescue => e@@ -71,7 +71,7 @@ module Oneboxerenddef self.invalidate(url)- Rails.cache.delete(onebox_cache_key(url))+ Discourse.cache.delete(onebox_cache_key(url))end# Parse URLs out of HTML, returning the document when finished.@@ -281,7 +281,7 @@ module Oneboxerenddef self.external_onebox(url)- Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day) do+ Discourse.cache.fetch(onebox_cache_key(url), expires_in: 1.day) dofd = FinalDestination.new(url,ignore_redirects: ignore_redirects,ignore_hostnames: blacklisted_domains,diff --git a/lib/search.rb b/lib/search.rbindex 4e163cb23ce..56503df5d45 100644--- a/lib/search.rb+++ b/lib/search.rb@@ -137,7 +137,7 @@ class Searchreturn 0 unless SiteSetting.search_prefer_recent_posts?# It can be quite slow to count all the posts so let's cache it- Rails.cache.fetch("search-min-post-id:#{SiteSetting.search_recent_posts_size}", expires_in: 1.week) do+ Discourse.cache.fetch("search-min-post-id:#{SiteSetting.search_recent_posts_size}", expires_in: 1.week) domin_post_id_no_cacheendenddiff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rbindex c19eb8814e7..2c651d0b612 100644--- a/lib/site_setting_extension.rb+++ b/lib/site_setting_extension.rb@@ -192,7 +192,7 @@ module SiteSettingExtensionenddef client_settings_json- Rails.cache.fetch(SiteSettingExtension.client_settings_cache_key, expires_in: 30.minutes) do+ Discourse.cache.fetch(SiteSettingExtension.client_settings_cache_key, expires_in: 30.minutes) doclient_settings_json_uncachedendend@@ -432,7 +432,7 @@ module SiteSettingExtensionprotecteddef clear_cache!- Rails.cache.delete(SiteSettingExtension.client_settings_cache_key)+ Discourse.cache.delete(SiteSettingExtension.client_settings_cache_key)Site.clear_anon_cache!enddiff --git a/script/benchmarks/cache/bench.rb b/script/benchmarks/cache/bench.rbnew file mode 100644index 00000000000..8f16f4bfcf6--- /dev/null+++ b/script/benchmarks/cache/bench.rb@@ -0,0 +1,81 @@+# frozen_string_literal: true++require 'benchmark/ips'+require File.expand_path('../../../../config/environment', __FILE__)++Benchmark.ips do |x|++ x.report("redis setex string") do |times|+ while times > 0+ $redis.setex("test_key", 60, "test")+ times -= 1+ end+ end++ x.report("redis setex marshal string") do |times|+ while times > 0+ $redis.setex("test_keym", 60, Marshal.dump("test"))+ times -= 1+ end+ end++ x.report("Discourse cache string") do |times|+ while times > 0+ Discourse.cache.write("test_key", "test", expires_in: 60)+ times -= 1+ end+ end++ x.report("Rails cache string") do |times|+ while times > 0+ Rails.cache.write("test_key_rails", "test", expires_in: 60)+ times -= 1+ end+ end++ x.compare!+end++Benchmark.ips do |x|+ x.report("redis get string") do |times|+ while times > 0+ $redis.get("test_key")+ times -= 1+ end+ end++ x.report("redis get string marshal") do |times|+ while times > 0+ Marshal.load($redis.get("test_keym"))+ times -= 1+ end+ end++ x.report("Discourse read cache string") do |times|+ while times > 0+ Discourse.cache.read("test_key")+ times -= 1+ end+ end++ x.report("Rails read cache string") do |times|+ while times > 0+ Rails.cache.read("test_key_rails")+ times -= 1+ end+ end++ x.compare!+end++# Comparison:+# redis setex string: 13250.0 i/s+# redis setex marshal string: 12866.4 i/s - same-ish: difference falls within error+# Discourse cache string: 10443.0 i/s - 1.27x slower+# Rails cache string: 10367.9 i/s - 1.28x slower++# Comparison:+# redis get string: 13147.4 i/s+# redis get string marshal: 12789.2 i/s - same-ish: difference falls within error+# Rails read cache string: 10486.4 i/s - 1.25x slower+# Discourse read cache string: 10457.1 i/s - 1.26x slowerdiff --git a/spec/components/cache_spec.rb b/spec/components/cache_spec.rbindex 33c54a573b0..178905a77a3 100644--- a/spec/components/cache_spec.rb+++ b/spec/components/cache_spec.rb@@ -9,6 +9,11 @@ describe Cache doCache.newend+ it "supports float" do+ cache.write("float", 1.1)+ expect(cache.read("float")).to eq(1.1)+ end+it "supports fixnum" docache.write("num", 1)expect(cache.read("num")).to eq(1)diff --git a/spec/components/site_setting_extension_spec.rb b/spec/components/site_setting_extension_spec.rbindex cfc28d77ed1..ee856e144c0 100644--- a/spec/components/site_setting_extension_spec.rb+++ b/spec/components/site_setting_extension_spec.rb@@ -800,7 +800,7 @@ describe SiteSettingExtension doit 'expires the cache' dosettings.default_locale = 'zh_CN'- expect(Rails.cache.exist?(SiteSettingExtension.client_settings_cache_key)).to be_falsey+ expect(Discourse.cache.exist?(SiteSettingExtension.client_settings_cache_key)).to be_falseyendit 'refreshes the client' dodiff --git a/spec/requests/onebox_controller_spec.rb b/spec/requests/onebox_controller_spec.rbindex 40e95b12fc8..1d14499e414 100644--- a/spec/requests/onebox_controller_spec.rb+++ b/spec/requests/onebox_controller_spec.rb@@ -56,7 +56,7 @@ describe OneboxController dostub_request(:get, url).to_return(status: 200, body: html).then.to_raisebypass_limiting- Rails.cache.delete("onebox__#{url}")+ Discourse.cache.delete("onebox__#{url}")get "/onebox.json", params: { url: url }expect(response.status).to eq(200)expect(response.body).to include("Onebox1")