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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis.



31
32
33
34
35
36
37
# File 'lib/redis.rb', line 31

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



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

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
# File 'lib/redis.rb', line 18

def self.connect(options = {})
  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



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

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



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

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

#append(key, value) ⇒ Object



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

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

#bgsaveObject



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

def bgsave
  @client.call(:bgsave)
end

#blpop(*args) ⇒ Object



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

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

#brpop(*args) ⇒ Object



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

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

#dbsizeObject



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

def dbsize
  @client.call(:dbsize)
end

#decr(key) ⇒ Object



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

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

#decrby(key, decrement) ⇒ Object



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

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

#del(*keys) ⇒ Object



319
320
321
# File 'lib/redis.rb', line 319

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

#discardObject



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

def discard
  @client.call(:discard)
end

#echo(value) ⇒ Object



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

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

#execObject



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

def exec
  @client.call(:exec)
end

#exists(key) ⇒ Object



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

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

#expire(key, seconds) ⇒ Object



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

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

#expireat(key, unix_time) ⇒ Object



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

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

#flushdbObject



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

def flushdb
  @client.call(:flushdb)
end

#get(key) ⇒ Object



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

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

#getset(key, value) ⇒ Object



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

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

#hdel(key, field) ⇒ Object



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

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

#hexists(key, field) ⇒ Object



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

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

#hget(key, field) ⇒ Object



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

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

#hgetall(key) ⇒ Object



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

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

#hincrby(key, field, increment) ⇒ Object



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

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

#hkeys(key) ⇒ Object



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

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

#hlen(key) ⇒ Object



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

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

#hmset(key, *attrs) ⇒ Object



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

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

#hset(key, field, value) ⇒ Object



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

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

#hvals(key) ⇒ Object



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

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

#idObject



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

def id
  @client.id
end

#incr(key) ⇒ Object



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

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

#incrby(key, increment) ⇒ Object



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

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

#infoObject



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

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

#inspectObject



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

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

#keys(pattern = "*") ⇒ Object



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

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

#lastsaveObject



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

def lastsave
  @client.call(:lastsave)
end

#lindex(key, index) ⇒ Object



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

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

#llen(key) ⇒ Object



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

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

#lpop(key) ⇒ Object



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

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

#lpush(key, value) ⇒ Object



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

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

#lrange(key, start, stop) ⇒ Object



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

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

#lrem(key, count, value) ⇒ Object



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

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

#lset(key, index, value) ⇒ Object



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

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

#ltrim(key, start, stop) ⇒ Object



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

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

#mapped_hmset(key, hash) ⇒ Object



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

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

#mapped_mget(*keys) ⇒ Object



411
412
413
414
415
416
417
418
# File 'lib/redis.rb', line 411

def mapped_mget(*keys)
  result = {}
  mget(*keys).each do |value|
    key = keys.shift
    result.merge!(key => value) unless value.nil?
  end
  result
end

#mapped_mset(hash) ⇒ Object



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

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

#mapped_msetnx(hash) ⇒ Object



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

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

#mget(*keys) ⇒ Object



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

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

#monitor(&block) ⇒ Object



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

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

#move(key, db) ⇒ Object



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

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

#mset(*args) ⇒ Object



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

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

#msetnx(*args) ⇒ Object



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

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

#multi(&block) ⇒ Object



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

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



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

def ping
  @client.call(:ping)
end

#pipelinedObject



459
460
461
462
463
464
465
# File 'lib/redis.rb', line 459

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



516
517
518
# File 'lib/redis.rb', line 516

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

#publish(channel, message) ⇒ Object



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

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

#punsubscribe(*channels) ⇒ Object

Raises:

  • (RuntimeError)


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

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

#quitObject



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

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

#randomkeyObject



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

def randomkey
  @client.call(:randomkey)
end

#rename(old_name, new_name) ⇒ Object



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

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

#renamenx(old_name, new_name) ⇒ Object



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

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

#rpop(key) ⇒ Object



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

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

#rpoplpush(source, destination) ⇒ Object



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

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

#rpush(key, value) ⇒ Object



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

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

#sadd(key, value) ⇒ Object



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

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

#saveObject



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

def save
  @client.call(:save)
end

#scard(key) ⇒ Object



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

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

#sdiff(*keys) ⇒ Object



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

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

#sdiffstore(destination, *keys) ⇒ Object



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

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

#select(db) ⇒ Object



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

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

#set(key, value) ⇒ Object



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

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

#setex(key, ttl, value) ⇒ Object



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

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

#setnx(key, value) ⇒ Object



315
316
317
# File 'lib/redis.rb', line 315

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

#sinter(*keys) ⇒ Object



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

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

#sinterstore(destination, *keys) ⇒ Object



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

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

#sismember(key, member) ⇒ Object



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

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

#smembers(key) ⇒ Object



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

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

#smove(source, destination, member) ⇒ Object



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

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

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



420
421
422
423
424
425
426
427
428
429
430
# File 'lib/redis.rb', line 420

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



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

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

#srandmember(key) ⇒ Object



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

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

#srem(key, value) ⇒ Object



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

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

#subscribe(*channels, &block) ⇒ Object



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

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

#subscribed?Boolean

Returns:

  • (Boolean)


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

def subscribed?
  @client.kind_of? SubscribedClient
end

#substr(key, start, stop) ⇒ Object



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

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

#sunion(*keys) ⇒ Object



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

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

#sunionstore(destination, *keys) ⇒ Object



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

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

#ttl(key) ⇒ Object



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

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

#type(key) ⇒ Object



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

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

#unsubscribe(*channels) ⇒ Object

Raises:

  • (RuntimeError)


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

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

#unwatchObject



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

def unwatch
  @client.call(:unwatch)
end

#watch(*keys) ⇒ Object



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

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

#zadd(key, score, member) ⇒ Object



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

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

#zcard(key) ⇒ Object



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

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

#zincrby(key, increment, member) ⇒ Object



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

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

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



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

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



252
253
254
255
256
257
258
# File 'lib/redis.rb', line 252

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



260
261
262
263
264
265
266
267
# File 'lib/redis.rb', line 260

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



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

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

#zrem(key, member) ⇒ Object



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

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

#zremrangebyrank(key, start, stop) ⇒ Object



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

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

#zremrangebyscore(key, min, max) ⇒ Object



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

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

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



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

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



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

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

#zscore(key, member) ⇒ Object



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

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

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



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

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