Skip to content

Instantly share code, notes, and snippets.

@akishin
Created June 22, 2013 04:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akishin/5835839 to your computer and use it in GitHub Desktop.
Save akishin/5835839 to your computer and use it in GitHub Desktop.
redis-rb auto compression client
# -*- 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