Module: Lark
Defined Under Namespace
Classes: Base
Instance Method Summary collapse
- #clean(group) ⇒ Object
- #configure(options) ⇒ Object
- #dbs ⇒ Object
- #destroy(id, group) ⇒ Object
- #domain ⇒ Object
- #get(group) ⇒ Object
- #get_all(group) ⇒ Object
- #index(group) ⇒ Object
- #index_search(group) ⇒ Object
- #key(id, group) ⇒ Object
- #load_data(id, group) ⇒ Object
- #on_expired(&blk) ⇒ Object
-
#pool(&blk) ⇒ Object
all the black magic happens here pool runs the block against all the db’s throws an error if non of them are online and returns an array of the block results from the ones who are online.
- #save_data(id, group, data) ⇒ Object
- #urls ⇒ Object
Instance Method Details
#clean(group) ⇒ Object
93 94 95 |
# File 'lib/lark.rb', line 93 def clean(group) index_search(group).reject { |i| load_data(i, group) }.each { |i| destroy(i, group) } end |
#configure(options) ⇒ Object
10 11 12 13 14 |
# File 'lib/lark.rb', line 10 def configure() @domain = [:domain] @expire = [:expire] @urls = [:urls] end |
#dbs ⇒ Object
34 35 36 37 38 39 |
# File 'lib/lark.rb', line 34 def dbs @dbs ||= urls.map do |url| puts "Connecting to #{url}"; Redis.connect(:url => url) end end |
#destroy(id, group) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/lark.rb', line 83 def destroy(id, group) pool do |db| db.multi do db.del key(id, group) db.srem index(group), id end end @expired.call(id, group) if @expired end |
#domain ⇒ Object
16 17 18 |
# File 'lib/lark.rb', line 16 def domain @domain.to_s end |
#get(group) ⇒ Object
124 125 126 |
# File 'lib/lark.rb', line 124 def get(group) get_all(group).select { |i| i } end |
#get_all(group) ⇒ Object
97 98 99 |
# File 'lib/lark.rb', line 97 def get_all(group) index_search(group).map { |i| load_data(i, group) } end |
#index(group) ⇒ Object
41 42 43 |
# File 'lib/lark.rb', line 41 def index(group) "#{domain}:#{group}:idx" end |
#index_search(group) ⇒ Object
75 76 77 |
# File 'lib/lark.rb', line 75 def index_search(group) pool { |db| db.smembers(index(group)) }.flatten.uniq.sort end |
#key(id, group) ⇒ Object
79 80 81 |
# File 'lib/lark.rb', line 79 def key(id, group) "#{domain}:#{group}:key:#{id}" end |
#load_data(id, group) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/lark.rb', line 101 def load_data(id, group) key = key(id, group) ## try each db - return when one works without an error ## throw an error if all are down pool do |db| data = db.hgetall(key) return data if not data.empty? end nil end |
#on_expired(&blk) ⇒ Object
71 72 73 |
# File 'lib/lark.rb', line 71 def on_expired(&blk) @expired = blk; end |
#pool(&blk) ⇒ Object
all the black magic happens here pool runs the block against all the db’s throws an error if non of them are online and returns an array of the block results from the ones who are online
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/lark.rb', line 51 def pool(&blk) dbs.push dbs.shift num_errors = 0 results = [] begin dbs.each do |db| begin results << blk.call(db) rescue Errno::ECONNREFUSED, Timeout::Error => e puts "#{e.class}: #{e.}" num_errors += 1 end end ensure raise LarkDBOffline if num_errors == dbs.size end results end |
#save_data(id, group, data) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/lark.rb', line 112 def save_data(id, group, data) key = key(id,group) pool do |db| db.multi do db.hmset *[ key, data.to_a ].flatten db.expire key, @expire if @expire db.sadd index(group), id end end end |
#urls ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lark.rb', line 20 def urls if @urls @urls elsif @url [ @url ] elsif ENV['LARK_URLS'] ENV['LARK_URLS'].split(",") elsif ENV['LARK_URL'] [ ENV['LARK_URL'] ] else [ "redis://127.0.0.1:6379/0" ] end end |