Class: JOffice::RedisManager
- Inherits:
-
Object
- Object
- JOffice::RedisManager
- Includes:
- FiberEvents, Singleton
- Defined in:
- lib/joffice_redis/redis_manager.rb
Instance Attribute Summary collapse
-
#db_prefix ⇒ Object
readonly
Returns the value of attribute db_prefix.
Instance Method Summary collapse
- #config ⇒ Object
- #config=(v) ⇒ Object
- #error_callback ⇒ Object
- #flush_all ⇒ Object
-
#lock! ⇒ Object
Lock the object so no other instances can modify it.
- #lock_expired?(lock) ⇒ Boolean
- #lock_timeout ⇒ Object
- #logger ⇒ Object
- #logger=(v) ⇒ Object
- #open_db_em(name, force = false) ⇒ Object
- #open_em_redis(db = 1) ⇒ Object
- #redis_em ⇒ Object
- #register_database(key) ⇒ Object
-
#semaphore ⇒ Object
private :open_em_redis.
-
#unlock! ⇒ Object
Release the lock.
Instance Attribute Details
#db_prefix ⇒ Object (readonly)
Returns the value of attribute db_prefix.
24 25 26 |
# File 'lib/joffice_redis/redis_manager.rb', line 24 def db_prefix @db_prefix end |
Instance Method Details
#config ⇒ Object
81 82 83 |
# File 'lib/joffice_redis/redis_manager.rb', line 81 def config @config||={} end |
#config=(v) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/joffice_redis/redis_manager.rb', line 65 def config=(v) v.symbolize_keys! @config=v @db_prefix=(config[:prefix] || 'test') p "DB_PREFIX: #{db_prefix}" end |
#error_callback ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/joffice_redis/redis_manager.rb', line 26 def error_callback @error_callback||= lambda {|code| #$log.error("Redis"){"[RedisEM]Error code: #{code}"} puts "[RedisEM]Error code: #{code}"; #JOffice::LogOutputter.flush; #EM.next_tick{EM.stop} } end |
#flush_all ⇒ Object
35 36 37 |
# File 'lib/joffice_redis/redis_manager.rb', line 35 def flush_all open_redis.flush_all end |
#lock! ⇒ Object
Lock the object so no other instances can modify it. This method implements the design pattern for locks described at: code.google.com/p/redis/wiki/SetnxCommand
163 164 165 166 167 168 169 170 171 |
# File 'lib/joffice_redis/redis_manager.rb', line 163 def lock! db=open_em_redis until db.setnx('_lock', lock_timeout) next unless lock = db.get('_lock') sleep(2) and next unless lock_expired?(lock) break unless lock = db.getset('_lock', lock_timeout) break if lock_expired?(lock) end end |
#lock_expired?(lock) ⇒ Boolean
183 184 185 |
# File 'lib/joffice_redis/redis_manager.rb', line 183 def lock_expired? lock lock.to_f < Time.now.to_f end |
#lock_timeout ⇒ Object
179 180 181 |
# File 'lib/joffice_redis/redis_manager.rb', line 179 def lock_timeout Time.now.to_f + 5 end |
#logger ⇒ Object
89 90 91 |
# File 'lib/joffice_redis/redis_manager.rb', line 89 def logger @logger||=nil end |
#logger=(v) ⇒ Object
85 86 87 |
# File 'lib/joffice_redis/redis_manager.rb', line 85 def logger=(v) @logger=v end |
#open_db_em(name, force = false) ⇒ Object
187 188 189 190 |
# File 'lib/joffice_redis/redis_manager.rb', line 187 def open_db_em(name, force=false) key= :"db:#{db_prefix}_#{name}" open_em_redis(db_names[key] || register_database(key)) end |
#open_em_redis(db = 1) ⇒ Object
133 134 135 |
# File 'lib/joffice_redis/redis_manager.rb', line 133 def open_em_redis(db=1) redis_em[db] end |
#redis_em ⇒ Object
129 130 131 |
# File 'lib/joffice_redis/redis_manager.rb', line 129 def redis_em @@redis_em||=Hash.new { |hash, id| hash[id] = force_open_em_redis(id); }; end |
#register_database(key) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/joffice_redis/redis_manager.rb', line 142 def register_database(key) register_fiber_and_singleton_task("register_database(#{key}) ") do redis=open_em_redis id=redis.get(key) if (new_db=id.nil?) id=redis.incr(increment_key) while id<2 id=redis.incr(increment_key) end redis.set(key,id) end p "Open db #{key} id=#{id} #{new_db}" db_names[key]=id end end |
#semaphore ⇒ Object
private :open_em_redis
138 139 140 |
# File 'lib/joffice_redis/redis_manager.rb', line 138 def semaphore @semaphore||=Mutex.new end |
#unlock! ⇒ Object
Release the lock.
175 176 177 |
# File 'lib/joffice_redis/redis_manager.rb', line 175 def unlock! open_em_redis.del('_lock') end |