Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make installing dalli and redis gem optional #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
source "https://rubygems.org"

gemspec

gem 'redis'
gem 'dalli'
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 12 additions & 3 deletions lib/suo.rb
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
6 changes: 5 additions & 1 deletion lib/suo/client/memcached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/suo/client/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions suo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ 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"
spec.add_development_dependency "rake", "~> 13.0"
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