Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2012 01:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4342355 to your computer and use it in GitHub Desktop.
Save anonymous/4342355 to your computer and use it in GitHub Desktop.
Shared Juno store Implement your own key value server in 50 lines!
require 'drb'
module Juno
class Shared < Base
def initialize(options = {}, &block)
options[:port] ||= 9000
@uri = options[:uri] || (options[:socket] ? "drbunix://#{options[:socket]}" :
"druby://#{options[:host]}:#{options[:port]}")
@builder = Builder.new(&block)
end
def key?(key, options = {})
with_adapter {|a| a.key?(key, options) }
end
def load(key, options = {})
with_adapter {|a| a.load(key, options) }
end
def store(key, value, options = {})
with_adapter {|a| a.store(key, value, options) }
end
def delete(key, options = {})
with_adapter {|a| a.delete(key, options) }
end
def clear(options = {})
with_adapter {|a| a.clear(options) }
self
end
def close
if @server
@adapter.close
@server.stop_service
@server = nil
end
@adapter = nil
end
private
def with_adapter
yield(@adapter ||= DRb::DRbObject.new(nil, @uri))
rescue DRb::DRbConnError
@adapter = Lock.new(@builder.build.last)
@server = DRb::DRbServer.new(@uri, @adapter)
retry
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment