Replace couchbase counters with drb version
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
# This is a non thread safe replacement for the
|
||||
# couchbase counter mechanism since every test needs
|
||||
# a clean start and Hash#clear is soooo much faster than
|
||||
# a couchbase bucket flush
|
||||
class InMemoryQCounter
|
||||
attr_reader :store
|
||||
|
||||
def initialize
|
||||
@store = {}
|
||||
end
|
||||
|
||||
def get(key, options = {})
|
||||
store[key]
|
||||
end
|
||||
|
||||
def set(key, value)
|
||||
store[key] = value
|
||||
end
|
||||
|
||||
def incr(key, options = {})
|
||||
# store[key] ||= options[:initial].to_i
|
||||
# store[key] += 1
|
||||
if store[key]
|
||||
store[key] += 1
|
||||
else
|
||||
store[key] = options[:initial].to_i
|
||||
end
|
||||
end
|
||||
|
||||
def decr(key, options = {})
|
||||
# store[key] ||= options[:initial].to_i
|
||||
# store[key] -= 1
|
||||
if store[key]
|
||||
store[key] -= 1
|
||||
else
|
||||
store[key] = options[:initial].to_i
|
||||
end
|
||||
end
|
||||
|
||||
def flush
|
||||
store.clear
|
||||
end
|
||||
end
|
||||
+1
-1
@@ -7,6 +7,7 @@ module Qwaiter
|
||||
autoload :Counter
|
||||
autoload :Broadcaster
|
||||
autoload :Couchbase
|
||||
autoload :DrbCounter
|
||||
|
||||
def self.broadcast_user(uid, event, data)
|
||||
message = {channel: "/user/#{uid}", data: {event: event, data: data}}
|
||||
@@ -18,4 +19,3 @@ module Qwaiter
|
||||
broadcaster.broadcast message
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -5,13 +5,20 @@ module Qwaiter
|
||||
end
|
||||
|
||||
def self.load_design_docs!
|
||||
return unless connection.present?
|
||||
Dir.glob(Rails.root.join('config/couchbase/design_docs', "*.json")).each do |design_doc|
|
||||
connection.save_design_doc File.open(design_doc)
|
||||
end
|
||||
end
|
||||
|
||||
def self.design_doc(name)
|
||||
return unless connection.present?
|
||||
connection.design_docs[name]
|
||||
end
|
||||
|
||||
def self.flush_counters!
|
||||
return unless connection.present?
|
||||
design_doc('supplier').counters(reduce: false).each{|counter| Qwaiter::Counter.set counter.key, 0}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -22,8 +22,3 @@ module Qwaiter
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# use the connection from couchbase-structures/documents
|
||||
# will be overwritten in the specs since flushing the real
|
||||
# thing is difficult
|
||||
Qwaiter::Counter.connection = $cb unless Rails.env.test?
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
module Qwaiter
|
||||
module DrbCounter
|
||||
def self.object
|
||||
require 'drb'
|
||||
DRbObject.new_with_uri('druby://localhost:9022')
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user