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

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



563
564
565
# File 'lib/redis.rb', line 563

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



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

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



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

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



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

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

#brpop(*args) ⇒ Object



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

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



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

def dbsize
  @client.call(:dbsize)
end

#decr(key) ⇒ Object



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

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

#decrby(key, decrement) ⇒ Object



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

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

#del(*keys) ⇒ Object



343
344
345
# File 'lib/redis.rb', line 343

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

#discardObject



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

def discard
  @client.call(:discard)
end

#echo(value) ⇒ Object



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

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

#execObject



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

def exec
  @client.call(:exec)
end

#exists(key) ⇒ Object



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

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

#expire(key, seconds) ⇒ Object



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

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

#expireat(key, unix_time) ⇒ Object



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

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



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

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

#hexists(key, field) ⇒ Object



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

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

#hget(key, field) ⇒ Object



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

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

#hgetall(key) ⇒ Object



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

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

#hincrby(key, field, increment) ⇒ Object



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

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

#hkeys(key) ⇒ Object



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

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

#hlen(key) ⇒ Object



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

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

#hmget(key, *fields) ⇒ Object



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

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

#hmset(key, *attrs) ⇒ Object



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

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

#hset(key, field, value) ⇒ Object



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

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

#hvals(key) ⇒ Object



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

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

#idObject



555
556
557
# File 'lib/redis.rb', line 555

def id
  @client.id
end

#incr(key) ⇒ Object



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

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

#incrby(key, increment) ⇒ Object



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

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



559
560
561
# File 'lib/redis.rb', line 559

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

#keys(pattern = "*") ⇒ Object



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

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

#lastsaveObject



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

def lastsave
  @client.call(:lastsave)
end

#lindex(key, index) ⇒ Object



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

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

#llen(key) ⇒ Object



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

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

#lpop(key) ⇒ Object



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

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

#lpush(key, value) ⇒ Object



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

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

#lrange(key, start, stop) ⇒ Object



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

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

#lrem(key, count, value) ⇒ Object



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

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

#lset(key, index, value) ⇒ Object



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

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

#ltrim(key, start, stop) ⇒ Object



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

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

#mapped_hmget(key, *fields) ⇒ Object



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

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

#mapped_hmset(key, hash) ⇒ Object



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

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

#mapped_mget(*keys) ⇒ Object



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

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

#mapped_mset(hash) ⇒ Object



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

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

#mapped_msetnx(hash) ⇒ Object



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

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



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

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

#move(key, db) ⇒ Object



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

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

#mset(*args) ⇒ Object



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

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

#msetnx(*args) ⇒ Object



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

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

#multi(&block) ⇒ Object



514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/redis.rb', line 514

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

  return result unless block_given?

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

  exec
end

#pingObject



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

def ping
  @client.call(:ping)
end

#pipelinedObject



494
495
496
497
498
499
500
# File 'lib/redis.rb', line 494

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



551
552
553
# File 'lib/redis.rb', line 551

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

#publish(channel, message) ⇒ Object



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

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

#punsubscribe(*channels) ⇒ Object

Raises:

  • (RuntimeError)


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

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

#quitObject



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

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

#randomkeyObject



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

def randomkey
  @client.call(:randomkey)
end

#rename(old_name, new_name) ⇒ Object



347
348
349
# File 'lib/redis.rb', line 347

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

#renamenx(old_name, new_name) ⇒ Object



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

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

#rpop(key) ⇒ Object



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

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

#rpoplpush(source, destination) ⇒ Object



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

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

#rpush(key, value) ⇒ Object



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

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

#sadd(key, value) ⇒ Object



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

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



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

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

#sdiff(*keys) ⇒ Object



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

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

#sdiffstore(destination, *keys) ⇒ Object



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

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



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

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

#setex(key, ttl, value) ⇒ Object



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

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

#setnx(key, value) ⇒ Object



339
340
341
# File 'lib/redis.rb', line 339

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

#shutdownObject



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

def shutdown
  @client.call(:shutdown)
end

#sinter(*keys) ⇒ Object



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

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

#sinterstore(destination, *keys) ⇒ Object



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

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

#sismember(key, member) ⇒ Object



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

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

#slaveof(host, port) ⇒ Object



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

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

#smembers(key) ⇒ Object



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

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

#smove(source, destination, member) ⇒ Object



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

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

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



447
448
449
450
451
452
453
454
455
456
457
# File 'lib/redis.rb', line 447

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



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

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

#srandmember(key) ⇒ Object



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

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

#srem(key, value) ⇒ Object



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

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

#subscribe(*channels, &block) ⇒ Object



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

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

#subscribed?Boolean

Returns:

  • (Boolean)


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

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



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

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

#sunionstore(destination, *keys) ⇒ Object



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

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

#ttl(key) ⇒ Object



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

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

#type(key) ⇒ Object



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

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

#unsubscribe(*channels) ⇒ Object

Raises:

  • (RuntimeError)


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

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

#unwatchObject



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

def unwatch
  @client.call(:unwatch)
end

#watch(*keys) ⇒ Object



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

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

#zadd(key, score, member) ⇒ Object



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

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

#zcard(key) ⇒ Object



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

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

#zcount(key, start, stop) ⇒ Object



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

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

#zincrby(key, increment, member) ⇒ Object



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

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

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



317
318
319
320
321
322
323
324
# File 'lib/redis.rb', line 317

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



272
273
274
275
276
277
278
# File 'lib/redis.rb', line 272

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



280
281
282
283
284
285
286
287
# File 'lib/redis.rb', line 280

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



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

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

#zrem(key, member) ⇒ Object



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

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

#zremrangebyrank(key, start, stop) ⇒ Object



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

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

#zremrangebyscore(key, min, max) ⇒ Object



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

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

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



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

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



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

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

#zscore(key, member) ⇒ Object



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

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

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



326
327
328
329
330
331
332
333
# File 'lib/redis.rb', line 326

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