File: distributed_store.rb

package info (click to toggle)
ruby-redis-store 1.1.6-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 284 kB
  • sloc: ruby: 909; makefile: 4
file content (50 lines) | stat: -rw-r--r-- 1,090 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'redis/distributed'

class Redis
  class DistributedStore < Distributed
    @@timeout = 5
    attr_reader :ring

    def initialize(addresses, options = { })
      nodes = addresses.map do |address|
        ::Redis::Store.new _merge_options(address, options)
      end

      _extend_namespace options
      @ring = Redis::HashRing.new nodes
    end

    def nodes
      ring.nodes
    end

    def reconnect
      nodes.each {|node| node.reconnect }
    end

    def set(key, value, options = nil)
      node_for(key).set(key, value, options)
    end

    def get(key, options = nil)
      node_for(key).get(key, options)
    end

    def setnx(key, value, options = nil)
      node_for(key).setnx(key, value, options)
    end

    private
      def _extend_namespace(options)
        @namespace = options[:namespace]
        extend ::Redis::Store::Namespace if @namespace
      end

      def _merge_options(address, options)
        address.merge({
          :timeout => options[:timeout] || @@timeout, 
          :namespace => options[:namespace]
        })
      end
  end
end