Class: Memodis::DistCache

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

Constant Summary collapse

CODERS =
{}

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DistCache

Returns a new instance of DistCache.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/memodis/dist_cache.rb', line 12

def initialize(options)
  @master = DistRedis.new({
    :db => options[:db],
    :hosts => options[:master],
    :timeout => options[:timeout],
  })
  @slaves = options[:slaves].map do |h|
    host, port = h.split(':')
    Redis.new({
      :db => options[:db],
      :host => host,
      :port => port,
      :timeout => options[:timeout],
    })
  end
  @encoder = resolve_coder(options[:encoder])
  @decoder = resolve_coder(options[:decoder])
  @key_gen = options.fetch(:key_gen, lambda { |k| k })
  @expires = options[:expires]
end

Instance Method Details

#[](key) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/memodis/dist_cache.rb', line 39

def [] key
  key = canonicalize(key)
  if val = get(key)
    decode(val)
  else
    nil # don't decode a miss
  end
end

#[]=(key, val) ⇒ Object



33
34
35
36
37
# File 'lib/memodis/dist_cache.rb', line 33

def []= key, val
  key = canonicalize(key)
  @master.set(key, encode(val))
  @master.expire(key, @expires) unless @expires.nil?
end