mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 10:57:04 +08:00
DEV: Implement multiple keys support for DiscourseRedis#exists
.
This commit is contained in:
parent
3894555b2e
commit
df62407f35
|
@ -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?
|
||||
|
|
|
@ -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) }
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user