Skip to content

Commit

Permalink
Make installing dalli and redis gem optional
Browse files Browse the repository at this point in the history
  • Loading branch information
doits committed Oct 15, 2021
1 parent 7bb28cc commit 4a4fde6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
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
17 changes: 12 additions & 5 deletions lib/suo.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
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"

require "suo/version"

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)
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
2 changes: 0 additions & 2 deletions suo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 4a4fde6

Please sign in to comment.