File: distributed_store.rb

package info (click to toggle)
ruby-redis-store 1.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312 kB
  • sloc: ruby: 1,480; makefile: 6
file content (68 lines) | stat: -rw-r--r-- 1,613 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'redis/distributed'

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

    def initialize(addresses, options = {})
      _extend_namespace options
      # `@tag` introduced in `redis-rb` 5.0
      @tag = options[:tag] || /^\{(.+?)\}/
      @ring = options[:ring] || Redis::HashRing.new([], options[:replicas] || Redis::HashRing::POINTS_PER_SERVER)

      addresses.each do |address|
        @ring.add_node(::Redis::Store.new _merge_options(address, options))
      end
    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

    def redis_version
      nodes.first.redis_version unless nodes.empty?
    end

    def supports_redis_version?(version)
      if nodes.empty?
        false
      else
        nodes.first.supports_redis_version?(version)
      end
    end

    def setex(key, expiry, value, options = nil)
      node_for(key).setex(key, expiry, 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