Instantly share code, notes, and snippets.
Created
December 20, 2012 01:51
-
Star
2
(2)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save anonymous/4342355 to your computer and use it in GitHub Desktop.
Shared Juno store Implement your own key value server in 50 lines!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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