-
Notifications
You must be signed in to change notification settings - Fork 112
Allow Redis Connection to be Injected #189
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
base: master
Are you sure you want to change the base?
Conversation
3a26c63 to
372025d
Compare
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.
372025d to
bf82c5c
Compare
|
@ashmaroli friendly ping - does all look ok here? |
|
Sorry. This had slipped out of my radar.. |
|
+1 for this |
| end | ||
|
|
||
| @redis = Redis.new(options) | ||
| @redis = options.fetch(:redis_conn, Redis.new(options)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor point but my preference for the argument name would just be redis.
| @redis = options.fetch(:redis_conn, Redis.new(options)) | |
| @redis = options.fetch(:redis, Redis.new(options)) |
|
FYI, the original classifier gem now has a pluggable storage system that makes Redis integration straightforward. Instead of baking Redis directly into the gem, it provides a class RedisStorage < Classifier::Storage::Base
def initialize(redis:, key:)
@redis, @key = redis, key
end
def write(data) = @redis.set(@key, data)
def read = @redis.get(@key)
def delete = @redis.del(@key)
def exists? = @redis.exists?(@key)
endUsage: # Inject your own Redis connection (pooled or otherwise)
redis_storage = RedisStorage.new(redis: my_redis_pool.checkout, key: 'classifier:spam')
# Create classifier with storage
classifier = Classifier::Bayes.new('Spam', 'Ham')
classifier.storage = redis_storage
# Train and save
classifier.train(:spam, 'Buy now!')
classifier.save
# Or load existing classifier from storage
classifier = Classifier::Bayes.load(storage: redis_storage)This approach lets you use connection pools, configure timeouts, or use any Redis client library you prefer - the gem doesn't dictate the connection management strategy. See: https://github.com/cardmagic/classifier/blob/master/lib/classifier/storage/base.rb |
See issue #188 for details.
Prior to this change, pooled redis connections were hard to share across jobs.
Now you can inject your own redis connection into the intiailizer via
the
redis_connparameter.