Skip to content

Allow Redis Connection to be Injected#189

Open
allcentury wants to merge 1 commit into
jekyll:masterfrom
allcentury:redis-inject
Open

Allow Redis Connection to be Injected#189
allcentury wants to merge 1 commit into
jekyll:masterfrom
allcentury:redis-inject

Conversation

@allcentury

@allcentury allcentury commented Nov 1, 2019

Copy link
Copy Markdown

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_conn parameter.

Comment thread test/backends/backend_redis_injected_test.rb
Comment thread lib/classifier-reborn/backends/bayes_redis_backend.rb Outdated
Comment thread lib/classifier-reborn/backends/bayes_redis_backend.rb Outdated
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.
@allcentury

Copy link
Copy Markdown
Author

@ashmaroli friendly ping - does all look ok here?

@ashmaroli

Copy link
Copy Markdown
Member

Sorry. This had slipped out of my radar..
However, I'll leave the merging to other maintainers here.

@lewispb

lewispb commented Dec 6, 2019

Copy link
Copy Markdown

+1 for this

end

@redis = Redis.new(options)
@redis = options.fetch(:redis_conn, Redis.new(options))

Copy link
Copy Markdown

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.

Suggested change
@redis = options.fetch(:redis_conn, Redis.new(options))
@redis = options.fetch(:redis, Redis.new(options))

@cardmagic

Copy link
Copy Markdown
Contributor

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 Classifier::Storage::Base protocol that you can implement for any backend. Here's the Redis example from the documentation:

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)
end

Usage:

# 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants