From b7e7da766d6b82fcc4f53605ad879fd041b6b501 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 6 May 2015 09:53:10 +1000 Subject: [PATCH] avoid static method so we can easily inherit off this class --- lib/discourse.rb | 1 + lib/discourse_redis.rb | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/discourse.rb b/lib/discourse.rb index 653703779ee..2a139eb2d71 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -296,6 +296,7 @@ module Discourse # after fork, otherwise Discourse will be # in a bad state def self.after_fork + # note: all this reconnecting may no longer be needed per https://github.com/redis/redis-rb/pull/414 current_db = RailsMultisite::ConnectionManagement.current_db RailsMultisite::ConnectionManagement.establish_connection(db: current_db) MessageBus.after_fork diff --git a/lib/discourse_redis.rb b/lib/discourse_redis.rb index 7c33dbbd1f9..8cf03960f34 100644 --- a/lib/discourse_redis.rb +++ b/lib/discourse_redis.rb @@ -66,22 +66,22 @@ class DiscourseRedis :sunion, :ttl, :type, :watch, :zadd, :zcard, :zcount, :zincrby, :zrange, :zrangebyscore, :zrank, :zrem, :zremrangebyrank, :zremrangebyscore, :zrevrange, :zrevrangebyscore, :zrevrank, :zrangebyscore].each do |m| define_method m do |*args| - args[0] = "#{DiscourseRedis.namespace}:#{args[0]}" + args[0] = "#{namespace}:#{args[0]}" DiscourseRedis.ignore_readonly { @redis.send(m, *args) } end end def del(k) DiscourseRedis.ignore_readonly do - k = "#{DiscourseRedis.namespace}:#{k}" + k = "#{namespace}:#{k}" @redis.del k end end def keys(pattern=nil) DiscourseRedis.ignore_readonly do - len = DiscourseRedis.namespace.length + 1 - @redis.keys("#{DiscourseRedis.namespace}:#{pattern || '*'}").map{ + len = namespace.length + 1 + @redis.keys("#{namespace}:#{pattern || '*'}").map{ |k| k[len..-1] } end @@ -103,7 +103,12 @@ class DiscourseRedis @redis.client.reconnect end + def namespace + RailsMultisite::ConnectionManagement.current_db + end + def self.namespace + Rails.logger.warn("DiscourseRedis.namespace is going to be deprecated, do not use it!") RailsMultisite::ConnectionManagement.current_db end