Permalink
Browse files

Now with marshal where and when appropriate.

  • Loading branch information...
Taylor Weibley
Taylor Weibley committed Mar 14, 2009
1 parent 16b7a34 commit e9034d9807cf0950ae838a1ac567481c453bf56b
Showing with 27 additions and 8 deletions.
  1. +27 −8 lib/redis.rb
View
@@ -33,7 +33,7 @@ def initialize(opts={})
#
# Return value: status code reply
def []=(key, val)
- val = Marshal.dump(val)
+ val = redis_marshal(val)
timeout_retry(10, 3){
write "SET #{key} #{val.size}\r\n#{val}\r\n"
status_code_reply
@@ -50,7 +50,7 @@ def []=(key, val)
#
# 1 if the key was set 0 if the key was not set
def set_unless_exists(key, val)
- val = Marshal.dump(val)
+ val = redis_marshal(val)
timeout_retry(10, 3){
write "SETNX #{key} #{val.size}\r\n#{val}\r\n"
integer_reply == 1
@@ -67,7 +67,7 @@ def set_unless_exists(key, val)
def [](key)
timeout_retry(10, 3){
write "GET #{key}\r\n"
- Marshal.load(bulk_reply)
+ redis_unmarshal(bulk_reply)
}
end
@@ -231,8 +231,9 @@ def type?(key)
#
# Return value: status code reply
def push_tail(key, string)
+ string = redis_marshal(string)
timeout_retry(10, 3){
- write "RPUSH #{key} #{string.to_s.size}\r\n#{string}\r\n"
+ write "RPUSH #{key} #{string.size}\r\n#{string.to_s}\r\n"
status_code_reply
}
end
@@ -245,8 +246,9 @@ def push_tail(key, string)
#
# Return value: status code reply
def push_head(key, string)
+ string = redis_marshal(string)
timeout_retry(10, 3){
- write "LPUSH #{key} #{string.to_s.size}\r\n#{string}\r\n"
+ write "LPUSH #{key} #{string.size}\r\n#{string.to_s}\r\n"
status_code_reply
}
end
@@ -265,7 +267,7 @@ def push_head(key, string)
def pop_head(key)
timeout_retry(10, 3){
write "LPOP #{key}\r\n"
- bulk_reply
+ redis_unmarshal(bulk_reply)
}
end
@@ -275,7 +277,7 @@ def pop_head(key)
def pop_tail(key)
timeout_retry(10, 3){
write "RPOP #{key}\r\n"
- bulk_reply
+ redis_unmarshal(bulk_reply)
}
end
@@ -285,8 +287,9 @@ def pop_tail(key)
#
# Return value: status code reply
def list_set(key, index, val)
+ val = redis_marshal(val)
timeout_retry(10, 3){
- write "LSET #{key} #{index} #{val.to_s.size}\r\n#{val}\r\n"
+ write "LSET #{key} #{index} #{val.size}\r\n#{val}\r\n"
status_code_reply
}
end
@@ -624,6 +627,22 @@ def quit
private
+ def redis_unmarshal(obj)
+ if obj[0] == 4
+ Marshal.load(obj)
+ else
+ obj
+ end
+ end
+
+ def redis_marshal(obj)
+ unless obj.class == String
+ Marshal.dump(obj)
+ else
+ obj
+ end
+ end
+
def close
socket.close unless socket.closed?
end

0 comments on commit e9034d9

Please sign in to comment.