Class: Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/redis.rb,
lib/redis/client.rb,
lib/redis/pipeline.rb,
lib/redis/hash_ring.rb,
lib/redis/subscribe.rb,
lib/redis/distributed.rb

Defined Under Namespace

Classes: Client, CommandOptions, Distributed, HashRing, Pipeline, ProtocolError, SubscribedClient, Subscription

Constant Summary collapse

VERSION =
"2.0.9"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis.



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

def initialize(options = {})
  if options[:thread_safe]
    @client = Client::ThreadSafe.new(options)
  else
    @client = Client.new(options)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(command, *args) ⇒ Object



601
602
603
# File 'lib/redis.rb', line 601

def method_missing(command, *args)
  @client.call(command, *args)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



22
23
24
# File 'lib/redis.rb', line 22

def client
  @client
end

Class Method Details

.connect(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/redis.rb', line 24

def self.connect(options = {})
  options = options.dup

  require "uri"

  url = URI(options.delete(:url) || ENV["REDIS_URL"] || "redis://127.0.0.1:6379/0")

  options[:host]     ||= url.host
  options[:port]     ||= url.port
  options[:password] ||= url.password
  options[:db]       ||= url.path[1..-1].to_i

  new(options)
end

.deprecate(message, trace = caller[0]) ⇒ Object



18
19
20
# File 'lib/redis.rb', line 18

def self.deprecate(message, trace = caller[0])
  $stderr.puts "\n#{message} (in #{trace})"
end

Instance Method Details

#[](key) ⇒ Object



449
450
451
# File 'lib/redis.rb', line 449

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



453
454
455
# File 'lib/redis.rb', line 453

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

#append(key, value) ⇒ Object



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

def append(key, value)
  @client.call(:append, key, value)
end

#auth(password) ⇒ Object



47
48
49
# File 'lib/redis.rb', line 47

def auth(password)
  @client.call(:auth, password)
end

#bgrewriteaofObject



82
83
84
# File 'lib/redis.rb', line 82

def bgrewriteaof
  @client.call(:bgrewriteaof)
end

#bgsaveObject



78
79
80
# File 'lib/redis.rb', line 78

def bgsave
  @client.call(:bgsave)
end

#blpop(*args) ⇒ Object



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

def blpop(*args)
  @client.call_without_timeout(:blpop, *args)
end

#brpop(*args) ⇒ Object



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

def brpop(*args)
  @client.call_without_timeout(:brpop, *args)
end

#config(action, *args) ⇒ Object



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

def config(action, *args)
  response = @client.call(:config, action, *args)
  response = Hash[*response] if action == :get
  response
end

#dbsizeObject



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

def dbsize
  @client.call(:dbsize)
end

#debug(*args) ⇒ Object



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

def debug(*args)
  @client.call(:debug, *args)
end

#decr(key) ⇒ Object



505
506
507
# File 'lib/redis.rb', line 505

def decr(key)
  @client.call(:decr, key)
end

#decrby(key, decrement) ⇒ Object



509
510
511
# File 'lib/redis.rb', line 509

def decrby(key, decrement)
  @client.call(:decrby, key, decrement)
end

#del(*keys) ⇒ Object



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

def del(*keys)
  @client.call(:del, *keys)
end

#discardObject



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

def discard
  @client.call(:discard)
end

#echo(value) ⇒ Object



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

def echo(value)
  @client.call(:echo, value)
end

#execObject



548
549
550
# File 'lib/redis.rb', line 548

def exec
  @client.call(:exec)
end

#exists(key) ⇒ Object



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

def exists(key)
  _bool @client.call(:exists, key)
end

#expire(key, seconds) ⇒ Object



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

def expire(key, seconds)
  _bool @client.call(:expire, key, seconds)
end

#expireat(key, unix_time) ⇒ Object



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

def expireat(key, unix_time)
  _bool @client.call(:expireat, key, unix_time)
end

#flushallObject



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

def flushall
  @client.call(:flushall)
end

#flushdbObject



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

def flushdb
  @client.call(:flushdb)
end

#get(key) ⇒ Object



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

def get(key)
  @client.call(:get, key)
end

#getset(key, value) ⇒ Object



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

def getset(key, value)
  @client.call(:getset, key, value)
end

#hdel(key, field) ⇒ Object



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

def hdel(key, field)
  @client.call(:hdel, key, field)
end

#hexists(key, field) ⇒ Object



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

def hexists(key, field)
  _bool @client.call(:hexists, key, field)
end

#hget(key, field) ⇒ Object



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

def hget(key, field)
  @client.call(:hget, key, field)
end

#hgetall(key) ⇒ Object



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

def hgetall(key)
  Hash[*@client.call(:hgetall, key)]
end

#hincrby(key, field, increment) ⇒ Object



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

def hincrby(key, field, increment)
  @client.call(:hincrby, key, field, increment)
end

#hkeys(key) ⇒ Object



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

def hkeys(key)
  @client.call(:hkeys, key)
end

#hlen(key) ⇒ Object



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

def hlen(key)
  @client.call(:hlen, key)
end

#hmget(key, *fields) ⇒ Object



409
410
411
# File 'lib/redis.rb', line 409

def hmget(key, *fields)
  @client.call(:hmget, key, *fields)
end

#hmset(key, *attrs) ⇒ Object



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

def hmset(key, *attrs)
  @client.call(:hmset, key, *attrs)
end

#hset(key, field, value) ⇒ Object



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

def hset(key, field, value)
  _bool @client.call(:hset, key, field, value)
end

#hsetnx(key, field, value) ⇒ Object



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

def hsetnx(key, field, value)
  _bool @client.call(:hsetnx, key, field, value)
end

#hvals(key) ⇒ Object



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

def hvals(key)
  @client.call(:hvals, key)
end

#idObject



593
594
595
# File 'lib/redis.rb', line 593

def id
  @client.id
end

#incr(key) ⇒ Object



497
498
499
# File 'lib/redis.rb', line 497

def incr(key)
  @client.call(:incr, key)
end

#incrby(key, increment) ⇒ Object



501
502
503
# File 'lib/redis.rb', line 501

def incrby(key, increment)
  @client.call(:incrby, key, increment)
end

#infoObject



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

def info
  Hash[*@client.call(:info).split(/:|\r\n/)]
end

#inspectObject



597
598
599
# File 'lib/redis.rb', line 597

def inspect
  "#<Redis client v#{Redis::VERSION} connected to #{id} (Redis v#{info["redis_version"]})>"
end

#keys(pattern = "*") ⇒ Object



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

def keys(pattern = "*")
  _array @client.call(:keys, pattern)
end

#lastsaveObject



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

def lastsave
  @client.call(:lastsave)
end

#lindex(key, index) ⇒ Object



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

def lindex(key, index)
  @client.call(:lindex, key, index)
end

#linsert(key, where, pivot, value) ⇒ Object



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

def linsert(key, where, pivot, value)
  @client.call(:linsert, key, where, pivot, value)
end

#llen(key) ⇒ Object



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

def llen(key)
  @client.call(:llen, key)
end

#lpop(key) ⇒ Object



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

def lpop(key)
  @client.call(:lpop, key)
end

#lpush(key, value) ⇒ Object



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

def lpush(key, value)
  @client.call(:lpush, key, value)
end

#lpushx(key, value) ⇒ Object



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

def lpushx(key, value)
  @client.call(:lpushx, key, value)
end

#lrange(key, start, stop) ⇒ Object



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

def lrange(key, start, stop)
  @client.call(:lrange, key, start, stop)
end

#lrem(key, count, value) ⇒ Object



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

def lrem(key, count, value)
  @client.call(:lrem, key, count, value)
end

#lset(key, index, value) ⇒ Object



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

def lset(key, index, value)
  @client.call(:lset, key, index, value)
end

#ltrim(key, start, stop) ⇒ Object



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

def ltrim(key, start, stop)
  @client.call(:ltrim, key, start, stop)
end

#mapped_hmget(key, *fields) ⇒ Object



413
414
415
# File 'lib/redis.rb', line 413

def mapped_hmget(key, *fields)
  Hash[*fields.zip(hmget(key, *fields)).flatten]
end

#mapped_hmset(key, hash) ⇒ Object



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

def mapped_hmset(key, hash)
  hmset(key, *hash.to_a.flatten)
end

#mapped_mget(*keys) ⇒ Object



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

def mapped_mget(*keys)
  Hash[*keys.zip(mget(*keys)).flatten]
end

#mapped_mset(hash) ⇒ Object



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

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

#mapped_msetnx(hash) ⇒ Object



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

def mapped_msetnx(hash)
  msetnx(*hash.to_a.flatten)
end

#mget(*keys) ⇒ Object



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

def mget(*keys)
  @client.call(:mget, *keys)
end

#monitor(&block) ⇒ Object



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

def monitor(&block)
  @client.call_loop(:monitor, &block)
end

#move(key, db) ⇒ Object



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

def move(key, db)
  _bool @client.call(:move, key, db)
end

#mset(*args) ⇒ Object



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

def mset(*args)
  @client.call(:mset, *args)
end

#msetnx(*args) ⇒ Object



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

def msetnx(*args)
  @client.call(:msetnx, *args)
end

#multi(&block) ⇒ Object



552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/redis.rb', line 552

def multi(&block)
  result = @client.call :multi

  return result unless block_given?

  begin
    yield(self)
  rescue Exception => e
    discard
    raise e
  end

  exec
end

#persist(key) ⇒ Object



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

def persist(key)
  _bool @client.call(:persist, key)
end

#pingObject



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

def ping
  @client.call(:ping)
end

#pipelinedObject



532
533
534
535
536
537
538
# File 'lib/redis.rb', line 532

def pipelined
  original, @client = @client, Pipeline.new
  yield
  original.call_pipelined(@client.commands) unless @client.commands.empty?
ensure
  @client = original
end

#psubscribe(*channels, &block) ⇒ Object



589
590
591
# File 'lib/redis.rb', line 589

def psubscribe(*channels, &block)
  subscription(:psubscribe, channels, block)
end

#publish(channel, message) ⇒ Object



567
568
569
# File 'lib/redis.rb', line 567

def publish(channel, message)
  @client.call(:publish, channel, message)
end

#punsubscribe(*channels) ⇒ Object

Raises:

  • (RuntimeError)


580
581
582
583
# File 'lib/redis.rb', line 580

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

#quitObject



517
518
519
520
521
522
# File 'lib/redis.rb', line 517

def quit
  @client.call(:quit)
rescue Errno::ECONNRESET
ensure
  @client.disconnect
end

#randomkeyObject



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

def randomkey
  @client.call(:randomkey)
end

#rename(old_name, new_name) ⇒ Object



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

def rename(old_name, new_name)
  @client.call(:rename, old_name, new_name)
end

#renamenx(old_name, new_name) ⇒ Object



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

def renamenx(old_name, new_name)
  _bool @client.call(:renamenx, old_name, new_name)
end

#rpop(key) ⇒ Object



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

def rpop(key)
  @client.call(:rpop, key)
end

#rpoplpush(source, destination) ⇒ Object



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

def rpoplpush(source, destination)
  @client.call(:rpoplpush, source, destination)
end

#rpush(key, value) ⇒ Object



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

def rpush(key, value)
  @client.call(:rpush, key, value)
end

#rpushx(key, value) ⇒ Object



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

def rpushx(key, value)
  @client.call(:rpushx, key, value)
end

#sadd(key, value) ⇒ Object



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

def sadd(key, value)
  _bool @client.call(:sadd, key, value)
end

#saveObject



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

def save
  @client.call(:save)
end

#scard(key) ⇒ Object



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

def scard(key)
  @client.call(:scard, key)
end

#sdiff(*keys) ⇒ Object



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

def sdiff(*keys)
  @client.call(:sdiff, *keys)
end

#sdiffstore(destination, *keys) ⇒ Object



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

def sdiffstore(destination, *keys)
  @client.call(:sdiffstore, destination, *keys)
end

#select(db) ⇒ Object



51
52
53
54
# File 'lib/redis.rb', line 51

def select(db)
  @client.db = db
  @client.call(:select, db)
end

#set(key, value) ⇒ Object



457
458
459
# File 'lib/redis.rb', line 457

def set(key, value)
  @client.call(:set, key, value)
end

#setex(key, ttl, value) ⇒ Object



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

def setex(key, ttl, value)
  @client.call(:setex, key, ttl, value)
end

#setnx(key, value) ⇒ Object



361
362
363
# File 'lib/redis.rb', line 361

def setnx(key, value)
  _bool @client.call(:setnx, key, value)
end

#shutdownObject



524
525
526
# File 'lib/redis.rb', line 524

def shutdown
  @client.call(:shutdown)
end

#sinter(*keys) ⇒ Object



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

def sinter(*keys)
  @client.call(:sinter, *keys)
end

#sinterstore(destination, *keys) ⇒ Object



250
251
252
# File 'lib/redis.rb', line 250

def sinterstore(destination, *keys)
  @client.call(:sinterstore, destination, *keys)
end

#sismember(key, member) ⇒ Object



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

def sismember(key, member)
  _bool @client.call(:sismember, key, member)
end

#slaveof(host, port) ⇒ Object



528
529
530
# File 'lib/redis.rb', line 528

def slaveof(host, port)
  @client.call(:slaveof, host, port)
end

#smembers(key) ⇒ Object



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

def smembers(key)
  @client.call(:smembers, key)
end

#smove(source, destination, member) ⇒ Object



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

def smove(source, destination, member)
  _bool @client.call(:smove, source, destination, member)
end

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



485
486
487
488
489
490
491
492
493
494
495
# File 'lib/redis.rb', line 485

def sort(key, options = {})
  command = CommandOptions.new(options) do |c|
    c.value :by
    c.splat :limit
    c.multi :get
    c.words :order
    c.value :store
  end

  @client.call(:sort, key, *command.to_a)
end

#spop(key) ⇒ Object



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

def spop(key)
  @client.call(:spop, key)
end

#srandmember(key) ⇒ Object



270
271
272
# File 'lib/redis.rb', line 270

def srandmember(key)
  @client.call(:srandmember, key)
end

#srem(key, value) ⇒ Object



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

def srem(key, value)
  _bool @client.call(:srem, key, value)
end

#strlen(key) ⇒ Object



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

def strlen(key)
  @client.call(:strlen, key)
end

#subscribe(*channels, &block) ⇒ Object



585
586
587
# File 'lib/redis.rb', line 585

def subscribe(*channels, &block)
  subscription(:subscribe, channels, block)
end

#subscribed?Boolean

Returns:

  • (Boolean)


571
572
573
# File 'lib/redis.rb', line 571

def subscribed?
  @client.kind_of? SubscribedClient
end

#substr(key, start, stop) ⇒ Object



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

def substr(key, start, stop)
  @client.call(:substr, key, start, stop)
end

#sunion(*keys) ⇒ Object



254
255
256
# File 'lib/redis.rb', line 254

def sunion(*keys)
  @client.call(:sunion, *keys)
end

#sunionstore(destination, *keys) ⇒ Object



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

def sunionstore(destination, *keys)
  @client.call(:sunionstore, destination, *keys)
end

#syncObject



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

def sync
  @client.call(:sync)
end

#ttl(key) ⇒ Object



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

def ttl(key)
  @client.call(:ttl, key)
end

#type(key) ⇒ Object



513
514
515
# File 'lib/redis.rb', line 513

def type(key)
  @client.call(:type, key)
end

#unsubscribe(*channels) ⇒ Object

Raises:

  • (RuntimeError)


575
576
577
578
# File 'lib/redis.rb', line 575

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

#unwatchObject



544
545
546
# File 'lib/redis.rb', line 544

def unwatch
  @client.call(:unwatch)
end

#watch(*keys) ⇒ Object



540
541
542
# File 'lib/redis.rb', line 540

def watch(*keys)
  @client.call(:watch, *keys)
end

#zadd(key, score, member) ⇒ Object



274
275
276
# File 'lib/redis.rb', line 274

def zadd(key, score, member)
  _bool @client.call(:zadd, key, score, member)
end

#zcard(key) ⇒ Object



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

def zcard(key)
  @client.call(:zcard, key)
end

#zcount(key, start, stop) ⇒ Object



311
312
313
# File 'lib/redis.rb', line 311

def zcount(key, start, stop)
  @client.call(:zcount, key, start, stop)
end

#zincrby(key, increment, member) ⇒ Object



286
287
288
# File 'lib/redis.rb', line 286

def zincrby(key, increment, member)
  @client.call(:zincrby, key, increment, member)
end

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



339
340
341
342
343
344
345
346
# File 'lib/redis.rb', line 339

def zinterstore(destination, keys, options = {})
  command = CommandOptions.new(options) do |c|
    c.splat :weights
    c.value :aggregate
  end

  @client.call(:zinterstore, destination, keys.size, *(keys + command.to_a))
end

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



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

def zrange(key, start, stop, options = {})
  command = CommandOptions.new(options) do |c|
    c.bool :with_scores
  end

  @client.call(:zrange, key, start, stop, *command.to_a)
end

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



302
303
304
305
306
307
308
309
# File 'lib/redis.rb', line 302

def zrangebyscore(key, min, max, options = {})
  command = CommandOptions.new(options) do |c|
    c.splat :limit
    c.bool  :with_scores
  end

  @client.call(:zrangebyscore, key, min, max, *command.to_a)
end

#zrank(key, member) ⇒ Object



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

def zrank(key, member)
  @client.call(:zrank, key, member)
end

#zrem(key, member) ⇒ Object



335
336
337
# File 'lib/redis.rb', line 335

def zrem(key, member)
  _bool @client.call(:zrem, key, member)
end

#zremrangebyrank(key, start, stop) ⇒ Object



327
328
329
# File 'lib/redis.rb', line 327

def zremrangebyrank(key, start, stop)
  @client.call(:zremrangebyrank, key, start, stop)
end

#zremrangebyscore(key, min, max) ⇒ Object



323
324
325
# File 'lib/redis.rb', line 323

def zremrangebyscore(key, min, max)
  @client.call(:zremrangebyscore, key, min, max)
end

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



315
316
317
318
319
320
321
# File 'lib/redis.rb', line 315

def zrevrange(key, start, stop, options = {})
  command = CommandOptions.new(options) do |c|
    c.bool :with_scores
  end

  @client.call(:zrevrange, key, start, stop, *command.to_a)
end

#zrevrank(key, member) ⇒ Object



282
283
284
# File 'lib/redis.rb', line 282

def zrevrank(key, member)
  @client.call(:zrevrank, key, member)
end

#zscore(key, member) ⇒ Object



331
332
333
# File 'lib/redis.rb', line 331

def zscore(key, member)
  @client.call(:zscore, key, member)
end

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



348
349
350
351
352
353
354
355
# File 'lib/redis.rb', line 348

def zunionstore(destination, keys, options = {})
  command = CommandOptions.new(options) do |c|
    c.splat :weights
    c.value :aggregate
  end

  @client.call(:zunionstore, destination, keys.size, *(keys + command.to_a))
end