DEV: Implement multiple keys support for DiscourseRedis#exists.

This commit is contained in:
Guo Xiang Tan 2020-06-01 11:20:20 +08:00
parent 3894555b2e
commit df62407f35
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
3 changed files with 39 additions and 2 deletions

View File

@ -489,7 +489,7 @@ module Discourse
end end
def self.readonly_mode?(keys = READONLY_KEYS) def self.readonly_mode?(keys = READONLY_KEYS)
recently_readonly? || Discourse.redis.mget(*keys).compact.present? recently_readonly? || Discourse.redis.exists(*keys)
end end
def self.pg_readonly_mode? def self.pg_readonly_mode?

View File

@ -194,7 +194,7 @@ class DiscourseRedis
end end
# Proxy key methods through, but prefix the keys with the namespace # 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, :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, :incrby, :incrbyfloat, :lindex, :linsert, :llen, :lpop, :lpush, :lpushx, :lrange, :lrem, :lset, :ltrim,
:mapped_hmset, :mapped_hmget, :mapped_mget, :mapped_mset, :mapped_msetnx, :move, :mset, :mapped_hmset, :mapped_hmget, :mapped_mget, :mapped_mset, :mapped_msetnx, :move, :mset,
@ -208,6 +208,17 @@ class DiscourseRedis
end end
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) def mget(*args)
args.map! { |a| "#{namespace}:#{a}" } if @namespace args.map! { |a| "#{namespace}:#{a}" } if @namespace
DiscourseRedis.ignore_readonly { @redis.mget(*args) } DiscourseRedis.ignore_readonly { @redis.mget(*args) }

View File

@ -87,6 +87,32 @@ describe DiscourseRedis do
expect(Discourse.recently_readonly?).to eq(true) expect(Discourse.recently_readonly?).to eq(true)
end end
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 end
context '.slave_host' do context '.slave_host' do