This repository has been archived by the owner on Dec 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It makes a new Redis object. But does it actually work?
- Loading branch information
Showing
9 changed files
with
172 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
require "bundler/gem_tasks" | ||
Dir["tasks/**/*.rake"].each { |task| load task } | ||
|
||
task :default => :spec |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
require "celluloid/redis/version" | ||
require "redis" | ||
|
||
module Celluloid | ||
module Redis | ||
# Your code goes here... | ||
end | ||
end | ||
require "celluloid/redis/version" | ||
require "celluloid/redis/redis_ext" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Redis | ||
class Client | ||
# Well this is really sad. redis-rb does not provide extensible driver | ||
# support. Instead they couple everything together though this method. | ||
# This leaves us no choice but to monkeypatch | ||
def _parse_driver(driver) | ||
driver = driver.to_s if driver.is_a?(Symbol) | ||
|
||
if driver.kind_of?(String) | ||
case driver | ||
when "ruby" | ||
require "redis/connection/ruby" | ||
driver = Connection::Ruby | ||
when "celluloid" | ||
require "redis/connection/celluloid" | ||
driver = Connection::Celluloid | ||
when "hiredis" | ||
require "redis/connection/hiredis" | ||
driver = Connection::Hiredis | ||
else | ||
raise "Unknown driver: #{driver}" | ||
end | ||
end | ||
|
||
driver | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
require "redis/connection/registry" | ||
require "redis/connection/command_helper" | ||
require "redis/errors" | ||
require "celluloid/io" | ||
|
||
class Redis | ||
module Connection | ||
class Celluloid | ||
include Redis::Connection::CommandHelper | ||
|
||
MINUS = "-".freeze | ||
PLUS = "+".freeze | ||
COLON = ":".freeze | ||
DOLLAR = "$".freeze | ||
ASTERISK = "*".freeze | ||
|
||
def self.connect(config) | ||
if config[:scheme] == "unix" | ||
sock = ::Celluloid::IO::UNIXSocket.connect(config[:path], config[:timeout]) | ||
else | ||
sock = ::Celluloid::IO::TCPSocket.connect(config[:host], config[:port], config[:timeout]) | ||
end | ||
|
||
new(sock) | ||
end | ||
|
||
def initialize(sock) | ||
@sock = sock | ||
end | ||
|
||
def connected? | ||
!!@sock | ||
end | ||
|
||
def disconnect | ||
@sock.close rescue nil | ||
ensure | ||
@sock = nil | ||
end | ||
|
||
def timeout=(timeout) | ||
if @sock.respond_to?(:timeout=) | ||
@sock.timeout = timeout | ||
end | ||
end | ||
|
||
def write(command) | ||
@sock.write(build_command(command)) | ||
end | ||
|
||
def read | ||
line = @sock.gets | ||
reply_type = line.slice!(0, 1) | ||
format_reply(reply_type, line) | ||
|
||
rescue Errno::EAGAIN | ||
raise TimeoutError | ||
end | ||
|
||
def format_reply(reply_type, line) | ||
case reply_type | ||
when MINUS then format_error_reply(line) | ||
when PLUS then format_status_reply(line) | ||
when COLON then format_integer_reply(line) | ||
when DOLLAR then format_bulk_reply(line) | ||
when ASTERISK then format_multi_bulk_reply(line) | ||
else raise ProtocolError.new(reply_type) | ||
end | ||
end | ||
|
||
def format_error_reply(line) | ||
CommandError.new(line.strip) | ||
end | ||
|
||
def format_status_reply(line) | ||
line.strip | ||
end | ||
|
||
def format_integer_reply(line) | ||
line.to_i | ||
end | ||
|
||
def format_bulk_reply(line) | ||
bulklen = line.to_i | ||
return if bulklen == -1 | ||
reply = encode(@sock.read(bulklen)) | ||
@sock.read(2) # Discard CRLF. | ||
reply | ||
end | ||
|
||
def format_multi_bulk_reply(line) | ||
n = line.to_i | ||
return if n == -1 | ||
|
||
Array.new(n) { read } | ||
end | ||
end | ||
end | ||
end | ||
|
||
Redis::Connection.drivers << Redis::Connection::Celluloid |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require "spec_helper" | ||
|
||
describe Redis::Connection::Celluloid do | ||
it "connects to Redis" do | ||
connection = Redis.new(:driver => :celluloid) | ||
connection.should be_a Redis | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require "celluloid/redis" | ||
require "redis/connection/celluloid" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new | ||
|
||
RSpec::Core::RakeTask.new(:rcov) do |task| | ||
task.rcov = true | ||
end |