Module: MeRedis::ZipValues

Defined in:
lib/me_redis/zip_values.rb

Overview

todo warn in development when gzipped size iz bigger than strict use prepend for classes

Defined Under Namespace

Modules: EmptyCompressor, FutureUnzip, ZlibCompressor

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/me_redis/zip_values.rb', line 40

def self.prepended(base)
  base::Future.prepend(FutureUnzip)

  base.extend(MeRedis::ClassMethods)

  base.me_config.default_compressor = ::MeRedis::ZipValues::ZlibCompressor
end

Instance Method Details

#_patch_futures(client) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/me_redis/zip_values.rb', line 62

def _patch_futures(client)
  client.futures.each do |ftr|

    ftr.set_transformation do |vl|
      if vl && FutureUnzip::COMMANDS[ftr._command[0]]
        # we only dealing here with GET methods, so it could be hash getters or get/mget
        keys = ftr._command[0][0] == 'h' ? ftr._command[1, 1] : ftr._command[1..-1]
        if ftr._command[0] == :mget
          vl.each_with_index.map{ |v, i| zip?(keys[i]) ? self.class.get_compressor_for_key(keys[i]).decompress( v ) : v }
        elsif zip?(keys[0])
          compressor = self.class.get_compressor_for_key(keys[0])
          # on hash commands it could be an array
          vl.is_a?(Array) ? vl.map!{ |v| compressor.decompress(v) } : compressor.decompress(vl)
        else
          vl
        end
      else
        vl
      end
    end

  end
end

#get(key) ⇒ Object

Redis prepended methods



100
# File 'lib/me_redis/zip_values.rb', line 100

def get( key ); unzip_value( super( key ), key) end

#getset(key, value) ⇒ Object



106
# File 'lib/me_redis/zip_values.rb', line 106

def getset( key, value ); unzip_value( super( key, zip_value(value, key) ), key ) end

#hget(key, h_key) ⇒ Object



108
# File 'lib/me_redis/zip_values.rb', line 108

def hget( key, h_key ); unzip_value( super( key, h_key ), key ) end

#hmget(key, *args) ⇒ Object



114
# File 'lib/me_redis/zip_values.rb', line 114

def hmget( key, *args ); unzip_arr_or_future( super(key, *args), key ) end

#hmset(key, *args) ⇒ Object



112
# File 'lib/me_redis/zip_values.rb', line 112

def hmset( key, *args ); super( key, map_hmsets_arr(key, *args) ) end

#hset(key, h_key, value) ⇒ Object



109
# File 'lib/me_redis/zip_values.rb', line 109

def hset( key, h_key, value );  super( key, h_key, zip_value(value, key) ) end

#hsetnx(key, h_key, value) ⇒ Object



110
# File 'lib/me_redis/zip_values.rb', line 110

def hsetnx( key, h_key, value ); super( key, h_key, zip_value(value, key) ) end

#mget(*args) ⇒ Object



103
# File 'lib/me_redis/zip_values.rb', line 103

def mget(*args); unzip_arr_or_future(super(*args), args ) end

#mset(*args) ⇒ Object



104
# File 'lib/me_redis/zip_values.rb', line 104

def mset(*args); super( *map_msets_arr(args) ) end

#multi(&block) ⇒ Object



55
56
57
58
59
60
# File 'lib/me_redis/zip_values.rb', line 55

def multi(&block)
  super do |redis|
    block.call(redis)
    _patch_futures(@client)
  end
end

#pipelined(&block) ⇒ Object



48
49
50
51
52
53
# File 'lib/me_redis/zip_values.rb', line 48

def pipelined(&block)
  super do |redis|
    block.call(redis)
    _patch_futures(@client)
  end
end

#set(key, value, options = {}) ⇒ Object



101
# File 'lib/me_redis/zip_values.rb', line 101

def set( key, value, options = {} ); super( key, zip_value(value, key), options ) end

#unzip_value(value, key) ⇒ Object



91
92
93
94
95
# File 'lib/me_redis/zip_values.rb', line 91

def unzip_value(value, key)
  return value if value.is_a?( FutureUnzip )

  value.is_a?(String) && zip?(key) ? self.class.get_compressor_for_key(key).decompress( value ) : value
end

#zip?(key) ⇒ Boolean

Returns:

  • (Boolean)


97
# File 'lib/me_redis/zip_values.rb', line 97

def zip?(key); self.class.zip?(key) end

#zip_value(value, key) ⇒ Object



87
88
89
# File 'lib/me_redis/zip_values.rb', line 87

def zip_value(value, key )
  zip?(key) ? self.class.get_compressor_for_key(key).compress( value ) : value
end