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..1a6bba8 100644 --- a/lib/suo.rb +++ b/lib/suo.rb @@ -1,10 +1,19 @@ require "securerandom" require "monitor" -require "dalli" -require "dalli/cas/client" +begin + require "dalli" -require "redis" + if Gem::Version.new(Dalli::VERSION) < Gem::Version.new('3.0.0') + require "dalli/cas/client" + end +rescue LoadError +end + +begin + require "redis" +rescue LoadError +end require "msgpack" diff --git a/lib/suo/client/memcached.rb b/lib/suo/client/memcached.rb index dbc74b8..e7ec2c1 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 not 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..d2a4dd2 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" @@ -30,4 +28,18 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rubocop", "~> 0.49.0" spec.add_development_dependency "minitest", "~> 5.5.0" spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4.7" + + spec.post_install_message = <<~MSG + **BREAKING CHANGE** + + 'dalli' and 'redis' gems are not automatically installed with 'suo' anymore. + + Please make sure to add the gem you use to your gemfile. + + If you use `Suo::Client::Memcached`: + gem 'dalli' + + If you use `Suo::Client::Redis`: + gem 'redis' + MSG end