Class: Redreloader

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.redisObject

Returns the value of attribute redis.



7
8
9
# File 'lib/redreloader.rb', line 7

def redis
  @redis
end

.ttlObject

Returns the value of attribute ttl.



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

def ttl
  @ttl
end

Class Method Details

.[](key) ⇒ Object



10
11
12
13
# File 'lib/redreloader.rb', line 10

def [](key)
  val = with_binary_redis { @redis.getrange(key, 16, -1) }
  Zlib::Inflate.inflate(val) if val && val != ""
end

.[]=(key, val) ⇒ Object



15
16
17
# File 'lib/redreloader.rb', line 15

def []=(key, val)
  with_binary_redis { @redis.set(key, calcmd5(val) + Zlib::Deflate.deflate(val), ex: @ttl) }
end

.calcmd5(val) ⇒ Object



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

def calcmd5(val)
  Digest::MD5.digest val
end

.digest(key) ⇒ Object



23
24
25
26
# File 'lib/redreloader.rb', line 23

def digest(key)
  data = with_binary_redis { @redis.getrange(key, 0, 15) }
  data if data != ""
end

.if_changed_from(key, old_digest) ⇒ Object



28
29
30
31
32
33
# File 'lib/redreloader.rb', line 28

def if_changed_from(key, old_digest)
  if (new_digest = digest(key)) && new_digest != old_digest
    newdata = self[key]
    yield(newdata, new_digest) if newdata
  end
end

.with_binary_redisObject



35
36
37
38
39
40
41
# File 'lib/redreloader.rb', line 35

def with_binary_redis
  original_encoding = Encoding.default_external
  Encoding.default_external = Encoding.find('binary')
  yield
ensure
  Encoding.default_external = original_encoding
end