diff --git a/app/models/theme.rb b/app/models/theme.rb index 167f01daadf..de9ed2b2ea6 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -410,7 +410,7 @@ class Theme < ActiveRecord::Base end def 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) do hash = {} self.settings.each do |setting| hash[setting.name] = setting.value @@ -420,7 +420,7 @@ class Theme < ActiveRecord::Base end def clear_cached_settings! - Rails.cache.delete("settings_for_theme_#{self.id}") + Discourse.cache.delete("settings_for_theme_#{self.id}") end def included_settings diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb index f7b46b711f1..eddda74c9c5 100644 --- a/app/models/topic_embed.rb +++ b/app/models/topic_embed.rb @@ -231,7 +231,7 @@ class TopicEmbed < ActiveRecord::Base end def 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) do url = TopicEmbed.where(topic_id: post.topic_id).pluck(:embed_url).first response = TopicEmbed.find_remote(url) diff --git a/lib/discourse.rb b/lib/discourse.rb index 2b53a91eb37..2ca11fec169 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -520,6 +520,7 @@ module Discourse SiteSetting.after_fork $redis._client.reconnect Rails.cache.reconnect + Discourse.cache.reconnect Logster.store.redis.reconnect # shuts down all connections in the pool Sidekiq.redis_pool.shutdown { |c| nil } diff --git a/lib/inline_oneboxer.rb b/lib/inline_oneboxer.rb index 04d8ea348c8..cca58193394 100644 --- a/lib/inline_oneboxer.rb +++ b/lib/inline_oneboxer.rb @@ -16,11 +16,11 @@ class InlineOneboxer end def self.purge(url) - Rails.cache.delete(cache_key(url)) + Discourse.cache.delete(cache_key(url)) end def self.cache_lookup(url) - Rails.cache.read(cache_key(url)) + Discourse.cache.read(cache_key(url)) end def self.lookup(url, opts = nil) @@ -72,7 +72,7 @@ class InlineOneboxer title: 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) end onebox diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index a4c9163804b..fdd19f64188 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -47,7 +47,7 @@ module Oneboxer end def self.cached_onebox(url) - if c = Rails.cache.read(onebox_cache_key(url)) + if c = Discourse.cache.read(onebox_cache_key(url)) c[:onebox] end rescue => e @@ -57,7 +57,7 @@ module Oneboxer end def self.cached_preview(url) - if c = Rails.cache.read(onebox_cache_key(url)) + if c = Discourse.cache.read(onebox_cache_key(url)) c[:preview] end rescue => e @@ -67,7 +67,7 @@ module Oneboxer end def 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. @@ -269,7 +269,7 @@ module Oneboxer end def 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) do fd = FinalDestination.new(url, ignore_redirects: ignore_redirects, ignore_hostnames: blacklisted_domains, force_get_hosts: force_get_hosts, preserve_fragment_url_hosts: preserve_fragment_url_hosts) uri = fd.resolve return blank_onebox if uri.blank? || blacklisted_domains.map { |hostname| uri.hostname.match?(hostname) }.any? diff --git a/lib/search.rb b/lib/search.rb index 13a31492d2c..5fbac3cbb18 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -139,7 +139,7 @@ class Search return 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) do min_post_id_no_cache end end diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb index 8b9e49f1b14..599c1800ec5 100644 --- a/lib/site_setting_extension.rb +++ b/lib/site_setting_extension.rb @@ -197,7 +197,7 @@ module SiteSettingExtension end def 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) do client_settings_json_uncached end end @@ -421,7 +421,7 @@ module SiteSettingExtension protected def clear_cache! - Rails.cache.delete(SiteSettingExtension.client_settings_cache_key) + Discourse.cache.delete(SiteSettingExtension.client_settings_cache_key) Site.clear_anon_cache! end diff --git a/script/benchmarks/cache/bench.rb b/script/benchmarks/cache/bench.rb new file mode 100644 index 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 slower diff --git a/spec/components/cache_spec.rb b/spec/components/cache_spec.rb index 33c54a573b0..178905a77a3 100644 --- a/spec/components/cache_spec.rb +++ b/spec/components/cache_spec.rb @@ -9,6 +9,11 @@ describe Cache do Cache.new end + it "supports float" do + cache.write("float", 1.1) + expect(cache.read("float")).to eq(1.1) + end + it "supports fixnum" do cache.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.rb index 809a1b77c88..3171d4134f7 100644 --- a/spec/components/site_setting_extension_spec.rb +++ b/spec/components/site_setting_extension_spec.rb @@ -802,7 +802,7 @@ describe SiteSettingExtension do it 'expires the cache' do settings.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_falsey end it 'refreshes the client' do diff --git a/spec/requests/onebox_controller_spec.rb b/spec/requests/onebox_controller_spec.rb index 40e95b12fc8..1d14499e414 100644 --- a/spec/requests/onebox_controller_spec.rb +++ b/spec/requests/onebox_controller_spec.rb @@ -56,7 +56,7 @@ describe OneboxController do stub_request(:get, url).to_return(status: 200, body: html).then.to_raise bypass_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")