Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gzip compressor #303

Merged
merged 1 commit into from
Dec 11, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/dalli/compressor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'zlib'
require 'stringio'

module Dalli
class Compressor
Expand All @@ -10,4 +11,19 @@ def self.decompress(data)
Zlib::Inflate.inflate(data)
end
end

class GzipCompressor
def self.compress(data)
io = StringIO.new("w")
gz = Zlib::GzipWriter.new(io)
gz.write(data)
gz.close
io.string
end

def self.decompress(data)
io = StringIO.new(data, "rb")
Zlib::GzipReader.new(io).read
end
end
end
13 changes: 13 additions & 0 deletions test/test_compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,16 @@ def self.decompress(data)
end
end
end

describe 'GzipCompressor' do

should 'compress and uncompress data using Zlib::GzipWriter/Reader' do
memcached(19127,nil,{:compress=>true,:compressor=>Dalli::GzipCompressor}) do |dc|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change :compressor to :compessor and watch the spec still pass. You need to set some expectations to verify your methods are being called.

data = (0...1025).map{65.+(rand(26)).chr}.join
assert dc.set("test", data)
assert_equal Dalli::GzipCompressor, dc.instance_variable_get('@ring').servers.first.compressor
assert_equal(data, dc.get("test"))
end
end

end