Class: RedisWrapper::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_wrapper/entry.rb

Constant Summary collapse

FLAG_MARSHALED =
"0x1"
FLAG_COMPRESSED =
"0x2"
DEFAULT_COMPRESS_LIMIT =

16 kilobytes

16 * 1024

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, options = {}) ⇒ Entry

Returns a new instance of Entry.



12
13
14
15
16
17
# File 'lib/redis_wrapper/entry.rb', line 12

def initialize(value, options = {})
  @flags = options[:flags] || {}
  @value = value
  @options = options
  flag_extraction if @value.class == String
end

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



3
4
5
# File 'lib/redis_wrapper/entry.rb', line 3

def flags
  @flags
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/redis_wrapper/entry.rb', line 3

def value
  @value
end

Class Method Details

.option_keysObject



8
9
10
# File 'lib/redis_wrapper/entry.rb', line 8

def self.option_keys
  [:expire_in,:flags, :raw]
end

Instance Method Details

#compress(val) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/redis_wrapper/entry.rb', line 54

def compress(val)
  if should_compress?(val) && !compressed?
    @flags[:compressed] = true
    return Zlib::Deflate.deflate(val)
  end
  val
end

#decompress(val) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/redis_wrapper/entry.rb', line 46

def decompress(val)
  if compressed?
    @flags[:compressed] = false
    return Zlib::Inflate.inflate(val)
  end
  val
end

#demarshal(val) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/redis_wrapper/entry.rb', line 38

def demarshal(val)
  if marshaled?
    @flags[:marshaled] = false
    return Marshal.load(val)
  end
  val
end

#extracted_resultObject



19
20
21
# File 'lib/redis_wrapper/entry.rb', line 19

def extracted_result
  @value = demarshal(decompress(@value))
end

#flag_extractionObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/redis_wrapper/entry.rb', line 69

def flag_extraction
  if @value
    pflag = @value.slice(@value.length-3..@value.length)
    if pflag == FLAG_MARSHALED
      @flags[:marshaled] = true
      @value = @value.slice(0..-4)
      flag_extraction
    elsif pflag == FLAG_COMPRESSED
      @flags[:compressed] = true
      @value = @value.slice(0..-4)
      flag_extraction
    end
  end
end

#flags_stringObject



31
32
33
34
35
36
# File 'lib/redis_wrapper/entry.rb', line 31

def flags_string
  string = ""
  string += FLAG_MARSHALED if @flags[:marshaled]
  string += FLAG_COMPRESSED if @flags[:compressed]
  string
end

#marshal(val) ⇒ Object



62
63
64
65
66
67
# File 'lib/redis_wrapper/entry.rb', line 62

def marshal(val)
  if val && !marshaled?
    @flags[:marshaled] = true
    Marshal.dump(val)
  end
end

#prepare(value) ⇒ Object



27
28
29
# File 'lib/redis_wrapper/entry.rb', line 27

def prepare(value)
  compress(marshal(value)) if value
end

#prepared_resultObject



23
24
25
# File 'lib/redis_wrapper/entry.rb', line 23

def prepared_result
  prepare(@value) + flags_string
end