Class: Memodis::DistCache
- Inherits:
-
Object
- Object
- Memodis::DistCache
- Defined in:
- lib/memodis/dist_cache.rb
Constant Summary collapse
- CODERS =
{}
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
-
#initialize(options) ⇒ DistCache
constructor
A new instance of DistCache.
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() @master = DistRedis.new({ :db => [:db], :hosts => [:master], :timeout => [:timeout], }) @slaves = [:slaves].map do |h| host, port = h.split(':') Redis.new({ :db => [:db], :host => host, :port => port, :timeout => [:timeout], }) end @encoder = resolve_coder([:encoder]) @decoder = resolve_coder([:decoder]) @key_gen = .fetch(:key_gen, lambda { |k| k }) @expires = [: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 |