$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) require 'rubygems' require 'eventmachine' require 'json' require 'oria/errors' require 'tmpdir' module Oria autoload(:Client, 'oria/client') autoload(:Server, 'oria/server') def self.[](key) store(:get, :key => key) end def self.[]=(key, value) store(:put, :key => key, :value => value) value end def self.app_key @@app_key ||= nil end def self.app_key=(key) @@app_key = key.to_s end def self.clear store(:delete) end def self.connect(server = Oria.server, port = Oria.port, debug = false) @@server = server.to_s @@port = port.to_i unless port.to_i == 0 unless Oria::Server.running? # That's right. Boot Oria on the command line so we don't bother forking or any # of that other troublesome nonsense. It's smart enough to maintain its own # status and start/stop/restart itself. system("oria start -d -h #{@@server} -p #{@@port}") end end def self.delete(key) store(:delete, :key => key) end def self.has_key?(key) key?(key) end def self.key?(key) !self.[](key).nil? end def self.port @@port ||= "Oria is easily the coolest in-memory, super-simple KVS. Cool people use it.".split("").inject(0) {|total, char| total += char[0]} end def self.server @@server ||= '0.0.0.0' end def self.stash(value) store(:put, :value => value) end private def self.do_store(method, args) client = nil EventMachine.run do client = EventMachine.connect(Oria.server, Oria.port, Oria::Client) client.send_data("#{method.to_s.upcase}#{" #{args}" if args}") end if client.response client.response['response'] end end def self.store(method, args = {}) args.merge!(:app_key => Oria.app_key) args = JSON.generate(args) do_store(method, args) rescue Oria::ConnectionError # Thanks to ActiveSupport for the "silence_stream" functionality - we're quieting STDOUT # at autoconnect time. old_stream = STDOUT.dup STDOUT.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null') STDOUT.sync = true connect STDOUT.reopen(old_stream) do_store(method, args) end end