From 23f163d43e396880123ff966cf88423124b29db2 Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Fri, 15 Oct 2021 17:35:29 +0200 Subject: [PATCH] Make installing dalli and redis gem optional --- Gemfile | 3 +++ README.md | 18 ++++++++++++++---- lib/suo.rb | 17 ++++++++++++----- lib/suo/client/memcached.rb | 6 +++++- lib/suo/client/redis.rb | 4 ++++ suo.gemspec | 2 -- 6 files changed, 38 insertions(+), 12 deletions(-) diff --git a/Gemfile b/Gemfile index b4e2a20..2aef6ca 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,6 @@ source "https://rubygems.org" gemspec + +gem 'redis' +gem 'dalli' diff --git a/README.md b/README.md index 39a86b0..598584e 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,21 @@ Suo provides a very performant distributed lock solution using Compare-And-Set ( ## Installation -Add this line to your application’s Gemfile: +1. Install the gem -```ruby -gem 'suo' -``` + ```ruby + gem 'suo' + ``` + +2. Install at least one caching library gem + + ```ruby + # to use Suo::Client::Memcached + gem 'dalli' + + # to use Suo::Client::Redis + gem 'redis' + ``` ## Usage diff --git a/lib/suo.rb b/lib/suo.rb index 77130ac..e06fae0 100644 --- a/lib/suo.rb +++ b/lib/suo.rb @@ -1,10 +1,16 @@ require "securerandom" require "monitor" -require "dalli" -require "dalli/cas/client" +begin + require "dalli" + require "dalli/cas/client" +rescue LoadError +end -require "redis" +begin + require "redis" +rescue LoadError +end require "msgpack" @@ -12,5 +18,6 @@ require "suo/errors" require "suo/client/base" -require "suo/client/memcached" -require "suo/client/redis" + +require "suo/client/memcached" if defined?(Dalli) +require "suo/client/redis" if defined?(Redis) diff --git a/lib/suo/client/memcached.rb b/lib/suo/client/memcached.rb index dbc74b8..23a73af 100644 --- a/lib/suo/client/memcached.rb +++ b/lib/suo/client/memcached.rb @@ -2,7 +2,11 @@ module Suo module Client class Memcached < Base def initialize(key, options = {}) - options[:client] ||= Dalli::Client.new(options[:connection] || ENV["MEMCACHE_SERVERS"] || "127.0.0.1:11211") + if !options[:client] && !defined?(::Dalli) + raise "Dalli class found. Please make sure you have 'dalli' as a dependency in your gemfile (`gem 'dalli'`)." + end + + options[:client] ||= ::Dalli::Client.new(options[:connection] || ENV["MEMCACHE_SERVERS"] || "127.0.0.1:11211") super end diff --git a/lib/suo/client/redis.rb b/lib/suo/client/redis.rb index 2a106c7..e8f7127 100644 --- a/lib/suo/client/redis.rb +++ b/lib/suo/client/redis.rb @@ -4,6 +4,10 @@ class Redis < Base OK_STR = "OK".freeze def initialize(key, options = {}) + if !options[:client] && !defined?(::Redis) + raise "Redis class not found. Please make sure you have 'redis' as a dependency in your gemfile (`gem 'redis'`)." + end + options[:client] ||= ::Redis.new(options[:connection] || {}) super end diff --git a/suo.gemspec b/suo.gemspec index cada028..fe42dea 100644 --- a/suo.gemspec +++ b/suo.gemspec @@ -21,8 +21,6 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.5" - spec.add_dependency "dalli" - spec.add_dependency "redis" spec.add_dependency "msgpack" spec.add_development_dependency "bundler"