Skip to content

Commit

Permalink
Allow Redis Connection to be Injected
Browse files Browse the repository at this point in the history
See issue jekyll#188 for details.

Prior to this change, pooled redis connections were hard to share across.

Now you can inject your own redis connection into the intiailizer via
the `redis_conn` parameter.
  • Loading branch information
Anthony Ross committed Nov 1, 2019
1 parent 0d5564a commit 3a26c63
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/classifier-reborn/backends/bayes_redis_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
module ClassifierReborn
# This class provides Redis as the storage backend for the classifier data structures
class BayesRedisBackend
# The class can be created with the same arguments that the redis gem accepts
# The class can be given an existing redis connection, or it can create a new connection for you with the same arguments that the redis gem accepts
# E.g.,
# b = ClassifierReborn::BayesRedisBackend.new
# b = ClassifierReborn::BayesRedisBackend.new host: "10.0.1.1", port: 6380, db: 2
# b = ClassifierReborn::BayesRedisBackend.new url: "redis://:[email protected]:6380/2"
#
# Options available are:
# redis_conn: an existing redis connection (useful if you are using a pool of connections)
# url: lambda { ENV["REDIS_URL"] }
# scheme: "redis"
# host: "127.0.0.1"
Expand All @@ -33,7 +34,7 @@ def initialize(options = {})
raise NoRedisError
end

@redis = Redis.new(options)
@redis = options.fetch(:redis_conn, Redis.new(options))
@redis.setnx(:total_words, 0)
@redis.setnx(:total_trainings, 0)
end
Expand Down
18 changes: 18 additions & 0 deletions test/backends/backend_redis_injected_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require File.dirname(__FILE__) + '/../test_helper'
require_relative './backend_common_tests'

class BackendRedisInjectedTest < Minitest::Test
include BackendCommonTests

def setup
redis = Redis.new
@backend = ClassifierReborn::BayesRedisBackend.new(redis_conn: redis)
redis.config(:set, 'save', '')
rescue Redis::CannotConnectError => e
skip(e)
end

def teardown
@backend.reset if defined? @backend
end
end

0 comments on commit 3a26c63

Please sign in to comment.