Skip to content

Commit

Permalink
Restore backwards compatibility broken in 4.3.0 (#51)
Browse files Browse the repository at this point in the history
Fixes #50
  • Loading branch information
benlangfeld authored Aug 1, 2024
1 parent 4e96170 commit 9cb046f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
22 changes: 19 additions & 3 deletions lib/modis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
require 'modis/model'

module Modis
@mutex = Mutex.new
class << self
attr_writer :connection_pool_size, :connection_pool_timeout,
:connection_pools

def mutex
@mutex ||= Mutex.new
end

def redis_options
@redis_options ||= { default: {} }
end
Expand All @@ -31,7 +34,20 @@ def redis_options=(options)
if options.is_a?(Hash) && options.values.first.is_a?(Hash)
@redis_options = options.transform_values(&:dup)
else
@redis_options[:default] = options
redis_options[:default] = options
end
end

def reset!
connection_pools.each do |key, _|
with_connection(key) do |connection|
keys = connection.keys "#{config.namespace}:*"
connection.del(*keys) unless keys.empty?
end
end
instance_variables.each do |var|
instance_variable_set(var, nil)
remove_instance_variable(var)
end
end

Expand All @@ -49,7 +65,7 @@ def connection_pools

def connection_pool(pool_name = :default)
connection_pools[pool_name] ||= begin
@mutex.synchronize do
mutex.synchronize do
ConnectionPool.new(
size: connection_pool_size,
timeout: connection_pool_timeout
Expand Down
6 changes: 3 additions & 3 deletions lib/modis/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Configuration < Struct.new(:namespace)
end

class << self
attr_reader :config
def config
@config ||= Configuration.new
end
end

@config = Configuration.new
end
2 changes: 2 additions & 0 deletions spec/multi_redis_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'spec_helper'

module MultiRedisSpec
class DefaultUserModel
include Modis::Model
Expand Down
12 changes: 3 additions & 9 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@

Redis.raise_deprecations = true if Gem.loaded_specs['redis'].version >= Gem::Version.new('4.6.0')

Modis.configure do |config|
config.namespace = 'modis'
end

RSpec.configure do |config|
config.after :each do
RSpec::Mocks.space.proxy_for(Modis).reset
Modis.connection_pools.each do |key, _|
Modis.with_connection(key) do |connection|
keys = connection.keys "#{Modis.config.namespace}:*"
connection.del(*keys) unless keys.empty?
end
Modis.reset!
Modis.configure do |modis_config|
modis_config.namespace = 'modis'
end
end
end

0 comments on commit 9cb046f

Please sign in to comment.