From 1e1ded16698c596167f3f319e4701c53afae0050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfram=20Mu=CC=88ller?= Date: Wed, 13 Aug 2025 11:31:45 +0200 Subject: [PATCH] Allow setting redis options --- README.md | 4 ++++ lib/gush/client.rb | 8 ++++---- lib/gush/configuration.rb | 15 +++++++++++++-- spec/gush/configuration_spec.rb | 16 ++++++++++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 505aa56..053a0b8 100644 --- a/README.md +++ b/README.md @@ -500,6 +500,8 @@ In order to prevent getting the RedisMutex::LockError error when having a large # config/initializers/gush.rb Gush.configure do |config| config.redis_url = "redis://localhost:6379" + # NB: you can also pass redis options as a hash: + # config.redis = { host: "localhost", port: 6379, db: 1 } config.concurrency = 5 config.locking_duration = 2 # how long you want to wait for the lock to be released, in seconds config.polling_interval = 0.3 # how long the polling interval should be, in seconds @@ -516,6 +518,8 @@ Running `NotifyWorkflow.create` inserts multiple keys into Redis every time it i # config/initializers/gush.rb Gush.configure do |config| config.redis_url = "redis://localhost:6379" + # NB: you can also pass redis options as a hash: + # config.redis = { host: "localhost", port: 6379, db: 1 } config.concurrency = 5 config.ttl = 3600*24*7 end diff --git a/lib/gush/client.rb b/lib/gush/client.rb index 144f25d..b05cd7e 100644 --- a/lib/gush/client.rb +++ b/lib/gush/client.rb @@ -8,12 +8,12 @@ class Client @@redis_connection = Concurrent::ThreadLocalVar.new(nil) def self.redis_connection(config) - cached = (@@redis_connection.value ||= { url: config.redis_url, connection: nil }) - return cached[:connection] if !cached[:connection].nil? && config.redis_url == cached[:url] + cached = (@@redis_connection.value ||= config.redis.merge(connection: nil)) + return cached[:connection] if !cached[:connection].nil? && config.redis == cached.except(:connection) - Redis.new(url: config.redis_url).tap do |instance| + Redis.new(config.redis).tap do |instance| RedisClassy.redis = instance - @@redis_connection.value = { url: config.redis_url, connection: instance } + @@redis_connection.value = config.redis.merge(connection: instance) end end diff --git a/lib/gush/configuration.rb b/lib/gush/configuration.rb index 40118f3..f0d5906 100644 --- a/lib/gush/configuration.rb +++ b/lib/gush/configuration.rb @@ -1,15 +1,17 @@ module Gush class Configuration - attr_accessor :concurrency, :namespace, :redis_url, :ttl, :locking_duration, :polling_interval + attr_accessor :concurrency, :namespace, :redis, :ttl, :locking_duration, :polling_interval def self.from_json(json) new(Gush::JSON.decode(json, symbolize_keys: true)) end def initialize(hash = {}) + raise ArgumentError, "You can't set both redis_url and redis" if hash.key?(:redis_url) && hash.key?(:redis) + self.concurrency = hash.fetch(:concurrency, 5) self.namespace = hash.fetch(:namespace, 'gush') - self.redis_url = hash.fetch(:redis_url, 'redis://localhost:6379') + self.redis = hash.key?(:redis) ? hash.fetch(:redis) : { url: hash.fetch(:redis_url, 'redis://localhost:6379') } self.gushfile = hash.fetch(:gushfile, 'Gushfile') self.ttl = hash.fetch(:ttl, -1) self.locking_duration = hash.fetch(:locking_duration, 2) # how long you want to wait for the lock to be released, in seconds @@ -24,11 +26,20 @@ def gushfile @gushfile.realpath if @gushfile.exist? end + def redis_url=(url) + redis[:url] = url + end + + def redis_url + redis[:url] + end + def to_hash { concurrency: concurrency, namespace: namespace, redis_url: redis_url, + redis: redis, ttl: ttl, locking_duration: locking_duration, polling_interval: polling_interval diff --git a/spec/gush/configuration_spec.rb b/spec/gush/configuration_spec.rb index 9398c26..74cfd38 100644 --- a/spec/gush/configuration_spec.rb +++ b/spec/gush/configuration_spec.rb @@ -22,6 +22,22 @@ end expect(Gush.configuration.redis_url).to eq("redis://localhost") + expect(Gush.configuration.redis).to eq({ url: "redis://localhost" }) + expect(Gush.configuration.concurrency).to eq(25) + expect(Gush.configuration.locking_duration).to eq(5) + expect(Gush.configuration.polling_interval).to eq(0.5) + end + + it "allows setting redis opptions" do + Gush.configure do |config| + config.redis = { host: "localhost", port: 6379, db: 1 } + config.concurrency = 25 + config.locking_duration = 5 + config.polling_interval = 0.5 + end + + expect(Gush.configuration.redis).to eq({ host: "localhost", port: 6379, db: 1 }) + expect(Gush.configuration.redis_url).to eq(nil) expect(Gush.configuration.concurrency).to eq(25) expect(Gush.configuration.locking_duration).to eq(5) expect(Gush.configuration.polling_interval).to eq(0.5)