redis-store-fix-expiry.rb 1.38 KB
Newer Older
1 2 3 4 5 6
# Monkey-patch Redis::Store to make 'setex' and 'expire' work with namespacing

module Gitlab
  class Redis
    class Store
      module Namespace
7
        # Redis::Store#setex in redis-store 1.1.4 does not respect namespaces;
8
        # this new method does.
9 10 11 12
        def setex(key, expires_in, value, options=nil)
          namespace(key) { |key| super(key, expires_in, value) }
        end

13 14
        # Redis::Store#expire in redis-store 1.1.4 does not respect namespaces;
        # this new method does.
15 16 17
        def expire(key, expires_in)
          namespace(key) { |key| super(key, expires_in) }
        end
18 19 20 21 22 23

        private

        # Our new definitions of #setex and #expire above assume that the
        # #namespace method exists. Because we cannot be sure of that, we
        # re-implement the #namespace method from Redis::Store::Namespace so
Jacob Vosmaer's avatar
Jacob Vosmaer committed
24 25
        # that it is available for all Redis::Store instances, whether they use
        # namespacing or not.
26 27 28 29 30 31 32 33 34 35 36
        #
        # Based on lib/redis/store/namespace.rb L49-51 (redis-store 1.1.4)
        def namespace(key)
          if @namespace
            yield interpolate(key)
          else
            # This Redis::Store instance does not use a namespace so we should
            # just pass through the key.
            yield key
          end
        end
37 38 39 40 41 42 43 44
      end
    end
  end
end

Redis::Store.class_eval do
  include Gitlab::Redis::Store::Namespace
end