Allow Redis Connection to be Injected#189
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.
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.