module Redised
Redised
allows for the common patter of module access to redis, when included a .redis and .redis= method are provided
Constants
- VERSION
Public Class Methods
included(klass)
click to toggle source
# File lib/redised.rb, line 82 def self.included(klass) klass.module_eval do # Accepts: # 1. A 'hostname:port' string # 2. A 'hostname:port:db' string (to select the Redis db) # 3. A 'hostname:port/namespace' string (to set the Redis namespace) # 4. A redis URL string 'redis://host:port' # 5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`, # or `Redis::Namespace`. # 6. A 'hostname:port:db:password' string (to select the Redis db & a password) def self.redis=(server) if server.respond_to? :split if server =~ /redis\:\/\// conn = ::Redised.redis_connection(:url => server) else server, namespace = server.split('/', 2) host, port, db, password = server.split(':') conn = ::Redised.redis_connection(Redised.redis_options.merge({ :host => host, :port => port, :thread_safe => true, :db => db, :password => password })) end @_redis = namespace ? Redis::Namespace.new(namespace, :redis => conn) : conn else @_redis = server end end def self.redised_namespace(new_name = nil) if new_name @_namespace = new_name @_redis = nil else @_namespace end end # Returns the current Redis connection. If none has been created, will # create a new one. def self.redis return @_redis if @_redis if ::Redised.redised_config self.redis = if redised_namespace ::Redised.redised_config[redised_namespace][::Redised.redised_env] else ::Redised.redised_config[::Redis.redised_env] end else self.redis = 'localhost:6379' end @_redis rescue NoMethodError => e raise("There was a problem setting up your redis for redised_namespace #{redised_namespace} (from file #{@_redised_config_path}): #{e}") end def redis self.class.redis end end end
redis()
click to toggle source
Returns the current Redis connection. If none has been created, will create a new one.
# File lib/redised.rb, line 127 def self.redis return @_redis if @_redis if ::Redised.redised_config self.redis = if redised_namespace ::Redised.redised_config[redised_namespace][::Redised.redised_env] else ::Redised.redised_config[::Redis.redised_env] end else self.redis = 'localhost:6379' end @_redis rescue NoMethodError => e raise("There was a problem setting up your redis for redised_namespace #{redised_namespace} (from file #{@_redised_config_path}): #{e}") end
redis=(server)
click to toggle source
Accepts:
1. A 'hostname:port' string 2. A 'hostname:port:db' string (to select the Redis db) 3. A 'hostname:port/namespace' string (to set the Redis namespace) 4. A redis URL string 'redis://host:port' 5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`, or `Redis::Namespace`. 6. A 'hostname:port:db:password' string (to select the Redis db & a password)
# File lib/redised.rb, line 93 def self.redis=(server) if server.respond_to? :split if server =~ /redis\:\/\// conn = ::Redised.redis_connection(:url => server) else server, namespace = server.split('/', 2) host, port, db, password = server.split(':') conn = ::Redised.redis_connection(Redised.redis_options.merge({ :host => host, :port => port, :thread_safe => true, :db => db, :password => password })) end @_redis = namespace ? Redis::Namespace.new(namespace, :redis => conn) : conn else @_redis = server end end
redis_connect()
click to toggle source
(Re)Connect all the redis connections
# File lib/redised.rb, line 28 def self.redis_connect @_redis_connections.collect do |params, redis| redis.client.connect [params, redis.client.connected?] end end
redis_connection(params)
click to toggle source
Get a reusable connection based on a set of params. The params are the same as the options you pass to ‘Redis.new`
This ensures that an app doesnt try to open multiple connections to the same redis server.
# File lib/redised.rb, line 14 def self.redis_connection(params) @_redis_connections ||= {} @_redis_connections[params] ||= Redis.new(params) end
redis_disconnect()
click to toggle source
Disconnect all the redis connections
# File lib/redised.rb, line 20 def self.redis_disconnect @_redis_connections.collect do |params, redis| redis.client.disconnect [params, redis.client.connected?] end end
redis_options()
click to toggle source
# File lib/redised.rb, line 78 def self.redis_options @_redised_redis_options || {} end
redis_options=(options)
click to toggle source
Set global redis options
Redised.redis_options = {:timeout => 1.0}
# File lib/redised.rb, line 74 def self.redis_options=(options) @_redised_redis_options = options || {} end
redised_config()
click to toggle source
Load/parse the YAML config setup at ‘redised_config_path`. If no config is setup, returns nil
Configs are in the format:
--- namespace: env: url
# File lib/redised.rb, line 44 def self.redised_config if @_redised_config_path @_redised_config ||= YAML.load_file(@_redised_config_path) end end
redised_config_path()
click to toggle source
Return the config path for the YAML config.
# File lib/redised.rb, line 51 def self.redised_config_path @_redised_config_path end
redised_config_path=(new_path)
click to toggle source
Set the config path to load from.
# File lib/redised.rb, line 56 def self.redised_config_path=(new_path) @_redised_config_path = new_path @_redised_config = nil end
redised_env()
click to toggle source
# File lib/redised.rb, line 61 def self.redised_env @_redised_env ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || nil end
redised_env=(new_env)
click to toggle source
# File lib/redised.rb, line 65 def self.redised_env=(new_env) @_redised_env = new_env @_redised_config = nil end
redised_namespace(new_name = nil)
click to toggle source
# File lib/redised.rb, line 116 def self.redised_namespace(new_name = nil) if new_name @_namespace = new_name @_redis = nil else @_namespace end end
Public Instance Methods
redis()
click to toggle source
# File lib/redised.rb, line 143 def redis self.class.redis end