Skip to content

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.rb
index aae30f66128..0597cda86fe 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -418,7 +418,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
@@ -438,7 +438,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 a18e34c3ad2..12ff86a646f 100644
--- a/app/models/topic_embed.rb
+++ b/app/models/topic_embed.rb
@@ -228,7 +228,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_first(:embed_url)
response = TopicEmbed.find_remote(url)
diff --git a/lib/discourse.rb b/lib/discourse.rb
index c61a2f14e7d..07b7369a6b8 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -591,6 +591,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 1405edc234e..b76ebe848cf 100644
--- a/lib/inline_oneboxer.rb
+++ b/lib/inline_oneboxer.rb
@@ -14,11 +14,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)
@@ -70,7 +70,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 e726f716852..fe054f5b542 100644
--- a/lib/oneboxer.rb
+++ b/lib/oneboxer.rb
@@ -51,7 +51,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
@@ -61,7 +61,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
@@ -71,7 +71,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.
@@ -281,7 +281,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,
diff --git a/lib/search.rb b/lib/search.rb
index 4e163cb23ce..56503df5d45 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -137,7 +137,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 c19eb8814e7..2c651d0b612 100644
--- a/lib/site_setting_extension.rb
+++ b/lib/site_setting_extension.rb
@@ -192,7 +192,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
@@ -432,7 +432,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 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 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")