Class: Redis::Distributed

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

Defined Under Namespace

Classes: CannotDistribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(urls, options = {}) ⇒ Distributed

Returns a new instance of Distributed.



18
19
20
21
22
23
# File 'lib/redis/distributed.rb', line 18

def initialize(urls, options = {})
  @tag = options.delete(:tag) || /^\{(.+?)\}/
  @default_options = options
  @ring = HashRing.new urls.map { |url| Redis.connect(options.merge(:url => url)) }
  @subscribed_node = nil
end

Instance Attribute Details

#ringObject (readonly)

Returns the value of attribute ring.



16
17
18
# File 'lib/redis/distributed.rb', line 16

def ring
  @ring
end

Instance Method Details

#[](key) ⇒ Object



125
126
127
# File 'lib/redis/distributed.rb', line 125

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



137
138
139
# File 'lib/redis/distributed.rb', line 137

def []=(key,value)
  set(key, value)
end

#add_node(url) ⇒ Object



33
34
35
# File 'lib/redis/distributed.rb', line 33

def add_node(url)
  @ring.add_node Redis.connect(@default_options.merge(:url => url))
end

#append(key, value) ⇒ Object



129
130
131
# File 'lib/redis/distributed.rb', line 129

def append(key, value)
  node_for(key).append(key, value)
end

#bgsaveObject



474
475
476
# File 'lib/redis/distributed.rb', line 474

def bgsave
  on_each_node :bgsave
end

#blpop(key, timeout) ⇒ Object



231
232
233
# File 'lib/redis/distributed.rb', line 231

def blpop(key, timeout)
  node_for(key).blpop(key, timeout)
end

#brpop(key, timeout) ⇒ Object



235
236
237
# File 'lib/redis/distributed.rb', line 235

def brpop(key, timeout)
  node_for(key).brpop(key, timeout)
end

#dbsizeObject



85
86
87
# File 'lib/redis/distributed.rb', line 85

def dbsize
  on_each_node :dbsize
end

#decr(key) ⇒ Object



177
178
179
# File 'lib/redis/distributed.rb', line 177

def decr(key)
  node_for(key).decr(key)
end

#decrby(key, decrement) ⇒ Object



181
182
183
# File 'lib/redis/distributed.rb', line 181

def decrby(key, decrement)
  node_for(key).decrby(key, decrement)
end

#del(*keys) ⇒ Object



57
58
59
# File 'lib/redis/distributed.rb', line 57

def del(*keys)
  on_each_node(:del, *keys)
end

#discardObject

Raises:



433
434
435
# File 'lib/redis/distributed.rb', line 433

def discard
  raise CannotDistribute, :discard
end

#echo(value) ⇒ Object



490
491
492
# File 'lib/redis/distributed.rb', line 490

def echo(value)
  on_each_node :echo, value
end

#execObject

Raises:



429
430
431
# File 'lib/redis/distributed.rb', line 429

def exec
  raise CannotDistribute, :exec
end

#exists(key) ⇒ Object



53
54
55
# File 'lib/redis/distributed.rb', line 53

def exists(key)
  node_for(key).exists(key)
end

#expire(key, seconds) ⇒ Object



89
90
91
# File 'lib/redis/distributed.rb', line 89

def expire(key, seconds)
  node_for(key).expire(key, seconds)
end

#expireat(key, unix_time) ⇒ Object



93
94
95
# File 'lib/redis/distributed.rb', line 93

def expireat(key, unix_time)
  node_for(key).expireat(key, unix_time)
end

#flushallObject



49
50
51
# File 'lib/redis/distributed.rb', line 49

def flushall
  on_each_node :flushall
end

#flushdbObject



105
106
107
# File 'lib/redis/distributed.rb', line 105

def flushdb
  on_each_node :flushdb
end

#get(key) ⇒ Object



117
118
119
# File 'lib/redis/distributed.rb', line 117

def get(key)
  node_for(key).get(key)
end

#getset(key, value) ⇒ Object



121
122
123
# File 'lib/redis/distributed.rb', line 121

def getset(key, value)
  node_for(key).getset(key, value)
end

#hdel(key, field) ⇒ Object



377
378
379
# File 'lib/redis/distributed.rb', line 377

def hdel(key, field)
  node_for(key).hdel(key, field)
end

#hexists(key, field) ⇒ Object



381
382
383
# File 'lib/redis/distributed.rb', line 381

def hexists(key, field)
  node_for(key).hexists(key, field)
end

#hget(key, field) ⇒ Object



373
374
375
# File 'lib/redis/distributed.rb', line 373

def hget(key, field)
  node_for(key).hget(key, field)
end

#hgetall(key) ⇒ Object



397
398
399
# File 'lib/redis/distributed.rb', line 397

def hgetall(key)
  node_for(key).hgetall(key)
end

#hincrby(key, field, increment) ⇒ Object



405
406
407
# File 'lib/redis/distributed.rb', line 405

def hincrby(key, field, increment)
  node_for(key).hincrby(key, field, increment)
end

#hkeys(key) ⇒ Object



389
390
391
# File 'lib/redis/distributed.rb', line 389

def hkeys(key)
  node_for(key).hkeys(key)
end

#hlen(key) ⇒ Object



385
386
387
# File 'lib/redis/distributed.rb', line 385

def hlen(key)
  node_for(key).hlen(key)
end

#hmset(key, *attrs) ⇒ Object



401
402
403
# File 'lib/redis/distributed.rb', line 401

def hmset(key, *attrs)
  node_for(key).hmset(key, *attrs)
end

#hset(key, field, value) ⇒ Object



369
370
371
# File 'lib/redis/distributed.rb', line 369

def hset(key, field, value)
  node_for(key).hset(key, field, value)
end

#hvals(key) ⇒ Object



393
394
395
# File 'lib/redis/distributed.rb', line 393

def hvals(key)
  node_for(key).hvals(key)
end

#incr(key) ⇒ Object



169
170
171
# File 'lib/redis/distributed.rb', line 169

def incr(key)
  node_for(key).incr(key)
end

#incrby(key, increment) ⇒ Object



173
174
175
# File 'lib/redis/distributed.rb', line 173

def incrby(key, increment)
  node_for(key).incrby(key, increment)
end

#infoObject



482
483
484
# File 'lib/redis/distributed.rb', line 482

def info
  on_each_node :info
end

#keys(glob = "*") ⇒ Object



65
66
67
# File 'lib/redis/distributed.rb', line 65

def keys(glob = "*")
  on_each_node(:keys, glob).flatten
end

#lastsaveObject



478
479
480
# File 'lib/redis/distributed.rb', line 478

def lastsave
  on_each_node :lastsave
end

#lindex(key, index) ⇒ Object



205
206
207
# File 'lib/redis/distributed.rb', line 205

def lindex(key, index)
  node_for(key).lindex(key, index)
end

#llen(key) ⇒ Object



193
194
195
# File 'lib/redis/distributed.rb', line 193

def llen(key)
  node_for(key).llen(key)
end

#lpop(key) ⇒ Object



217
218
219
# File 'lib/redis/distributed.rb', line 217

def lpop(key)
  node_for(key).lpop(key)
end

#lpush(key, value) ⇒ Object



189
190
191
# File 'lib/redis/distributed.rb', line 189

def lpush(key, value)
  node_for(key).lpush(key, value)
end

#lrange(key, start, stop) ⇒ Object



197
198
199
# File 'lib/redis/distributed.rb', line 197

def lrange(key, start, stop)
  node_for(key).lrange(key, start, stop)
end

#lrem(key, count, value) ⇒ Object



213
214
215
# File 'lib/redis/distributed.rb', line 213

def lrem(key, count, value)
  node_for(key).lrem(key, count, value)
end

#lset(key, index, value) ⇒ Object



209
210
211
# File 'lib/redis/distributed.rb', line 209

def lset(key, index, value)
  node_for(key).lset(key, index, value)
end

#ltrim(key, start, stop) ⇒ Object



201
202
203
# File 'lib/redis/distributed.rb', line 201

def ltrim(key, start, stop)
  node_for(key).ltrim(key, start, stop)
end

#mapped_mget(*keys) ⇒ Object

Raises:



145
146
147
# File 'lib/redis/distributed.rb', line 145

def mapped_mget(*keys)
  raise CannotDistribute, :mapped_mget
end

#mapped_mset(hash) ⇒ Object



157
158
159
# File 'lib/redis/distributed.rb', line 157

def mapped_mset(hash)
  mset(*hash.to_a.flatten)
end

#mapped_msetnx(hash) ⇒ Object

Raises:



165
166
167
# File 'lib/redis/distributed.rb', line 165

def mapped_msetnx(hash)
  raise CannotDistribute, :mapped_msetnx
end

#mget(*keys) ⇒ Object

Raises:



141
142
143
# File 'lib/redis/distributed.rb', line 141

def mget(*keys)
  raise CannotDistribute, :mget
end

#monitorObject

Raises:

  • (NotImplementedError)


486
487
488
# File 'lib/redis/distributed.rb', line 486

def monitor
  raise NotImplementedError
end

#move(key, db) ⇒ Object



101
102
103
# File 'lib/redis/distributed.rb', line 101

def move(key, db)
  node_for(key).move(key, db)
end

#mset(*args) ⇒ Object

Raises:



153
154
155
# File 'lib/redis/distributed.rb', line 153

def mset(*args)
  raise CannotDistribute, :mset
end

#msetnx(*args) ⇒ Object

Raises:



161
162
163
# File 'lib/redis/distributed.rb', line 161

def msetnx(*args)
  raise CannotDistribute, :msetnx
end

#multi(&block) ⇒ Object

Raises:



417
418
419
# File 'lib/redis/distributed.rb', line 417

def multi(&block)
  raise CannotDistribute, :multi
end

#node_for(key) ⇒ Object



25
26
27
# File 'lib/redis/distributed.rb', line 25

def node_for(key)
  @ring.get_node(key_tag(key) || key)
end

#nodesObject



29
30
31
# File 'lib/redis/distributed.rb', line 29

def nodes
  @ring.nodes
end

#pingObject



45
46
47
# File 'lib/redis/distributed.rb', line 45

def ping
  on_each_node :ping
end

#pipelinedObject

Raises:



494
495
496
# File 'lib/redis/distributed.rb', line 494

def pipelined
  raise CannotDistribute, :pipelined
end

#psubscribe(*channels, &block) ⇒ Object

Raises:

  • (NotImplementedError)


466
467
468
# File 'lib/redis/distributed.rb', line 466

def psubscribe(*channels, &block)
  raise NotImplementedError
end

#publish(channel, message) ⇒ Object



437
438
439
# File 'lib/redis/distributed.rb', line 437

def publish(channel, message)
  node_for(channel).publish(channel, message)
end

#punsubscribe(*channels) ⇒ Object

Raises:

  • (NotImplementedError)


462
463
464
# File 'lib/redis/distributed.rb', line 462

def punsubscribe(*channels)
  raise NotImplementedError
end

#quitObject



37
38
39
# File 'lib/redis/distributed.rb', line 37

def quit
  on_each_node :quit
end

#randomkeyObject

Raises:



69
70
71
# File 'lib/redis/distributed.rb', line 69

def randomkey
  raise CannotDistribute, :randomkey
end

#rename(old_name, new_name) ⇒ Object



73
74
75
76
77
# File 'lib/redis/distributed.rb', line 73

def rename(old_name, new_name)
  ensure_same_node(:rename, old_name, new_name) do |node|
    node.rename(old_name, new_name)
  end
end

#renamenx(old_name, new_name) ⇒ Object



79
80
81
82
83
# File 'lib/redis/distributed.rb', line 79

def renamenx(old_name, new_name)
  ensure_same_node(:renamenx, old_name, new_name) do |node|
    node.renamenx(old_name, new_name)
  end
end

#rpop(key) ⇒ Object



221
222
223
# File 'lib/redis/distributed.rb', line 221

def rpop(key)
  node_for(key).rpop(key)
end

#rpoplpush(source, destination) ⇒ Object



225
226
227
228
229
# File 'lib/redis/distributed.rb', line 225

def rpoplpush(source, destination)
  ensure_same_node(:rpoplpush, source, destination) do |node|
    node.rpoplpush(source, destination)
  end
end

#rpush(key, value) ⇒ Object



185
186
187
# File 'lib/redis/distributed.rb', line 185

def rpush(key, value)
  node_for(key).rpush(key, value)
end

#sadd(key, value) ⇒ Object



239
240
241
# File 'lib/redis/distributed.rb', line 239

def sadd(key, value)
  node_for(key).sadd(key, value)
end

#saveObject



470
471
472
# File 'lib/redis/distributed.rb', line 470

def save
  on_each_node :save
end

#scard(key) ⇒ Object



257
258
259
# File 'lib/redis/distributed.rb', line 257

def scard(key)
  node_for(key).scard(key)
end

#sdiff(*keys) ⇒ Object



289
290
291
292
293
# File 'lib/redis/distributed.rb', line 289

def sdiff(*keys)
  ensure_same_node(:sdiff, *keys) do |node|
    node.sdiff(*keys)
  end
end

#sdiffstore(destination, *keys) ⇒ Object



295
296
297
298
299
# File 'lib/redis/distributed.rb', line 295

def sdiffstore(destination, *keys)
  ensure_same_node(:sdiffstore, destination, *keys) do |node|
    node.sdiffstore(destination, *keys)
  end
end

#select(db) ⇒ Object



41
42
43
# File 'lib/redis/distributed.rb', line 41

def select(db)
  on_each_node :select, db
end

#set(key, value) ⇒ Object



109
110
111
# File 'lib/redis/distributed.rb', line 109

def set(key, value)
  node_for(key).set(key, value)
end

#setex(key, ttl, value) ⇒ Object



113
114
115
# File 'lib/redis/distributed.rb', line 113

def setex(key, ttl, value)
  node_for(key).setex(key, ttl, value)
end

#setnx(key, value) ⇒ Object



149
150
151
# File 'lib/redis/distributed.rb', line 149

def setnx(key, value)
  node_for(key).setnx(key, value)
end

#sinter(*keys) ⇒ Object



265
266
267
268
269
# File 'lib/redis/distributed.rb', line 265

def sinter(*keys)
  ensure_same_node(:sinter, *keys) do |node|
    node.sinter(*keys)
  end
end

#sinterstore(destination, *keys) ⇒ Object



271
272
273
274
275
# File 'lib/redis/distributed.rb', line 271

def sinterstore(destination, *keys)
  ensure_same_node(:sinterstore, destination, *keys) do |node|
    node.sinterstore(destination, *keys)
  end
end

#sismember(key, member) ⇒ Object



261
262
263
# File 'lib/redis/distributed.rb', line 261

def sismember(key, member)
  node_for(key).sismember(key, member)
end

#smembers(key) ⇒ Object



301
302
303
# File 'lib/redis/distributed.rb', line 301

def smembers(key)
  node_for(key).smembers(key)
end

#smove(source, destination, member) ⇒ Object



251
252
253
254
255
# File 'lib/redis/distributed.rb', line 251

def smove(source, destination, member)
  ensure_same_node(:smove, source, destination) do |node|
    node.smove(source, destination, member)
  end
end

#sort(key, options = {}) ⇒ Object



409
410
411
412
413
414
415
# File 'lib/redis/distributed.rb', line 409

def sort(key, options = {})
  keys = [key, options[:by], options[:store], *Array(options[:get])].compact

  ensure_same_node(:sort, *keys) do |node|
    node.sort(key, options)
  end
end

#spop(key) ⇒ Object



247
248
249
# File 'lib/redis/distributed.rb', line 247

def spop(key)
  node_for(key).spop(key)
end

#srandmember(key) ⇒ Object



305
306
307
# File 'lib/redis/distributed.rb', line 305

def srandmember(key)
  node_for(key).srandmember(key)
end

#srem(key, value) ⇒ Object



243
244
245
# File 'lib/redis/distributed.rb', line 243

def srem(key, value)
  node_for(key).srem(key, value)
end

#subscribe(channel, *channels, &block) ⇒ Object



450
451
452
453
454
455
456
457
458
459
460
# File 'lib/redis/distributed.rb', line 450

def subscribe(channel, *channels, &block)
  if channels.empty?
    @subscribed_node = node_for(channel)
    @subscribed_node.subscribe(channel, &block)
  else
    ensure_same_node(:subscribe, channel, *channels) do |node|
      @subscribed_node = node
      node.subscribe(channel, *channels, &block)
    end
  end
end

#subscribed?Boolean

Returns:

  • (Boolean)


441
442
443
# File 'lib/redis/distributed.rb', line 441

def subscribed?
  !! @subscribed_node
end

#substr(key, start, stop) ⇒ Object



133
134
135
# File 'lib/redis/distributed.rb', line 133

def substr(key, start, stop)
  node_for(key).substr(key, start, stop)
end

#sunion(*keys) ⇒ Object



277
278
279
280
281
# File 'lib/redis/distributed.rb', line 277

def sunion(*keys)
  ensure_same_node(:sunion, *keys) do |node|
    node.sunion(*keys)
  end
end

#sunionstore(destination, *keys) ⇒ Object



283
284
285
286
287
# File 'lib/redis/distributed.rb', line 283

def sunionstore(destination, *keys)
  ensure_same_node(:sunionstore, destination, *keys) do |node|
    node.sunionstore(destination, *keys)
  end
end

#ttl(key) ⇒ Object



97
98
99
# File 'lib/redis/distributed.rb', line 97

def ttl(key)
  node_for(key).ttl(key)
end

#type(key) ⇒ Object



61
62
63
# File 'lib/redis/distributed.rb', line 61

def type(key)
  node_for(key).type(key)
end

#unsubscribe(*channels) ⇒ Object

Raises:

  • (RuntimeError)


445
446
447
448
# File 'lib/redis/distributed.rb', line 445

def unsubscribe(*channels)
  raise RuntimeError, "Can't unsubscribe if not subscribed." unless subscribed?
  @subscribed_node.unsubscribe(*channels)
end

#unwatchObject

Raises:



425
426
427
# File 'lib/redis/distributed.rb', line 425

def unwatch
  raise CannotDistribute, :unwatch
end

#watch(*keys) ⇒ Object

Raises:



421
422
423
# File 'lib/redis/distributed.rb', line 421

def watch(*keys)
  raise CannotDistribute, :watch
end

#zadd(key, score, member) ⇒ Object



309
310
311
# File 'lib/redis/distributed.rb', line 309

def zadd(key, score, member)
  node_for(key).zadd(key, score, member)
end

#zcard(key) ⇒ Object



349
350
351
# File 'lib/redis/distributed.rb', line 349

def zcard(key)
  node_for(key).zcard(key)
end

#zincrby(key, increment, member) ⇒ Object



317
318
319
# File 'lib/redis/distributed.rb', line 317

def zincrby(key, increment, member)
  node_for(key).zincrby(key, increment, member)
end

#zinterstore(destination, keys, options = {}) ⇒ Object



357
358
359
360
361
# File 'lib/redis/distributed.rb', line 357

def zinterstore(destination, keys, options = {})
  ensure_same_node(:zinterstore, destination, *keys) do |node|
    node.zinterstore(destination, keys, options)
  end
end

#zrange(key, start, stop, options = {}) ⇒ Object



321
322
323
# File 'lib/redis/distributed.rb', line 321

def zrange(key, start, stop, options = {})
  node_for(key).zrange(key, start, stop, options)
end

#zrangebyscore(key, min, max, options = {}) ⇒ Object



345
346
347
# File 'lib/redis/distributed.rb', line 345

def zrangebyscore(key, min, max, options = {})
  node_for(key).zrangebyscore(key, min, max, options)
end

#zrank(key, member) ⇒ Object



325
326
327
# File 'lib/redis/distributed.rb', line 325

def zrank(key, member)
  node_for(key).zrank(key, member)
end

#zrem(key, member) ⇒ Object



313
314
315
# File 'lib/redis/distributed.rb', line 313

def zrem(key, member)
  node_for(key).zrem(key, member)
end

#zremrangebyrank(key, start, stop) ⇒ Object



341
342
343
# File 'lib/redis/distributed.rb', line 341

def zremrangebyrank(key, start, stop)
  node_for(key).zremrangebyrank(key, start, stop)
end

#zremrangebyscore(key, min, max) ⇒ Object



337
338
339
# File 'lib/redis/distributed.rb', line 337

def zremrangebyscore(key, min, max)
  node_for(key).zremrangebyscore(key, min, max)
end

#zrevrange(key, start, stop, options = {}) ⇒ Object



333
334
335
# File 'lib/redis/distributed.rb', line 333

def zrevrange(key, start, stop, options = {})
  node_for(key).zrevrange(key, start, stop, options)
end

#zrevrank(key, member) ⇒ Object



329
330
331
# File 'lib/redis/distributed.rb', line 329

def zrevrank(key, member)
  node_for(key).zrevrank(key, member)
end

#zscore(key, member) ⇒ Object



353
354
355
# File 'lib/redis/distributed.rb', line 353

def zscore(key, member)
  node_for(key).zscore(key, member)
end

#zunionstore(destination, keys, options = {}) ⇒ Object



363
364
365
366
367
# File 'lib/redis/distributed.rb', line 363

def zunionstore(destination, keys, options = {})
  ensure_same_node(:zunionstore, destination, *keys) do |node|
    node.zunionstore(destination, keys, options)
  end
end