forked from jekyll/classifier-reborn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Redis Connection to be Injected
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
Showing
2 changed files
with
21 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |