Skip to content

A platform for community discussion. Free, open, simple.

DEV: Implement multiple keys support for `DiscourseRedis#exists`.

Guo Xiang Tandf62407f3506
diff --git a/lib/discourse.rb b/lib/discourse.rb
index aa417260daf..84175589723 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -489,7 +489,7 @@ module Discourse
end
def self.readonly_mode?(keys = READONLY_KEYS)
- recently_readonly? || Discourse.redis.mget(*keys).compact.present?
+ recently_readonly? || Discourse.redis.exists(*keys)
end
def self.pg_readonly_mode?
diff --git a/lib/discourse_redis.rb b/lib/discourse_redis.rb
index 6b630b7b34d..5ef5de9ce9d 100644
--- a/lib/discourse_redis.rb
+++ b/lib/discourse_redis.rb
@@ -194,7 +194,7 @@ class DiscourseRedis
end
# Proxy key methods through, but prefix the keys with the namespace
- [:append, :blpop, :brpop, :brpoplpush, :decr, :decrby, :exists, :expire, :expireat, :get, :getbit, :getrange, :getset,
+ [:append, :blpop, :brpop, :brpoplpush, :decr, :decrby, :expire, :expireat, :get, :getbit, :getrange, :getset,
:hdel, :hexists, :hget, :hgetall, :hincrby, :hincrbyfloat, :hkeys, :hlen, :hmget, :hmset, :hset, :hsetnx, :hvals, :incr,
:incrby, :incrbyfloat, :lindex, :linsert, :llen, :lpop, :lpush, :lpushx, :lrange, :lrem, :lset, :ltrim,
:mapped_hmset, :mapped_hmget, :mapped_mget, :mapped_mset, :mapped_msetnx, :move, :mset,
@@ -208,6 +208,17 @@ class DiscourseRedis
end
end
+ # Remove when this has been upstreamed in https://github.com/redis/redis-rb/pull/911
+ def exists(*keys)
+ keys.map! { |a| "#{namespace}:#{a}" } if @namespace
+
+ DiscourseRedis.ignore_readonly do
+ @redis._client.call([:exists, *keys]) do |value|
+ value > 0
+ end
+ end
+ end
+
def mget(*args)
args.map! { |a| "#{namespace}:#{a}" } if @namespace
DiscourseRedis.ignore_readonly { @redis.mget(*args) }
diff --git a/spec/components/discourse_redis_spec.rb b/spec/components/discourse_redis_spec.rb
index abc9a5917a8..059e1449628 100644
--- a/spec/components/discourse_redis_spec.rb
+++ b/spec/components/discourse_redis_spec.rb
@@ -87,6 +87,32 @@ describe DiscourseRedis do
expect(Discourse.recently_readonly?).to eq(true)
end
end
+
+ # Remove when this has been upstreamed in https://github.com/redis/redis-rb/pull/911
+ describe '.exists' do
+ it 'should return false when key is not present' do
+ expect(Discourse.redis.exists('test')).to eq(false)
+ end
+
+ it 'should return false when keys are not present' do
+ expect(Discourse.redis.exists('test', 'test2')).to eq(false)
+ end
+
+ it 'should return true when key is present' do
+ Discourse.redis.set('test', 1)
+
+ expect(Discourse.redis.exists('test')).to eq(true)
+ end
+
+ it 'should return true when any key is present' do
+ Discourse.redis.set('test', 1)
+ Discourse.redis.set('test2', 1)
+
+ expect(Discourse.redis.exists('test')).to eq(true)
+ expect(Discourse.redis.exists('test', 'test2')).to eq(true)
+ expect(Discourse.redis.exists('test2', 'test3')).to eq(true)
+ end
+ end
end
context '.slave_host' do