Created
June 22, 2013 04:04
-
-
Save akishin/5835839 to your computer and use it in GitHub Desktop.
redis-rb auto compression client
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
# -*- coding: utf-8 -*- | |
require 'json' | |
require 'zlib' | |
class Redis | |
class Client | |
def call(command, &block) | |
if write?(command.first) | |
command[command.length - 1] = encode(command.last) | |
end | |
reply = process([command]) { read } | |
raise reply if reply.is_a?(CommandError) | |
if read?(command.first) && !reply.nil? | |
reply = decode(reply) | |
end | |
if block | |
block.call(reply) | |
else | |
reply | |
end | |
end | |
def write?(command) | |
case command | |
when :set, :setex, | |
:setnx, :psetex then | |
true | |
else | |
false | |
end | |
end | |
def read?(command) | |
case command | |
when :get then | |
true | |
else | |
false | |
end | |
end | |
def encode(data) | |
Zlib::Deflate.deflate(data) | |
end | |
def decode(data) | |
Zlib::Inflate.inflate(data).force_encoding("UTF-8") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment