Class: Redis

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/redis.rb,
lib/redis/client.rb,
lib/redis/version.rb,
lib/redis/pipeline.rb,
lib/redis/hash_ring.rb,
lib/redis/subscribe.rb,
lib/redis/distributed.rb,
lib/redis/connection/ruby.rb,
lib/redis/connection/hiredis.rb,
lib/redis/connection/registry.rb,
lib/redis/connection/synchrony.rb,
lib/redis/connection/command_helper.rb

Defined Under Namespace

Modules: Connection, DisableThreadSafety Classes: Client, CommandOptions, Distributed, HashRing, Pipeline, ProtocolError, SubscribedClient, Subscription

Constant Summary collapse

VERSION =
"2.2.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/redis.rb', line 53

def initialize(options = {})
  @client = Client.new(options)

  if options[:thread_safe] == false
    # Override #synchronize
    extend DisableThreadSafety
  else
    # Monitor#initialize
    super()
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(command, *args) ⇒ Object



1079
1080
1081
1082
1083
# File 'lib/redis.rb', line 1079

def method_missing(command, *args)
  synchronize do
    @client.call [command, *args]
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Class Method Details

.connect(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/redis.rb', line 28

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

.currentObject



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

def self.current
  Thread.current[:redis] ||= Redis.connect
end

.current=(redis) ⇒ Object



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

def self.current=(redis)
  Thread.current[:redis] = redis
end

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



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

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

Instance Method Details

#[](key) ⇒ Object



830
831
832
# File 'lib/redis.rb', line 830

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



834
835
836
# File 'lib/redis.rb', line 834

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

#append(key, value) ⇒ Object

Append a value to a key.



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

def append(key, value)
  synchronize do
    @client.call [:append, key, value]
  end
end

#auth(password) ⇒ Object

Authenticate to the server.



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

def auth(password)
  synchronize do
    @client.call [:auth, password]
  end
end

#bgrewriteaofObject

Asynchronously rewrite the append-only file.



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

def bgrewriteaof
  synchronize do
    @client.call [:bgrewriteaof]
  end
end

#bgsaveObject

Asynchronously save the dataset to disk.



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

def bgsave
  synchronize do
    @client.call [:bgsave]
  end
end

#blpop(*args) ⇒ Object

Remove and get the first element in a list, or block until one is available.



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

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

#brpop(*args) ⇒ Object

Remove and get the last element in a list, or block until one is available.



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

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

#brpoplpush(source, destination, timeout) ⇒ Object

Pop a value from a list, push it to another list and return it; or block until one is available.



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

def brpoplpush(source, destination, timeout)
  synchronize do
    @client.call_without_timeout(:brpoplpush, source, destination, timeout)
  end
end

#config(action, *args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/redis.rb', line 107

def config(action, *args)
  synchronize do
    reply = @client.call [:config, action, *args]

    if reply.kind_of?(Array) && action == :get
      Hash[*reply]
    else
      reply
    end
  end
end

#dbsizeObject

Return the number of keys in the selected database.



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

def dbsize
  synchronize do
    @client.call [:dbsize]
  end
end

#debug(*args) ⇒ Object



811
812
813
814
815
# File 'lib/redis.rb', line 811

def debug(*args)
  synchronize do
    @client.call [:debug, *args]
  end
end

#decr(key) ⇒ Object

Decrement the integer value of a key by one.



928
929
930
931
932
# File 'lib/redis.rb', line 928

def decr(key)
  synchronize do
    @client.call [:decr, key]
  end
end

#decrby(key, decrement) ⇒ Object

Decrement the integer value of a key by the given number.



935
936
937
938
939
# File 'lib/redis.rb', line 935

def decrby(key, decrement)
  synchronize do
    @client.call [:decrby, key, decrement]
  end
end

#del(*keys) ⇒ Object

Delete a key.



679
680
681
682
683
# File 'lib/redis.rb', line 679

def del(*keys)
  synchronize do
    @client.call [:del, *keys]
  end
end

#discardObject

Discard all commands issued after MULTI.



791
792
793
794
795
# File 'lib/redis.rb', line 791

def discard
  synchronize do
    @client.call [:discard]
  end
end

#echo(value) ⇒ Object

Echo the given string.



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

def echo(value)
  synchronize do
    @client.call [:echo, value]
  end
end

#execObject

Execute all commands issued after MULTI.



1001
1002
1003
1004
1005
# File 'lib/redis.rb', line 1001

def exec
  synchronize do
    @client.call [:exec]
  end
end

#exists(key) ⇒ Object

Determine if a key exists.



292
293
294
295
296
# File 'lib/redis.rb', line 292

def exists(key)
  synchronize do
    _bool @client.call [:exists, key]
  end
end

#expire(key, seconds) ⇒ Object

Set a key’s time to live in seconds.



700
701
702
703
704
# File 'lib/redis.rb', line 700

def expire(key, seconds)
  synchronize do
    _bool @client.call [:expire, key, seconds]
  end
end

#expireat(key, unix_time) ⇒ Object

Set the expiration for a key as a UNIX timestamp.



721
722
723
724
725
# File 'lib/redis.rb', line 721

def expireat(key, unix_time)
  synchronize do
    _bool @client.call [:expireat, key, unix_time]
  end
end

#flushallObject

Remove all keys from all databases.



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

def flushall
  synchronize do
    @client.call [:flushall]
  end
end

#flushdbObject

Remove all keys from the current database.



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

def flushdb
  synchronize do
    @client.call [:flushdb]
  end
end

#get(key) ⇒ Object

Get the value of a key.



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

def get(key)
  synchronize do
    @client.call [:get, key]
  end
end

#getbit(key, offset) ⇒ Object

Returns the bit value at offset in the string value stored at key.



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

def getbit(key, offset)
  synchronize do
    @client.call [:getbit, key, offset]
  end
end

#getrange(key, start, stop) ⇒ Object

Get a substring of the string stored at a key.



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

def getrange(key, start, stop)
  synchronize do
    @client.call [:getrange, key, start, stop]
  end
end

#getset(key, value) ⇒ Object

Set the string value of a key and return its old value.



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

def getset(key, value)
  synchronize do
    @client.call [:getset, key, value]
  end
end

#hdel(key, field) ⇒ Object

Delete a hash field.



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

def hdel(key, field)
  synchronize do
    @client.call [:hdel, key, field]
  end
end

#hexists(key, field) ⇒ Object

Determine if a hash field exists.



798
799
800
801
802
# File 'lib/redis.rb', line 798

def hexists(key, field)
  synchronize do
    _bool @client.call [:hexists, key, field]
  end
end

#hget(key, field) ⇒ Object

Get the value of a hash field.



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

def hget(key, field)
  synchronize do
    @client.call [:hget, key, field]
  end
end

#hgetall(key) ⇒ Object

Get all the fields and values in a hash.



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/redis.rb', line 210

def hgetall(key)
  synchronize do
    reply = @client.call [:hgetall, key]

    if reply.kind_of?(Array)
      Hash[*reply]
    else
      reply
    end
  end
end

#hincrby(key, field, increment) ⇒ Object

Increment the integer value of a hash field by the given number.



784
785
786
787
788
# File 'lib/redis.rb', line 784

def hincrby(key, field, increment)
  synchronize do
    @client.call [:hincrby, key, field, increment]
  end
end

#hkeys(key) ⇒ Object

Get all the fields in a hash.



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

def hkeys(key)
  synchronize do
    @client.call [:hkeys, key]
  end
end

#hlen(key) ⇒ Object

Get the number of fields in a hash.



770
771
772
773
774
# File 'lib/redis.rb', line 770

def hlen(key)
  synchronize do
    @client.call [:hlen, key]
  end
end

#hmget(key, *fields) ⇒ Object

Get the values of all the given hash fields.



753
754
755
756
757
# File 'lib/redis.rb', line 753

def hmget(key, *fields)
  synchronize do
    @client.call [:hmget, key, *fields]
  end
end

#hmset(key, *attrs) ⇒ Object

Set multiple hash fields to multiple values.



742
743
744
745
746
# File 'lib/redis.rb', line 742

def hmset(key, *attrs)
  synchronize do
    @client.call [:hmset, key, *attrs]
  end
end

#hset(key, field, value) ⇒ Object

Set the string value of a hash field.



728
729
730
731
732
# File 'lib/redis.rb', line 728

def hset(key, field, value)
  synchronize do
    _bool @client.call [:hset, key, field, value]
  end
end

#hsetnx(key, field, value) ⇒ Object

Set the value of a hash field, only if the field does not exist.



735
736
737
738
739
# File 'lib/redis.rb', line 735

def hsetnx(key, field, value)
  synchronize do
    _bool @client.call [:hsetnx, key, field, value]
  end
end

#hvals(key) ⇒ Object

Get all the values in a hash.



777
778
779
780
781
# File 'lib/redis.rb', line 777

def hvals(key)
  synchronize do
    @client.call [:hvals, key]
  end
end

#idObject



1067
1068
1069
1070
1071
# File 'lib/redis.rb', line 1067

def id
  synchronize do
    @client.id
  end
end

#incr(key) ⇒ Object

Increment the integer value of a key by one.



914
915
916
917
918
# File 'lib/redis.rb', line 914

def incr(key)
  synchronize do
    @client.call [:incr, key]
  end
end

#incrby(key, increment) ⇒ Object

Increment the integer value of a key by the given number.



921
922
923
924
925
# File 'lib/redis.rb', line 921

def incrby(key, increment)
  synchronize do
    @client.call [:incrby, key, increment]
  end
end

#info(cmd = nil) ⇒ Object

Get information and statistics about the server.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/redis.rb', line 88

def info(cmd = nil)
  synchronize do
    reply = @client.call [:info, cmd].compact

    if reply.kind_of?(String)
      reply = Hash[*reply.split(/:|\r\n/).grep(/^[^#]/)]

      if cmd && cmd.to_s == "commandstats"
        # Extract nested hashes for INFO COMMANDSTATS
        reply = Hash[reply.map do |k, v|
          [k[/^cmdstat_(.*)$/, 1], Hash[*v.split(/,|=/)]]
        end]
      end
    end

    reply
  end
end

#inspectObject



1073
1074
1075
1076
1077
# File 'lib/redis.rb', line 1073

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

#keys(pattern = "*") ⇒ Object

Find all keys matching the given pattern.



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/redis.rb', line 244

def keys(pattern = "*")
  synchronize do
    reply = @client.call [:keys, pattern]

    if reply.kind_of?(String)
      reply.split(" ")
    else
      reply
    end
  end
end

#lastsaveObject

Get the UNIX time stamp of the last successful save to disk.



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

def lastsave
  synchronize do
    @client.call [:lastsave]
  end
end

#lindex(key, index) ⇒ Object

Get an element from a list by its index.



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

def lindex(key, index)
  synchronize do
    @client.call [:lindex, key, index]
  end
end

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

Insert an element before or after another element in a list.



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

def linsert(key, where, pivot, value)
  synchronize do
    @client.call [:linsert, key, where, pivot, value]
  end
end

#llen(key) ⇒ Object

Get the length of a list.



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

def llen(key)
  synchronize do
    @client.call [:llen, key]
  end
end

#lpop(key) ⇒ Object

Remove and get the first element in a list.



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

def lpop(key)
  synchronize do
    @client.call [:lpop, key]
  end
end

#lpush(key, value) ⇒ Object

Prepend a value to a list.



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

def lpush(key, value)
  synchronize do
    @client.call [:lpush, key, value]
  end
end

#lpushx(key, value) ⇒ Object

Prepend a value to a list, only if the list exists.



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

def lpushx(key, value)
  synchronize do
    @client.call [:lpushx, key, value]
  end
end

#lrange(key, start, stop) ⇒ Object

Get a range of elements from a list.



306
307
308
309
310
# File 'lib/redis.rb', line 306

def lrange(key, start, stop)
  synchronize do
    @client.call [:lrange, key, start, stop]
  end
end

#lrem(key, count, value) ⇒ Object

Remove elements from a list.



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

def lrem(key, count, value)
  synchronize do
    @client.call [:lrem, key, count, value]
  end
end

#lset(key, index, value) ⇒ Object

Set the value of an element in a list by its index.



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

def lset(key, index, value)
  synchronize do
    @client.call [:lset, key, index, value]
  end
end

#ltrim(key, start, stop) ⇒ Object

Trim a list to the specified range.



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

def ltrim(key, start, stop)
  synchronize do
    @client.call [:ltrim, key, start, stop]
  end
end

#mapped_hmget(key, *fields) ⇒ Object



759
760
761
762
763
764
765
766
767
# File 'lib/redis.rb', line 759

def mapped_hmget(key, *fields)
  reply = hmget(key, *fields)

  if reply.kind_of?(Array)
    Hash[*fields.zip(reply).flatten]
  else
    reply
  end
end

#mapped_hmset(key, hash) ⇒ Object



748
749
750
# File 'lib/redis.rb', line 748

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

#mapped_mget(*keys) ⇒ Object



888
889
890
891
892
893
894
895
896
# File 'lib/redis.rb', line 888

def mapped_mget(*keys)
  reply = mget(*keys)

  if reply.kind_of?(Array)
    Hash[*keys.zip(reply).flatten]
  else
    reply
  end
end

#mapped_mset(hash) ⇒ Object



873
874
875
# File 'lib/redis.rb', line 873

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

#mapped_msetnx(hash) ⇒ Object



884
885
886
# File 'lib/redis.rb', line 884

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

#mget(*keys) ⇒ Object

Get the values of all the given keys.



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

def mget(*keys)
  synchronize do
    @client.call [:mget, *keys]
  end
end

#monitor(&block) ⇒ Object

Listen for all requests received by the server in real time.



805
806
807
808
809
# File 'lib/redis.rb', line 805

def monitor(&block)
  synchronize do
    @client.call_loop([:monitor], &block)
  end
end

#move(key, db) ⇒ Object

Move a key to another database.



665
666
667
668
669
# File 'lib/redis.rb', line 665

def move(key, db)
  synchronize do
    _bool @client.call [:move, key, db]
  end
end

#mset(*args) ⇒ Object

Set multiple keys to multiple values.



867
868
869
870
871
# File 'lib/redis.rb', line 867

def mset(*args)
  synchronize do
    @client.call [:mset, *args]
  end
end

#msetnx(*args) ⇒ Object

Set multiple keys to multiple values, only if none of the keys exist.



878
879
880
881
882
# File 'lib/redis.rb', line 878

def msetnx(*args)
  synchronize do
    @client.call [:msetnx, *args]
  end
end

#multiObject

Mark the start of a transaction block.



1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'lib/redis.rb', line 1008

def multi
  synchronize do
    if !block_given?
      @client.call :multi
    else
      result = pipelined(:raise => false) do
        multi
        yield(self)
        exec
      end

      result.last
    end
  end
end

#object(*args) ⇒ Object



817
818
819
820
821
# File 'lib/redis.rb', line 817

def object(*args)
  synchronize do
    @client.call [:object, *args]
  end
end

#persist(key) ⇒ Object

Remove the expiration from a key.



707
708
709
710
711
# File 'lib/redis.rb', line 707

def persist(key)
  synchronize do
    _bool @client.call [:persist, key]
  end
end

#pingObject

Ping the server.



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

def ping
  synchronize do
    @client.call [:ping]
  end
end

#pipelined(options = {}) ⇒ Object



974
975
976
977
978
979
980
981
982
983
984
# File 'lib/redis.rb', line 974

def pipelined(options = {})
  synchronize do
    begin
      original, @client = @client, Pipeline.new
      yield
      original.call_pipelined(@client.commands, options) unless @client.commands.empty?
    ensure
      @client = original
    end
  end
end

#psubscribe(*channels, &block) ⇒ Object

Listen for messages published to channels matching the given patterns.



1061
1062
1063
1064
1065
# File 'lib/redis.rb', line 1061

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

#publish(channel, message) ⇒ Object

Post a message to a channel.



1025
1026
1027
1028
1029
# File 'lib/redis.rb', line 1025

def publish(channel, message)
  synchronize do
    @client.call [:publish, channel, message]
  end
end

#punsubscribe(*channels) ⇒ Object

Stop listening for messages posted to channels matching the given patterns.



1046
1047
1048
1049
1050
1051
# File 'lib/redis.rb', line 1046

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

#quitObject

Close the connection.



949
950
951
952
953
954
955
956
957
958
# File 'lib/redis.rb', line 949

def quit
  synchronize do
    begin
      @client.call [:quit]
    rescue Errno::ECONNRESET
    ensure
      @client.disconnect
    end
  end
end

#randomkeyObject

Return a random key from the keyspace.



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

def randomkey
  synchronize do
    @client.call [:randomkey]
  end
end

#rename(old_name, new_name) ⇒ Object

Rename a key.



686
687
688
689
690
# File 'lib/redis.rb', line 686

def rename(old_name, new_name)
  synchronize do
    @client.call [:rename, old_name, new_name]
  end
end

#renamenx(old_name, new_name) ⇒ Object

Rename a key, only if the new key does not exist.



693
694
695
696
697
# File 'lib/redis.rb', line 693

def renamenx(old_name, new_name)
  synchronize do
    _bool @client.call [:renamenx, old_name, new_name]
  end
end

#rpop(key) ⇒ Object

Remove and get the last element in a list.



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

def rpop(key)
  synchronize do
    @client.call [:rpop, key]
  end
end

#rpoplpush(source, destination) ⇒ Object

Remove the last element in a list, append it to another list and return it.



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

def rpoplpush(source, destination)
  synchronize do
    @client.call [:rpoplpush, source, destination]
  end
end

#rpush(key, value) ⇒ Object

Append a value to a list.



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

def rpush(key, value)
  synchronize do
    @client.call [:rpush, key, value]
  end
end

#rpushx(key, value) ⇒ Object

Append a value to a list, only if the list exists.



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

def rpushx(key, value)
  synchronize do
    @client.call [:rpushx, key, value]
  end
end

#sadd(key, value) ⇒ Object

Add a member to a set.



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

def sadd(key, value)
  synchronize do
    _bool @client.call [:sadd, key, value]
  end
end

#saveObject

Synchronously save the dataset to disk.



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

def save
  synchronize do
    @client.call [:save]
  end
end

#scard(key) ⇒ Object

Get the number of members in a set.



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

def scard(key)
  synchronize do
    @client.call [:scard, key]
  end
end

#sdiff(*keys) ⇒ Object

Subtract multiple sets.



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

def sdiff(*keys)
  synchronize do
    @client.call [:sdiff, *keys]
  end
end

#sdiffstore(destination, *keys) ⇒ Object

Subtract multiple sets and store the resulting set in a key.



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

def sdiffstore(destination, *keys)
  synchronize do
    @client.call [:sdiffstore, destination, *keys]
  end
end

#select(db) ⇒ Object

Change the selected database for the current connection.



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

def select(db)
  synchronize do
    @client.db = db
    @client.call [:select, db]
  end
end

#set(key, value) ⇒ Object

Set the string value of a key.



839
840
841
842
843
# File 'lib/redis.rb', line 839

def set(key, value)
  synchronize do
    @client.call [:set, key, value]
  end
end

#setbit(key, offset, value) ⇒ Object

Sets or clears the bit at offset in the string value stored at key.



846
847
848
849
850
# File 'lib/redis.rb', line 846

def setbit(key, offset, value)
  synchronize do
    @client.call [:setbit, key, offset, value]
  end
end

#setex(key, ttl, value) ⇒ Object

Set the value and expiration of a key.



853
854
855
856
857
# File 'lib/redis.rb', line 853

def setex(key, ttl, value)
  synchronize do
    @client.call [:setex, key, ttl, value]
  end
end

#setnx(key, value) ⇒ Object

Set the value of a key, only if the key does not exist.



672
673
674
675
676
# File 'lib/redis.rb', line 672

def setnx(key, value)
  synchronize do
    _bool @client.call [:setnx, key, value]
  end
end

#setrange(key, offset, value) ⇒ Object

Overwrite part of a string at key starting at the specified offset.



860
861
862
863
864
# File 'lib/redis.rb', line 860

def setrange(key, offset, value)
  synchronize do
    @client.call [:setrange, key, offset, value]
  end
end

#shutdownObject

Synchronously save the dataset to disk and then shut down the server.



961
962
963
964
965
# File 'lib/redis.rb', line 961

def shutdown
  synchronize do
    @client.call_without_reply [:shutdown]
  end
end

#sinter(*keys) ⇒ Object

Intersect multiple sets.



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

def sinter(*keys)
  synchronize do
    @client.call [:sinter, *keys]
  end
end

#sinterstore(destination, *keys) ⇒ Object

Intersect multiple sets and store the resulting set in a key.



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

def sinterstore(destination, *keys)
  synchronize do
    @client.call [:sinterstore, destination, *keys]
  end
end

#sismember(key, member) ⇒ Object

Determine if a given value is a member of a set.



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

def sismember(key, member)
  synchronize do
    _bool @client.call [:sismember, key, member]
  end
end

#slaveof(host, port) ⇒ Object

Make the server a slave of another instance, or promote it as master.



968
969
970
971
972
# File 'lib/redis.rb', line 968

def slaveof(host, port)
  synchronize do
    @client.call [:slaveof, host, port]
  end
end

#smembers(key) ⇒ Object

Get all the members in a set.



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

def smembers(key)
  synchronize do
    @client.call [:smembers, key]
  end
end

#smove(source, destination, member) ⇒ Object

Move a member from one set to another.



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

def smove(source, destination, member)
  synchronize do
    _bool @client.call [:smove, source, destination, member]
  end
end

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

Sort the elements in a list, set or sorted set.



899
900
901
902
903
904
905
906
907
908
909
910
911
# File 'lib/redis.rb', line 899

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

  synchronize do
    @client.call [:sort, key, *command.to_a]
  end
end

#spop(key) ⇒ Object

Remove and return a random member from a set.



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

def spop(key)
  synchronize do
    @client.call [:spop, key]
  end
end

#srandmember(key) ⇒ Object

Get a random member from a set.



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

def srandmember(key)
  synchronize do
    @client.call [:srandmember, key]
  end
end

#srem(key, value) ⇒ Object

Remove a member from a set.



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

def srem(key, value)
  synchronize do
    _bool @client.call [:srem, key, value]
  end
end

#strlen(key) ⇒ Object

Get the length of the value stored in a key.



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

def strlen(key)
  synchronize do
    @client.call [:strlen, key]
  end
end

#subscribe(*channels, &block) ⇒ Object

Listen for messages published to the given channels.



1054
1055
1056
1057
1058
# File 'lib/redis.rb', line 1054

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

#subscribed?Boolean

Returns:

  • (Boolean)


1031
1032
1033
1034
1035
# File 'lib/redis.rb', line 1031

def subscribed?
  synchronize do
    @client.kind_of? SubscribedClient
  end
end

#substr(key, start, stop) ⇒ Object



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

def substr(key, start, stop)
  synchronize do
    @client.call [:substr, key, start, stop]
  end
end

#sunion(*keys) ⇒ Object

Add multiple sets.



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

def sunion(*keys)
  synchronize do
    @client.call [:sunion, *keys]
  end
end

#sunionstore(destination, *keys) ⇒ Object

Add multiple sets and store the resulting set in a key.



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

def sunionstore(destination, *keys)
  synchronize do
    @client.call [:sunionstore, destination, *keys]
  end
end

#syncObject

Internal command used for replication.



824
825
826
827
828
# File 'lib/redis.rb', line 824

def sync
  synchronize do
    @client.call [:sync]
  end
end

#ttl(key) ⇒ Object

Get the time to live for a key.



714
715
716
717
718
# File 'lib/redis.rb', line 714

def ttl(key)
  synchronize do
    @client.call [:ttl, key]
  end
end

#type(key) ⇒ Object

Determine the type stored at key.



942
943
944
945
946
# File 'lib/redis.rb', line 942

def type(key)
  synchronize do
    @client.call [:type, key]
  end
end

#unsubscribe(*channels) ⇒ Object

Stop listening for messages posted to the given channels.



1038
1039
1040
1041
1042
1043
# File 'lib/redis.rb', line 1038

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

#unwatchObject

Forget about all watched keys.



994
995
996
997
998
# File 'lib/redis.rb', line 994

def unwatch
  synchronize do
    @client.call [:unwatch]
  end
end

#watch(*keys) ⇒ Object

Watch the given keys to determine execution of the MULTI/EXEC block.



987
988
989
990
991
# File 'lib/redis.rb', line 987

def watch(*keys)
  synchronize do
    @client.call [:watch, *keys]
  end
end

#without_reconnect(&block) ⇒ Object

Run code without the client reconnecting



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

def without_reconnect(&block)
  synchronize do
    @client.without_reconnect(&block)
  end
end

#zadd(key, score, member) ⇒ Object

Add a member to a sorted set, or update its score if it already exists.



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

def zadd(key, score, member)
  synchronize do
    _bool @client.call [:zadd, key, score, member]
  end
end

#zcard(key) ⇒ Object

Get the number of members in a sorted set.



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

def zcard(key)
  synchronize do
    @client.call [:zcard, key]
  end
end

#zcount(key, start, stop) ⇒ Object

Count the members in a sorted set with scores within the given values.



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

def zcount(key, start, stop)
  synchronize do
    @client.call [:zcount, key, start, stop]
  end
end

#zincrby(key, increment, member) ⇒ Object

Increment the score of a member in a sorted set.



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

def zincrby(key, increment, member)
  synchronize do
    @client.call [:zincrby, key, increment, member]
  end
end

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

Intersect multiple sorted sets and store the resulting sorted set in a new key.



641
642
643
644
645
646
647
648
649
650
# File 'lib/redis.rb', line 641

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

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

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

Return a range of members in a sorted set, by index.



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

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

  synchronize do
    @client.call [:zrange, key, start, stop, *command.to_a]
  end
end

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

Return a range of members in a sorted set, by score.



565
566
567
568
569
570
571
572
573
574
575
# File 'lib/redis.rb', line 565

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

  synchronize do
    @client.call [:zrangebyscore, key, min, max, *command.to_a]
  end
end

#zrank(key, member) ⇒ Object

Determine the index of a member in a sorted set.



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

def zrank(key, member)
  synchronize do
    @client.call [:zrank, key, member]
  end
end

#zrem(key, member) ⇒ Object

Remove a member from a sorted set.



633
634
635
636
637
# File 'lib/redis.rb', line 633

def zrem(key, member)
  synchronize do
    _bool @client.call [:zrem, key, member]
  end
end

#zremrangebyrank(key, start, stop) ⇒ Object

Remove all members in a sorted set within the given indexes.



619
620
621
622
623
# File 'lib/redis.rb', line 619

def zremrangebyrank(key, start, stop)
  synchronize do
    @client.call [:zremrangebyrank, key, start, stop]
  end
end

#zremrangebyscore(key, min, max) ⇒ Object

Remove all members in a sorted set within the given scores.



612
613
614
615
616
# File 'lib/redis.rb', line 612

def zremrangebyscore(key, min, max)
  synchronize do
    @client.call [:zremrangebyscore, key, min, max]
  end
end

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

Return a range of members in a sorted set, by index, with scores ordered from high to low.



586
587
588
589
590
591
592
593
594
595
# File 'lib/redis.rb', line 586

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

  synchronize do
    @client.call [:zrevrange, key, start, stop, *command.to_a]
  end
end

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

Return a range of members in a sorted set, by score, with scores ordered from high to low.



599
600
601
602
603
604
605
606
607
608
609
# File 'lib/redis.rb', line 599

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

  synchronize do
    @client.call [:zrevrangebyscore, key, max, min, *command.to_a]
  end
end

#zrevrank(key, member) ⇒ Object

Determine the index of a member in a sorted set, with scores ordered from high to low.



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

def zrevrank(key, member)
  synchronize do
    @client.call [:zrevrank, key, member]
  end
end

#zscore(key, member) ⇒ Object

Get the score associated with the given member in a sorted set.



626
627
628
629
630
# File 'lib/redis.rb', line 626

def zscore(key, member)
  synchronize do
    @client.call [:zscore, key, member]
  end
end

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

Add multiple sorted sets and store the resulting sorted set in a new key.



653
654
655
656
657
658
659
660
661
662
# File 'lib/redis.rb', line 653

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

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