Module: Familia::Connection
- Included in:
- Familia
- Defined in:
- lib/familia/connection.rb
Overview
The Connection module provides Redis connection management for Familia. It allows easy setup and access to Redis clients across different URIs.
Instance Attribute Summary collapse
-
#enable_redis_counter ⇒ Boolean
Whether Redis command counter is enabled.
-
#enable_redis_logging ⇒ Boolean
Whether Redis command logging is enabled.
-
#redis_clients ⇒ Hash
readonly
A hash of Redis clients, keyed by server ID.
-
#uri ⇒ URI
(also: #url)
The default URI for Redis connections.
Instance Method Summary collapse
-
#connect(uri = nil) ⇒ Redis
Establishes a connection to a Redis server.
-
#redis(uri = nil) ⇒ Redis
Retrieves or creates a Redis client for the given URI.
-
#redis_uri_by_class(klass) ⇒ Redis
Retrieves the Redis client associated with the given class.
Instance Attribute Details
#enable_redis_counter ⇒ Boolean
Returns Whether Redis command counter is enabled.
24 25 26 |
# File 'lib/familia/connection.rb', line 24 def enable_redis_counter @enable_redis_counter end |
#enable_redis_logging ⇒ Boolean
Returns Whether Redis command logging is enabled.
21 22 23 |
# File 'lib/familia/connection.rb', line 21 def enable_redis_logging @enable_redis_logging end |
#redis_clients ⇒ Hash (readonly)
Returns A hash of Redis clients, keyed by server ID.
15 16 17 |
# File 'lib/familia/connection.rb', line 15 def redis_clients @redis_clients end |
#uri ⇒ URI Also known as: url
Returns The default URI for Redis connections.
18 19 20 |
# File 'lib/familia/connection.rb', line 18 def uri @uri end |
Instance Method Details
#connect(uri = nil) ⇒ Redis
Establishes a connection to a Redis server.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/familia/connection.rb', line 34 def connect(uri = nil) uri = URI.parse(uri) if uri.is_a?(String) uri ||= @redis_uri_by_class[self] uri ||= Familia.uri raise ArgumentError, 'No URI specified' unless uri conf = uri.conf @redis_uri_by_class[self] = uri.serverid if Familia.enable_redis_logging RedisLogger.logger = Familia.logger RedisClient.register(RedisLogger) end if Familia.enable_redis_counter # NOTE: This middleware stays thread-safe with a mutex so it will # be a bottleneck when enabled in multi-threaded environments. RedisClient.register(RedisCommandCounter) end redis = Redis.new(conf) # Close the existing connection if it exists @redis_clients[uri.serverid].close if @redis_clients[uri.serverid] @redis_clients[uri.serverid] = redis end |
#redis(uri = nil) ⇒ Redis
Retrieves or creates a Redis client for the given URI.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/familia/connection.rb', line 69 def redis(uri = nil) if uri.is_a?(Integer) tmp = Familia.uri tmp.db = uri uri = tmp elsif uri.is_a?(String) uri &&= URI.parse uri end uri ||= Familia.uri connect(uri) unless @redis_clients[uri.serverid] @redis_clients[uri.serverid] end |
#redis_uri_by_class(klass) ⇒ Redis
Retrieves the Redis client associated with the given class.
86 87 88 89 |
# File 'lib/familia/connection.rb', line 86 def redis_uri_by_class(klass) uri = @redis_uri_by_class[klass] connect(uri) end |