Class: Redis::Directory
- Inherits:
-
Object
- Object
- Redis::Directory
- Defined in:
- lib/redis_directory.rb
Defined Under Namespace
Classes: ReservationError, UndefinedServiceError
Constant Summary collapse
- SERVICES_KEY =
"services"
- DATABASES_KEY =
"databases"
- MAXIMUM_DATABASE_COUNT =
Hard-coded, because I don’t know how to get this from the Redis connection at runtime.
65535
Instance Method Summary collapse
- #get(service_name, connection_name) ⇒ Object
-
#initialize(connection_options) ⇒ Directory
constructor
You must provide the
connection_options
to the directory server. -
#next_db(service_name) ⇒ Object
This locates the next available database for a given service, starting at an index of 1.
- #redis ⇒ Object
- #reserve(service_name, connection_name) ⇒ Object
- #services ⇒ Object
Constructor Details
#initialize(connection_options) ⇒ Directory
You must provide the connection_options
to the directory server.
28 29 30 |
# File 'lib/redis_directory.rb', line 28 def initialize() @redis = Redis.connect() end |
Instance Method Details
#get(service_name, connection_name) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/redis_directory.rb', line 64 def get(service_name, connection_name) db = reserve(service_name, connection_name) raise ReservationError.new(self, service_name, connection_name) if db.nil? connection_list = services[service_name].map do |server| if server =~ /^redis\:\/\// "#{server}/#{db}" else "redis://#{server}/#{db}" end end connection = nil if connection_list.size == 1 connection = Redis.connect(:url => connection_list.first) else connection = Redis::Distributed.new(connection_list) end connection.set("connection-name", connection_name) connection end |
#next_db(service_name) ⇒ Object
This locates the next available database for a given service, starting at an index of 1. The 0 database is reserved in case you have a default connection, or local redis server (in which case 0 should be the directory database to avoid conflicts).
45 46 47 48 49 |
# File 'lib/redis_directory.rb', line 45 def next_db(service_name) raise UndefinedServiceError.new(self, service_name) unless services.keys.include?(service_name) databases = redis.hvals("#{service_name}-service").map { |i| i.to_i }.sort (1..MAXIMUM_DATABASE_COUNT).select { |i| break i unless databases.include? i } end |
#redis ⇒ Object
87 88 89 |
# File 'lib/redis_directory.rb', line 87 def redis @redis end |
#reserve(service_name, connection_name) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/redis_directory.rb', line 51 def reserve(service_name, connection_name) new_db = nil # redis.multi do if redis.hexists("#{service_name}-service", connection_name) new_db = redis.hget("#{service_name}-service", connection_name) else new_db = next_db(service_name) redis.hset("#{service_name}-service", connection_name, new_db) end # end new_db end |
#services ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/redis_directory.rb', line 32 def services if redis.exists(SERVICES_KEY) redis.hgetall(SERVICES_KEY).inject({}) do |h,(k,v)| h[k] = JSON.parse(v); h end else {} end end |