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.8"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis.



33
34
35
36
37
38
39
# File 'lib/redis.rb', line 33

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



595
596
597
# File 'lib/redis.rb', line 595

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Class Method Details

.connect(options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/redis.rb', line 18

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



12
13
14
# File 'lib/redis.rb', line 12

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

Instance Method Details

#[](key) ⇒ Object



443
444
445
# File 'lib/redis.rb', line 443

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



447
448
449
# File 'lib/redis.rb', line 447

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

#append(key, value) ⇒ Object



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

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

#auth(password) ⇒ Object



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

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

#bgrewriteaofObject



76
77
78
# File 'lib/redis.rb', line 76

def bgrewriteaof
  @client.call(:bgrewriteaof)
end

#bgsaveObject



72
73
74
# File 'lib/redis.rb', line 72

def bgsave
  @client.call(:bgsave)
end

#blpop(*args) ⇒ Object



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

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

#brpop(*args) ⇒ Object



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

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

#config(action, *args) ⇒ Object



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

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

#dbsizeObject



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

def dbsize
  @client.call(:dbsize)
end

#debug(*args) ⇒ Object



435
436
437
# File 'lib/redis.rb', line 435

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

#decr(key) ⇒ Object



499
500
501
# File 'lib/redis.rb', line 499

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

#decrby(key, decrement) ⇒ Object



503
504
505
# File 'lib/redis.rb', line 503

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

#del(*keys) ⇒ Object



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

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

#discardObject



423
424
425
# File 'lib/redis.rb', line 423

def discard
  @client.call(:discard)
end

#echo(value) ⇒ Object



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

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

#execObject



542
543
544
# File 'lib/redis.rb', line 542

def exec
  @client.call(:exec)
end

#exists(key) ⇒ Object



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

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

#expire(key, seconds) ⇒ Object



371
372
373
# File 'lib/redis.rb', line 371

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

#expireat(key, unix_time) ⇒ Object



383
384
385
# File 'lib/redis.rb', line 383

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

#flushallObject



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

def flushall
  @client.call(:flushall)
end

#flushdbObject



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

def flushdb
  @client.call(:flushdb)
end

#get(key) ⇒ Object



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

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

#getset(key, value) ⇒ Object



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

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

#hdel(key, field) ⇒ Object



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

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

#hexists(key, field) ⇒ Object



427
428
429
# File 'lib/redis.rb', line 427

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

#hget(key, field) ⇒ Object



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

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

#hgetall(key) ⇒ Object



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

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

#hincrby(key, field, increment) ⇒ Object



419
420
421
# File 'lib/redis.rb', line 419

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

#hkeys(key) ⇒ Object



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

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

#hlen(key) ⇒ Object



411
412
413
# File 'lib/redis.rb', line 411

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

#hmget(key, *fields) ⇒ Object



403
404
405
# File 'lib/redis.rb', line 403

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

#hmset(key, *attrs) ⇒ Object



395
396
397
# File 'lib/redis.rb', line 395

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

#hset(key, field, value) ⇒ Object



387
388
389
# File 'lib/redis.rb', line 387

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

#hsetnx(key, field, value) ⇒ Object



391
392
393
# File 'lib/redis.rb', line 391

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

#hvals(key) ⇒ Object



415
416
417
# File 'lib/redis.rb', line 415

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

#idObject



587
588
589
# File 'lib/redis.rb', line 587

def id
  @client.id
end

#incr(key) ⇒ Object



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

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

#incrby(key, increment) ⇒ Object



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

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

#infoObject



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

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

#inspectObject



591
592
593
# File 'lib/redis.rb', line 591

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

#keys(pattern = "*") ⇒ Object



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

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

#lastsaveObject



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

def lastsave
  @client.call(:lastsave)
end

#lindex(key, index) ⇒ Object



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

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

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



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

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

#llen(key) ⇒ Object



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

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

#lpop(key) ⇒ Object



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

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

#lpush(key, value) ⇒ Object



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

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

#lpushx(key, value) ⇒ Object



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

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

#lrange(key, start, stop) ⇒ Object



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

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

#lrem(key, count, value) ⇒ Object



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

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

#lset(key, index, value) ⇒ Object



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

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

#ltrim(key, start, stop) ⇒ Object



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

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

#mapped_hmget(key, *fields) ⇒ Object



407
408
409
# File 'lib/redis.rb', line 407

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

#mapped_hmset(key, hash) ⇒ Object



399
400
401
# File 'lib/redis.rb', line 399

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

#mapped_mget(*keys) ⇒ Object



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

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

#mapped_mset(hash) ⇒ Object



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

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

#mapped_msetnx(hash) ⇒ Object



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

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

#mget(*keys) ⇒ Object



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

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

#monitor(&block) ⇒ Object



431
432
433
# File 'lib/redis.rb', line 431

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

#move(key, db) ⇒ Object



351
352
353
# File 'lib/redis.rb', line 351

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

#mset(*args) ⇒ Object



459
460
461
# File 'lib/redis.rb', line 459

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

#msetnx(*args) ⇒ Object



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

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

#multi(&block) ⇒ Object



546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/redis.rb', line 546

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



375
376
377
# File 'lib/redis.rb', line 375

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

#pingObject



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

def ping
  @client.call(:ping)
end

#pipelinedObject



526
527
528
529
530
531
532
# File 'lib/redis.rb', line 526

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



583
584
585
# File 'lib/redis.rb', line 583

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

#publish(channel, message) ⇒ Object



561
562
563
# File 'lib/redis.rb', line 561

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

#punsubscribe(*channels) ⇒ Object

Raises:

  • (RuntimeError)


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

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

#quitObject



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

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

#randomkeyObject



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

def randomkey
  @client.call(:randomkey)
end

#rename(old_name, new_name) ⇒ Object



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

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

#renamenx(old_name, new_name) ⇒ Object



367
368
369
# File 'lib/redis.rb', line 367

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

#rpop(key) ⇒ Object



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

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

#rpoplpush(source, destination) ⇒ Object



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

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

#rpush(key, value) ⇒ Object



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

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

#rpushx(key, value) ⇒ Object



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

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

#sadd(key, value) ⇒ Object



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

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

#saveObject



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

def save
  @client.call(:save)
end

#scard(key) ⇒ Object



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

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

#sdiff(*keys) ⇒ Object



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

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

#sdiffstore(destination, *keys) ⇒ Object



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

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

#select(db) ⇒ Object



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

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

#set(key, value) ⇒ Object



451
452
453
# File 'lib/redis.rb', line 451

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

#setex(key, ttl, value) ⇒ Object



455
456
457
# File 'lib/redis.rb', line 455

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

#setnx(key, value) ⇒ Object



355
356
357
# File 'lib/redis.rb', line 355

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

#shutdownObject



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

def shutdown
  @client.call(:shutdown)
end

#sinter(*keys) ⇒ Object



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

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

#sinterstore(destination, *keys) ⇒ Object



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

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

#sismember(key, member) ⇒ Object



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

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

#slaveof(host, port) ⇒ Object



522
523
524
# File 'lib/redis.rb', line 522

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

#smembers(key) ⇒ Object



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

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

#smove(source, destination, member) ⇒ Object



228
229
230
# File 'lib/redis.rb', line 228

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

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



479
480
481
482
483
484
485
486
487
488
489
# File 'lib/redis.rb', line 479

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



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

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

#srandmember(key) ⇒ Object



264
265
266
# File 'lib/redis.rb', line 264

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

#srem(key, value) ⇒ Object



224
225
226
# File 'lib/redis.rb', line 224

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

#strlen(key) ⇒ Object



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

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

#subscribe(*channels, &block) ⇒ Object



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

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

#subscribed?Boolean

Returns:

  • (Boolean)


565
566
567
# File 'lib/redis.rb', line 565

def subscribed?
  @client.kind_of? SubscribedClient
end

#substr(key, start, stop) ⇒ Object



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

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

#sunion(*keys) ⇒ Object



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

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

#sunionstore(destination, *keys) ⇒ Object



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

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

#syncObject



439
440
441
# File 'lib/redis.rb', line 439

def sync
  @client.call(:sync)
end

#ttl(key) ⇒ Object



379
380
381
# File 'lib/redis.rb', line 379

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

#type(key) ⇒ Object



507
508
509
# File 'lib/redis.rb', line 507

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

#unsubscribe(*channels) ⇒ Object

Raises:

  • (RuntimeError)


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

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

#unwatchObject



538
539
540
# File 'lib/redis.rb', line 538

def unwatch
  @client.call(:unwatch)
end

#watch(*keys) ⇒ Object



534
535
536
# File 'lib/redis.rb', line 534

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

#zadd(key, score, member) ⇒ Object



268
269
270
# File 'lib/redis.rb', line 268

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

#zcard(key) ⇒ Object



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

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

#zcount(key, start, stop) ⇒ Object



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

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

#zincrby(key, increment, member) ⇒ Object



280
281
282
# File 'lib/redis.rb', line 280

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

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



333
334
335
336
337
338
339
340
# File 'lib/redis.rb', line 333

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



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

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



296
297
298
299
300
301
302
303
# File 'lib/redis.rb', line 296

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



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

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

#zrem(key, member) ⇒ Object



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

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

#zremrangebyrank(key, start, stop) ⇒ Object



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

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

#zremrangebyscore(key, min, max) ⇒ Object



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

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

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



309
310
311
312
313
314
315
# File 'lib/redis.rb', line 309

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



276
277
278
# File 'lib/redis.rb', line 276

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

#zscore(key, member) ⇒ Object



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

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

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



342
343
344
345
346
347
348
349
# File 'lib/redis.rb', line 342

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