about summary refs log tree commit diff
path: root/app/lib/redis_configuration.rb
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2022-04-28 19:24:18 +0200
committerGitHub <noreply@github.com>2022-04-28 19:24:18 +0200
commit78f7f23ad21359893cb022b7c2f7644d5c22cb43 (patch)
tree77b919683a8656a361d7d62c8745233bc8b2d310 /app/lib/redis_configuration.rb
parent6a9d1549484a6fb02d7d01e884577a7185302046 (diff)
parentf23f784f1811a5e7ae82faaf8868e389e9608f5d (diff)
Merge pull request #1756 from ClearlyClaire/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/lib/redis_configuration.rb')
-rw-r--r--app/lib/redis_configuration.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/app/lib/redis_configuration.rb b/app/lib/redis_configuration.rb
new file mode 100644
index 000000000..fc8cf2f80
--- /dev/null
+++ b/app/lib/redis_configuration.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+class RedisConfiguration
+  class << self
+    def with
+      pool.with { |redis| yield redis }
+    end
+
+    def pool
+      @pool ||= ConnectionPool.new(size: pool_size) { new.connection }
+    end
+
+    def pool_size
+      if Sidekiq.server?
+        Sidekiq.options[:concurrency]
+      else
+        ENV['MAX_THREADS'] || 5
+      end
+    end
+  end
+
+  def connection
+    if namespace?
+      Redis::Namespace.new(namespace, redis: raw_connection)
+    else
+      raw_connection
+    end
+  end
+
+  def namespace?
+    namespace.present?
+  end
+
+  def namespace
+    ENV.fetch('REDIS_NAMESPACE', nil)
+  end
+
+  def url
+    ENV['REDIS_URL']
+  end
+
+  private
+
+  def raw_connection
+    Redis.new(url: url, driver: :hiredis)
+  end
+end