Permalink
Browse files

add gzip compressor

  • Loading branch information...
1 parent e778936 commit 98a123874b4750e6aaa26f4b49a11dd19f39e11d @tkellen tkellen committed Dec 10, 2012
Showing with 29 additions and 0 deletions.
  1. +16 −0 lib/dalli/compressor.rb
  2. +13 −0 test/test_compressor.rb
View
@@ -1,4 +1,5 @@
require 'zlib'
+require 'stringio'
module Dalli
class Compressor
@@ -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
View
@@ -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|
+ 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

0 comments on commit 98a1238

Please sign in to comment.