Skip to content

a simple module that allows you to setup your class or module with a redis connection

License

Notifications You must be signed in to change notification settings

paperlesspost/redised

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redised

Redised provides a simple module for establishing reusable class level namespaced redis connections in your vanilla Ruby classes.

Why?

We use redis a lot throughout our apps. This pattern of having a class level ‘redis` method thats already namespaced and connected to a specific server via a config has served us well. It has allowed us to split different usages across different namespaces and eventually different hosts/servers.

Usage

You can include Redised as a module in your class.

class MyRedisClass
  include Redised
end

This now gives you the power to assign the url of the redis server you want to connect to as a string:

MyRedisClass.redis = 'localhost:6739:1/myredisclass' # hostname:port:db/namespace
# also accepts a Redis or Redis::Namespace object

This gets a little easier and more powerful when you setup a YAML config:

# in config/redis.yml 
---
mynamespace:
  development: localhost:6379
  production: redis01:6379/mynamespace
othernamespace:
  development: localhost:6379
  production: redis01:6379/othernamespace

You can tell redised where this config lives:

Redised.redised_config_path = File.join(Rails.root, 'config', 'redis.yml')

And what the ‘env’ is (will try to pull from RACK_ENV and RAILS_ENV):

Redised.redised_env = Rails.env #=> 'production'

Then in your class you tell it what namespace config this class points to:

class MyRedisClass
  include Redised

  redised_namespace 'mynamespace'

end

It will now automatically load the correct redis connection for your namespace/env:

MyRedisClass.redis #=> #<Redis::Namespace ..>
MyRedisClass.redis._client.host #=> 'redis01'
MyRedisClass.namespace #=> 'mynamespace'

The implementation of the ‘self.redis` method is very close to the `Resque.redis` method and is actually a drop in replacement (if you want to load your connection settings from a config). Our `config/initializers/resque.rb` looks like:

module Resque
  include Redised
  extend self

  redised_namespace 'paperless'
end

Acknowledgments

The original parsing of URL idea came from @defunkt/resque.

Contributing to redised

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.

  • Fork the project.

  • Start a feature/bugfix branch.

  • Commit and push until you are happy with your contribution.

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2012 Aaron Quint, Paperless Inc. See LICENSE.txt for further details.

About

a simple module that allows you to setup your class or module with a redis connection

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages