DEV: use Discourse.cache over Rails.cache

Discourse.cache is a more consistent method to use and offers clean fallback
if you are skipping redis

This is part of a larger change that both optimizes Discoruse.cache and omits
use of setex on $redis in favor of consistently using discourse cache

Bench does reveal that use of Rails.cache and Discourse.cache is 1.25x slower
than redis.setex / get so a re-implementation will follow prior to porting
This commit is contained in:
Sam Saffron
2019-11-27 12:35:14 +11:00
parent 0807751390
commit 0fb497eb23
11 changed files with 102 additions and 15 deletions

81
script/benchmarks/cache/bench.rb vendored Normal file
View File

@ -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