Class: Redis
- Inherits:
-
Object
- Object
- Redis
- Includes:
- MonitorMixin
- Defined in:
- lib/redis.rb,
lib/redis/retry.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 Classes: Client, CommandOptions, Distributed, HashRing, Pipeline, ProtocolError, Retry, SubscribedClient, Subscription
Constant Summary collapse
- VERSION =
"2.2.2.1"
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Class Method Summary collapse
- .connect(options = {}) ⇒ Object
- .current ⇒ Object
- .current=(redis) ⇒ Object
- .deprecate(message, trace = caller[0]) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#append(key, value) ⇒ Object
Append a value to a key.
-
#auth(password) ⇒ Object
Authenticate to the server.
-
#bgrewriteaof ⇒ Object
Asynchronously rewrite the append-only file.
-
#bgsave ⇒ Object
Asynchronously save the dataset to disk.
-
#blpop(*args) ⇒ Object
Remove and get the first element in a list, or block until one is available.
-
#brpop(*args) ⇒ Object
Remove and get the last element in a list, or block until one is available.
-
#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.
- #config(action, *args) ⇒ Object
-
#dbsize ⇒ Object
Return the number of keys in the selected database.
- #debug(*args) ⇒ Object
-
#decr(key) ⇒ Object
Decrement the integer value of a key by one.
-
#decrby(key, decrement) ⇒ Object
Decrement the integer value of a key by the given number.
-
#del(*keys) ⇒ Object
Delete a key.
-
#discard ⇒ Object
Discard all commands issued after MULTI.
-
#echo(value) ⇒ Object
Echo the given string.
-
#exec ⇒ Object
Execute all commands issued after MULTI.
-
#exists(key) ⇒ Object
Determine if a key exists.
-
#expire(key, seconds) ⇒ Object
Set a key’s time to live in seconds.
-
#expireat(key, unix_time) ⇒ Object
Set the expiration for a key as a UNIX timestamp.
-
#flushall ⇒ Object
Remove all keys from all databases.
-
#flushdb ⇒ Object
Remove all keys from the current database.
-
#get(key) ⇒ Object
Get the value of a key.
-
#getbit(key, offset) ⇒ Object
Returns the bit value at offset in the string value stored at key.
-
#getrange(key, start, stop) ⇒ Object
Get a substring of the string stored at a key.
-
#getset(key, value) ⇒ Object
Set the string value of a key and return its old value.
-
#hdel(key, field) ⇒ Object
Delete a hash field.
-
#hexists(key, field) ⇒ Object
Determine if a hash field exists.
-
#hget(key, field) ⇒ Object
Get the value of a hash field.
-
#hgetall(key) ⇒ Object
Get all the fields and values in a hash.
-
#hincrby(key, field, increment) ⇒ Object
Increment the integer value of a hash field by the given number.
-
#hkeys(key) ⇒ Object
Get all the fields in a hash.
-
#hlen(key) ⇒ Object
Get the number of fields in a hash.
-
#hmget(key, *fields) ⇒ Object
Get the values of all the given hash fields.
-
#hmset(key, *attrs) ⇒ Object
Set multiple hash fields to multiple values.
-
#hset(key, field, value) ⇒ Object
Set the string value of a hash field.
-
#hsetnx(key, field, value) ⇒ Object
Set the value of a hash field, only if the field does not exist.
-
#hvals(key) ⇒ Object
Get all the values in a hash.
- #id ⇒ Object
-
#incr(key) ⇒ Object
Increment the integer value of a key by one.
-
#incrby(key, increment) ⇒ Object
Increment the integer value of a key by the given number.
-
#info(cmd = nil) ⇒ Object
Get information and statistics about the server.
-
#initialize(options = {}) ⇒ Redis
constructor
A new instance of Redis.
- #inspect ⇒ Object
-
#keys(pattern = "*") ⇒ Object
Find all keys matching the given pattern.
-
#lastsave ⇒ Object
Get the UNIX time stamp of the last successful save to disk.
-
#lindex(key, index) ⇒ Object
Get an element from a list by its index.
-
#linsert(key, where, pivot, value) ⇒ Object
Insert an element before or after another element in a list.
-
#llen(key) ⇒ Object
Get the length of a list.
-
#lpop(key) ⇒ Object
Remove and get the first element in a list.
-
#lpush(key, value) ⇒ Object
Prepend a value to a list.
-
#lpushx(key, value) ⇒ Object
Prepend a value to a list, only if the list exists.
-
#lrange(key, start, stop) ⇒ Object
Get a range of elements from a list.
-
#lrem(key, count, value) ⇒ Object
Remove elements from a list.
-
#lset(key, index, value) ⇒ Object
Set the value of an element in a list by its index.
-
#ltrim(key, start, stop) ⇒ Object
Trim a list to the specified range.
- #mapped_hmget(key, *fields) ⇒ Object
- #mapped_hmset(key, hash) ⇒ Object
- #mapped_mget(*keys) ⇒ Object
- #mapped_mset(hash) ⇒ Object
- #mapped_msetnx(hash) ⇒ Object
- #method_missing(command, *args) ⇒ Object
-
#mget(*keys) ⇒ Object
Get the values of all the given keys.
-
#monitor(&block) ⇒ Object
Listen for all requests received by the server in real time.
-
#move(key, db) ⇒ Object
Move a key to another database.
-
#mset(*args) ⇒ Object
Set multiple keys to multiple values.
-
#msetnx(*args) ⇒ Object
Set multiple keys to multiple values, only if none of the keys exist.
-
#multi ⇒ Object
Mark the start of a transaction block.
- #object(*args) ⇒ Object
-
#persist(key) ⇒ Object
Remove the expiration from a key.
-
#ping ⇒ Object
Ping the server.
- #pipelined(options = {}) ⇒ Object
-
#psubscribe(*channels, &block) ⇒ Object
Listen for messages published to channels matching the given patterns.
-
#publish(channel, message) ⇒ Object
Post a message to a channel.
-
#punsubscribe(*channels) ⇒ Object
Stop listening for messages posted to channels matching the given patterns.
-
#quit ⇒ Object
Close the connection.
-
#randomkey ⇒ Object
Return a random key from the keyspace.
-
#rename(old_name, new_name) ⇒ Object
Rename a key.
-
#renamenx(old_name, new_name) ⇒ Object
Rename a key, only if the new key does not exist.
-
#rpop(key) ⇒ Object
Remove and get the last element in a list.
-
#rpoplpush(source, destination) ⇒ Object
Remove the last element in a list, append it to another list and return it.
-
#rpush(key, value) ⇒ Object
Append a value to a list.
-
#rpushx(key, value) ⇒ Object
Append a value to a list, only if the list exists.
-
#sadd(key, value) ⇒ Object
Add a member to a set.
-
#save ⇒ Object
Synchronously save the dataset to disk.
-
#scard(key) ⇒ Object
Get the number of members in a set.
-
#sdiff(*keys) ⇒ Object
Subtract multiple sets.
-
#sdiffstore(destination, *keys) ⇒ Object
Subtract multiple sets and store the resulting set in a key.
-
#select(db) ⇒ Object
Change the selected database for the current connection.
-
#set(key, value) ⇒ Object
Set the string value of a key.
-
#setbit(key, offset, value) ⇒ Object
Sets or clears the bit at offset in the string value stored at key.
-
#setex(key, ttl, value) ⇒ Object
Set the value and expiration of a key.
-
#setnx(key, value) ⇒ Object
Set the value of a key, only if the key does not exist.
-
#setrange(key, offset, value) ⇒ Object
Overwrite part of a string at key starting at the specified offset.
-
#shutdown ⇒ Object
Synchronously save the dataset to disk and then shut down the server.
-
#sinter(*keys) ⇒ Object
Intersect multiple sets.
-
#sinterstore(destination, *keys) ⇒ Object
Intersect multiple sets and store the resulting set in a key.
-
#sismember(key, member) ⇒ Object
Determine if a given value is a member of a set.
-
#slaveof(host, port) ⇒ Object
Make the server a slave of another instance, or promote it as master.
-
#smembers(key) ⇒ Object
Get all the members in a set.
-
#smove(source, destination, member) ⇒ Object
Move a member from one set to another.
-
#sort(key, options = {}) ⇒ Object
Sort the elements in a list, set or sorted set.
-
#spop(key) ⇒ Object
Remove and return a random member from a set.
-
#srandmember(key) ⇒ Object
Get a random member from a set.
-
#srem(key, value) ⇒ Object
Remove a member from a set.
-
#strlen(key) ⇒ Object
Get the length of the value stored in a key.
-
#subscribe(*channels, &block) ⇒ Object
Listen for messages published to the given channels.
- #subscribed? ⇒ Boolean
- #substr(key, start, stop) ⇒ Object
-
#sunion(*keys) ⇒ Object
Add multiple sets.
-
#sunionstore(destination, *keys) ⇒ Object
Add multiple sets and store the resulting set in a key.
-
#sync ⇒ Object
Internal command used for replication.
-
#ttl(key) ⇒ Object
Get the time to live for a key.
-
#type(key) ⇒ Object
Determine the type stored at key.
-
#unsubscribe(*channels) ⇒ Object
Stop listening for messages posted to the given channels.
-
#unwatch ⇒ Object
Forget about all watched keys.
-
#watch(*keys) ⇒ Object
Watch the given keys to determine execution of the MULTI/EXEC block.
- #with_retry(&block) ⇒ Object
- #with_thread_safety(&block) ⇒ Object
-
#without_reconnect(&block) ⇒ Object
Run code without the client reconnecting.
- #wrapped ⇒ Object
-
#zadd(key, score, member) ⇒ Object
Add a member to a sorted set, or update its score if it already exists.
-
#zcard(key) ⇒ Object
Get the number of members in a sorted set.
-
#zcount(key, start, stop) ⇒ Object
Count the members in a sorted set with scores within the given values.
-
#zincrby(key, increment, member) ⇒ Object
Increment the score of a member in a sorted set.
-
#zinterstore(destination, keys, options = {}) ⇒ Object
Intersect multiple sorted sets and store the resulting sorted set in a new key.
-
#zrange(key, start, stop, options = {}) ⇒ Object
Return a range of members in a sorted set, by index.
-
#zrangebyscore(key, min, max, options = {}) ⇒ Object
Return a range of members in a sorted set, by score.
-
#zrank(key, member) ⇒ Object
Determine the index of a member in a sorted set.
-
#zrem(key, member) ⇒ Object
Remove a member from a sorted set.
-
#zremrangebyrank(key, start, stop) ⇒ Object
Remove all members in a sorted set within the given indexes.
-
#zremrangebyscore(key, min, max) ⇒ Object
Remove all members in a sorted set within the given scores.
-
#zrevrange(key, start, stop, options = {}) ⇒ Object
Return a range of members in a sorted set, by index, with scores ordered from high to low.
-
#zrevrangebyscore(key, max, min, options = {}) ⇒ Object
Return a range of members in a sorted set, by score, with scores ordered from high to low.
-
#zrevrank(key, member) ⇒ Object
Determine the index of a member in a sorted set, with scores ordered from high to low.
-
#zscore(key, member) ⇒ Object
Get the score associated with the given member in a sorted set.
-
#zunionstore(destination, keys, options = {}) ⇒ Object
Add multiple sorted sets and store the resulting sorted set in a new key.
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(command, *args) ⇒ Object
1095 1096 1097 1098 1099 |
# File 'lib/redis.rb', line 1095 def method_missing(command, *args) wrapped do @client.call [command, *args] end end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
21 22 23 |
# File 'lib/redis.rb', line 21 def client @client end |
Class Method Details
.connect(options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/redis.rb', line 23 def self.connect( = {}) = .dup require "uri" url = URI(.delete(:url) || ENV["REDIS_URL"] || "redis://127.0.0.1:6379/0") [:host] ||= url.host [:port] ||= url.port [:password] ||= url.password [:db] ||= url.path[1..-1].to_i new() end |
.current ⇒ Object
38 39 40 |
# File 'lib/redis.rb', line 38 def self.current Thread.current[:redis] ||= Redis.connect end |
.current=(redis) ⇒ Object
42 43 44 |
# File 'lib/redis.rb', line 42 def self.current=(redis) Thread.current[:redis] = redis end |
.deprecate(message, trace = caller[0]) ⇒ Object
17 18 19 |
# File 'lib/redis.rb', line 17 def self.deprecate(, trace = caller[0]) $stderr.puts "\n#{} (in #{trace})" end |
Instance Method Details
#[](key) ⇒ Object
846 847 848 |
# File 'lib/redis.rb', line 846 def [](key) get(key) end |
#[]=(key, value) ⇒ Object
850 851 852 |
# File 'lib/redis.rb', line 850 def []=(key,value) set(key, value) end |
#append(key, value) ⇒ Object
Append a value to a key.
206 207 208 209 210 |
# File 'lib/redis.rb', line 206 def append(key, value) wrapped do @client.call [:append, key, value] end end |
#auth(password) ⇒ Object
Authenticate to the server.
89 90 91 92 93 |
# File 'lib/redis.rb', line 89 def auth(password) wrapped do @client.call [:auth, password] end end |
#bgrewriteaof ⇒ Object
Asynchronously rewrite the append-only file.
164 165 166 167 168 |
# File 'lib/redis.rb', line 164 def bgrewriteaof wrapped do @client.call [:bgrewriteaof] end end |
#bgsave ⇒ Object
Asynchronously save the dataset to disk.
157 158 159 160 161 |
# File 'lib/redis.rb', line 157 def bgsave wrapped do @client.call [:bgsave] end end |
#blpop(*args) ⇒ Object
Remove and get the first element in a list, or block until one is available.
399 400 401 402 403 |
# File 'lib/redis.rb', line 399 def blpop(*args) wrapped 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.
406 407 408 409 410 |
# File 'lib/redis.rb', line 406 def brpop(*args) wrapped 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.
414 415 416 417 418 |
# File 'lib/redis.rb', line 414 def brpoplpush(source, destination, timeout) wrapped do @client.call_without_timeout(:brpoplpush, source, destination, timeout) end end |
#config(action, *args) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/redis.rb', line 123 def config(action, *args) wrapped do reply = @client.call [:config, action, *args] if reply.kind_of?(Array) && action == :get Hash[*reply] else reply end end end |
#dbsize ⇒ Object
Return the number of keys in the selected database.
301 302 303 304 305 |
# File 'lib/redis.rb', line 301 def dbsize wrapped do @client.call [:dbsize] end end |
#debug(*args) ⇒ Object
827 828 829 830 831 |
# File 'lib/redis.rb', line 827 def debug(*args) wrapped do @client.call [:debug, *args] end end |
#decr(key) ⇒ Object
Decrement the integer value of a key by one.
944 945 946 947 948 |
# File 'lib/redis.rb', line 944 def decr(key) wrapped do @client.call [:decr, key] end end |
#decrby(key, decrement) ⇒ Object
Decrement the integer value of a key by the given number.
951 952 953 954 955 |
# File 'lib/redis.rb', line 951 def decrby(key, decrement) wrapped do @client.call [:decrby, key, decrement] end end |
#del(*keys) ⇒ Object
Delete a key.
695 696 697 698 699 |
# File 'lib/redis.rb', line 695 def del(*keys) wrapped do @client.call [:del, *keys] end end |
#discard ⇒ Object
Discard all commands issued after MULTI.
807 808 809 810 811 |
# File 'lib/redis.rb', line 807 def discard wrapped do @client.call [:discard] end end |
#echo(value) ⇒ Object
Echo the given string.
280 281 282 283 284 |
# File 'lib/redis.rb', line 280 def echo(value) wrapped do @client.call [:echo, value] end end |
#exec ⇒ Object
Execute all commands issued after MULTI.
1017 1018 1019 1020 1021 |
# File 'lib/redis.rb', line 1017 def exec wrapped do @client.call [:exec] end end |
#exists(key) ⇒ Object
Determine if a key exists.
308 309 310 311 312 |
# File 'lib/redis.rb', line 308 def exists(key) wrapped do _bool @client.call [:exists, key] end end |
#expire(key, seconds) ⇒ Object
Set a key’s time to live in seconds.
716 717 718 719 720 |
# File 'lib/redis.rb', line 716 def expire(key, seconds) wrapped do _bool @client.call [:expire, key, seconds] end end |
#expireat(key, unix_time) ⇒ Object
Set the expiration for a key as a UNIX timestamp.
737 738 739 740 741 |
# File 'lib/redis.rb', line 737 def expireat(key, unix_time) wrapped do _bool @client.call [:expireat, key, unix_time] end end |
#flushall ⇒ Object
Remove all keys from all databases.
143 144 145 146 147 |
# File 'lib/redis.rb', line 143 def flushall wrapped do @client.call [:flushall] end end |
#flushdb ⇒ Object
Remove all keys from the current database.
136 137 138 139 140 |
# File 'lib/redis.rb', line 136 def flushdb wrapped do @client.call [:flushdb] end end |
#get(key) ⇒ Object
Get the value of a key.
171 172 173 174 175 |
# File 'lib/redis.rb', line 171 def get(key) wrapped do @client.call [:get, key] end end |
#getbit(key, offset) ⇒ Object
Returns the bit value at offset in the string value stored at key.
178 179 180 181 182 |
# File 'lib/redis.rb', line 178 def getbit(key, offset) wrapped do @client.call [:getbit, key, offset] end end |
#getrange(key, start, stop) ⇒ Object
Get a substring of the string stored at a key.
185 186 187 188 189 |
# File 'lib/redis.rb', line 185 def getrange(key, start, stop) wrapped 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.
192 193 194 195 196 |
# File 'lib/redis.rb', line 192 def getset(key, value) wrapped do @client.call [:getset, key, value] end end |
#hdel(key, field) ⇒ Object
Delete a hash field.
246 247 248 249 250 |
# File 'lib/redis.rb', line 246 def hdel(key, field) wrapped do @client.call [:hdel, key, field] end end |
#hexists(key, field) ⇒ Object
Determine if a hash field exists.
814 815 816 817 818 |
# File 'lib/redis.rb', line 814 def hexists(key, field) wrapped do _bool @client.call [:hexists, key, field] end end |
#hget(key, field) ⇒ Object
Get the value of a hash field.
239 240 241 242 243 |
# File 'lib/redis.rb', line 239 def hget(key, field) wrapped do @client.call [:hget, key, field] end end |
#hgetall(key) ⇒ Object
Get all the fields and values in a hash.
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/redis.rb', line 226 def hgetall(key) wrapped 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.
800 801 802 803 804 |
# File 'lib/redis.rb', line 800 def hincrby(key, field, increment) wrapped do @client.call [:hincrby, key, field, increment] end end |
#hkeys(key) ⇒ Object
Get all the fields in a hash.
253 254 255 256 257 |
# File 'lib/redis.rb', line 253 def hkeys(key) wrapped do @client.call [:hkeys, key] end end |
#hlen(key) ⇒ Object
Get the number of fields in a hash.
786 787 788 789 790 |
# File 'lib/redis.rb', line 786 def hlen(key) wrapped do @client.call [:hlen, key] end end |
#hmget(key, *fields) ⇒ Object
Get the values of all the given hash fields.
769 770 771 772 773 |
# File 'lib/redis.rb', line 769 def hmget(key, *fields) wrapped do @client.call [:hmget, key, *fields] end end |
#hmset(key, *attrs) ⇒ Object
Set multiple hash fields to multiple values.
758 759 760 761 762 |
# File 'lib/redis.rb', line 758 def hmset(key, *attrs) wrapped do @client.call [:hmset, key, *attrs] end end |
#hset(key, field, value) ⇒ Object
Set the string value of a hash field.
744 745 746 747 748 |
# File 'lib/redis.rb', line 744 def hset(key, field, value) wrapped 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.
751 752 753 754 755 |
# File 'lib/redis.rb', line 751 def hsetnx(key, field, value) wrapped do _bool @client.call [:hsetnx, key, field, value] end end |
#hvals(key) ⇒ Object
Get all the values in a hash.
793 794 795 796 797 |
# File 'lib/redis.rb', line 793 def hvals(key) wrapped do @client.call [:hvals, key] end end |
#id ⇒ Object
1083 1084 1085 1086 1087 |
# File 'lib/redis.rb', line 1083 def id wrapped do @client.id end end |
#incr(key) ⇒ Object
Increment the integer value of a key by one.
930 931 932 933 934 |
# File 'lib/redis.rb', line 930 def incr(key) wrapped do @client.call [:incr, key] end end |
#incrby(key, increment) ⇒ Object
Increment the integer value of a key by the given number.
937 938 939 940 941 |
# File 'lib/redis.rb', line 937 def incrby(key, increment) wrapped do @client.call [:incrby, key, increment] end end |
#info(cmd = nil) ⇒ Object
Get information and statistics about the server.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/redis.rb', line 104 def info(cmd = nil) wrapped 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 |
#inspect ⇒ Object
1089 1090 1091 1092 1093 |
# File 'lib/redis.rb', line 1089 def inspect wrapped 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.
260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/redis.rb', line 260 def keys(pattern = "*") wrapped do reply = @client.call [:keys, pattern] if reply.kind_of?(String) reply.split(" ") else reply end end end |
#lastsave ⇒ Object
Get the UNIX time stamp of the last successful save to disk.
294 295 296 297 298 |
# File 'lib/redis.rb', line 294 def lastsave wrapped do @client.call [:lastsave] end end |
#lindex(key, index) ⇒ Object
Get an element from a list by its index.
336 337 338 339 340 |
# File 'lib/redis.rb', line 336 def lindex(key, index) wrapped 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.
343 344 345 346 347 |
# File 'lib/redis.rb', line 343 def linsert(key, where, pivot, value) wrapped do @client.call [:linsert, key, where, pivot, value] end end |
#llen(key) ⇒ Object
Get the length of a list.
315 316 317 318 319 |
# File 'lib/redis.rb', line 315 def llen(key) wrapped do @client.call [:llen, key] end end |
#lpop(key) ⇒ Object
Remove and get the first element in a list.
428 429 430 431 432 |
# File 'lib/redis.rb', line 428 def lpop(key) wrapped do @client.call [:lpop, key] end end |
#lpush(key, value) ⇒ Object
Prepend a value to a list.
378 379 380 381 382 |
# File 'lib/redis.rb', line 378 def lpush(key, value) wrapped do @client.call [:lpush, key, value] end end |
#lpushx(key, value) ⇒ Object
Prepend a value to a list, only if the list exists.
385 386 387 388 389 |
# File 'lib/redis.rb', line 385 def lpushx(key, value) wrapped do @client.call [:lpushx, key, value] end end |
#lrange(key, start, stop) ⇒ Object
Get a range of elements from a list.
322 323 324 325 326 |
# File 'lib/redis.rb', line 322 def lrange(key, start, stop) wrapped do @client.call [:lrange, key, start, stop] end end |
#lrem(key, count, value) ⇒ Object
Remove elements from a list.
357 358 359 360 361 |
# File 'lib/redis.rb', line 357 def lrem(key, count, value) wrapped 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.
350 351 352 353 354 |
# File 'lib/redis.rb', line 350 def lset(key, index, value) wrapped do @client.call [:lset, key, index, value] end end |
#ltrim(key, start, stop) ⇒ Object
Trim a list to the specified range.
329 330 331 332 333 |
# File 'lib/redis.rb', line 329 def ltrim(key, start, stop) wrapped do @client.call [:ltrim, key, start, stop] end end |
#mapped_hmget(key, *fields) ⇒ Object
775 776 777 778 779 780 781 782 783 |
# File 'lib/redis.rb', line 775 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
764 765 766 |
# File 'lib/redis.rb', line 764 def mapped_hmset(key, hash) hmset(key, *hash.to_a.flatten) end |
#mapped_mget(*keys) ⇒ Object
904 905 906 907 908 909 910 911 912 |
# File 'lib/redis.rb', line 904 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
889 890 891 |
# File 'lib/redis.rb', line 889 def mapped_mset(hash) mset(*hash.to_a.flatten) end |
#mapped_msetnx(hash) ⇒ Object
900 901 902 |
# File 'lib/redis.rb', line 900 def mapped_msetnx(hash) msetnx(*hash.to_a.flatten) end |
#mget(*keys) ⇒ Object
Get the values of all the given keys.
199 200 201 202 203 |
# File 'lib/redis.rb', line 199 def mget(*keys) wrapped do @client.call [:mget, *keys] end end |
#monitor(&block) ⇒ Object
Listen for all requests received by the server in real time.
821 822 823 824 825 |
# File 'lib/redis.rb', line 821 def monitor(&block) wrapped do @client.call_loop([:monitor], &block) end end |
#move(key, db) ⇒ Object
Move a key to another database.
681 682 683 684 685 |
# File 'lib/redis.rb', line 681 def move(key, db) wrapped do _bool @client.call [:move, key, db] end end |
#mset(*args) ⇒ Object
Set multiple keys to multiple values.
883 884 885 886 887 |
# File 'lib/redis.rb', line 883 def mset(*args) wrapped do @client.call [:mset, *args] end end |
#msetnx(*args) ⇒ Object
Set multiple keys to multiple values, only if none of the keys exist.
894 895 896 897 898 |
# File 'lib/redis.rb', line 894 def msetnx(*args) wrapped do @client.call [:msetnx, *args] end end |
#multi ⇒ Object
Mark the start of a transaction block.
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 |
# File 'lib/redis.rb', line 1024 def multi wrapped 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
833 834 835 836 837 |
# File 'lib/redis.rb', line 833 def object(*args) wrapped do @client.call [:object, *args] end end |
#persist(key) ⇒ Object
Remove the expiration from a key.
723 724 725 726 727 |
# File 'lib/redis.rb', line 723 def persist(key) wrapped do _bool @client.call [:persist, key] end end |
#ping ⇒ Object
Ping the server.
287 288 289 290 291 |
# File 'lib/redis.rb', line 287 def ping wrapped do @client.call [:ping] end end |
#pipelined(options = {}) ⇒ Object
990 991 992 993 994 995 996 997 998 999 1000 |
# File 'lib/redis.rb', line 990 def pipelined( = {}) wrapped do begin original, @client = @client, Pipeline.new yield original.call_pipelined(@client.commands, ) unless @client.commands.empty? ensure @client = original end end end |
#psubscribe(*channels, &block) ⇒ Object
Listen for messages published to channels matching the given patterns.
1077 1078 1079 1080 1081 |
# File 'lib/redis.rb', line 1077 def psubscribe(*channels, &block) wrapped do subscription(:psubscribe, channels, block) end end |
#publish(channel, message) ⇒ Object
Post a message to a channel.
1041 1042 1043 1044 1045 |
# File 'lib/redis.rb', line 1041 def publish(channel, ) wrapped do @client.call [:publish, channel, ] end end |
#punsubscribe(*channels) ⇒ Object
Stop listening for messages posted to channels matching the given patterns.
1062 1063 1064 1065 1066 1067 |
# File 'lib/redis.rb', line 1062 def punsubscribe(*channels) wrapped do raise RuntimeError, "Can't unsubscribe if not subscribed." unless subscribed? @client.punsubscribe(*channels) end end |
#quit ⇒ Object
Close the connection.
965 966 967 968 969 970 971 972 973 974 |
# File 'lib/redis.rb', line 965 def quit wrapped do begin @client.call [:quit] rescue Errno::ECONNRESET ensure @client.disconnect end end end |
#randomkey ⇒ Object
Return a random key from the keyspace.
273 274 275 276 277 |
# File 'lib/redis.rb', line 273 def randomkey wrapped do @client.call [:randomkey] end end |
#rename(old_name, new_name) ⇒ Object
Rename a key.
702 703 704 705 706 |
# File 'lib/redis.rb', line 702 def rename(old_name, new_name) wrapped 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.
709 710 711 712 713 |
# File 'lib/redis.rb', line 709 def renamenx(old_name, new_name) wrapped do _bool @client.call [:renamenx, old_name, new_name] end end |
#rpop(key) ⇒ Object
Remove and get the last element in a list.
392 393 394 395 396 |
# File 'lib/redis.rb', line 392 def rpop(key) wrapped 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.
421 422 423 424 425 |
# File 'lib/redis.rb', line 421 def rpoplpush(source, destination) wrapped do @client.call [:rpoplpush, source, destination] end end |
#rpush(key, value) ⇒ Object
Append a value to a list.
364 365 366 367 368 |
# File 'lib/redis.rb', line 364 def rpush(key, value) wrapped do @client.call [:rpush, key, value] end end |
#rpushx(key, value) ⇒ Object
Append a value to a list, only if the list exists.
371 372 373 374 375 |
# File 'lib/redis.rb', line 371 def rpushx(key, value) wrapped do @client.call [:rpushx, key, value] end end |
#sadd(key, value) ⇒ Object
Add a member to a set.
449 450 451 452 453 |
# File 'lib/redis.rb', line 449 def sadd(key, value) wrapped do _bool @client.call [:sadd, key, value] end end |
#save ⇒ Object
Synchronously save the dataset to disk.
150 151 152 153 154 |
# File 'lib/redis.rb', line 150 def save wrapped do @client.call [:save] end end |
#scard(key) ⇒ Object
Get the number of members in a set.
477 478 479 480 481 |
# File 'lib/redis.rb', line 477 def scard(key) wrapped do @client.call [:scard, key] end end |
#sdiff(*keys) ⇒ Object
Subtract multiple sets.
512 513 514 515 516 |
# File 'lib/redis.rb', line 512 def sdiff(*keys) wrapped do @client.call [:sdiff, *keys] end end |
#sdiffstore(destination, *keys) ⇒ Object
Subtract multiple sets and store the resulting set in a key.
519 520 521 522 523 |
# File 'lib/redis.rb', line 519 def sdiffstore(destination, *keys) wrapped do @client.call [:sdiffstore, destination, *keys] end end |
#select(db) ⇒ Object
Change the selected database for the current connection.
96 97 98 99 100 101 |
# File 'lib/redis.rb', line 96 def select(db) wrapped do @client.db = db @client.call [:select, db] end end |
#set(key, value) ⇒ Object
Set the string value of a key.
855 856 857 858 859 |
# File 'lib/redis.rb', line 855 def set(key, value) wrapped 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.
862 863 864 865 866 |
# File 'lib/redis.rb', line 862 def setbit(key, offset, value) wrapped do @client.call [:setbit, key, offset, value] end end |
#setex(key, ttl, value) ⇒ Object
Set the value and expiration of a key.
869 870 871 872 873 |
# File 'lib/redis.rb', line 869 def setex(key, ttl, value) wrapped 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.
688 689 690 691 692 |
# File 'lib/redis.rb', line 688 def setnx(key, value) wrapped 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.
876 877 878 879 880 |
# File 'lib/redis.rb', line 876 def setrange(key, offset, value) wrapped do @client.call [:setrange, key, offset, value] end end |
#shutdown ⇒ Object
Synchronously save the dataset to disk and then shut down the server.
977 978 979 980 981 |
# File 'lib/redis.rb', line 977 def shutdown wrapped do @client.call_without_reply [:shutdown] end end |
#sinter(*keys) ⇒ Object
Intersect multiple sets.
484 485 486 487 488 |
# File 'lib/redis.rb', line 484 def sinter(*keys) wrapped do @client.call [:sinter, *keys] end end |
#sinterstore(destination, *keys) ⇒ Object
Intersect multiple sets and store the resulting set in a key.
491 492 493 494 495 |
# File 'lib/redis.rb', line 491 def sinterstore(destination, *keys) wrapped do @client.call [:sinterstore, destination, *keys] end end |
#sismember(key, member) ⇒ Object
Determine if a given value is a member of a set.
442 443 444 445 446 |
# File 'lib/redis.rb', line 442 def sismember(key, member) wrapped 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.
984 985 986 987 988 |
# File 'lib/redis.rb', line 984 def slaveof(host, port) wrapped do @client.call [:slaveof, host, port] end end |
#smembers(key) ⇒ Object
Get all the members in a set.
435 436 437 438 439 |
# File 'lib/redis.rb', line 435 def smembers(key) wrapped do @client.call [:smembers, key] end end |
#smove(source, destination, member) ⇒ Object
Move a member from one set to another.
463 464 465 466 467 |
# File 'lib/redis.rb', line 463 def smove(source, destination, member) wrapped do _bool @client.call [:smove, source, destination, member] end end |
#sort(key, options = {}) ⇒ Object
Sort the elements in a list, set or sorted set.
915 916 917 918 919 920 921 922 923 924 925 926 927 |
# File 'lib/redis.rb', line 915 def sort(key, = {}) command = CommandOptions.new() do |c| c.value :by c.splat :limit c.multi :get c.words :order c.value :store end wrapped do @client.call [:sort, key, *command.to_a] end end |
#spop(key) ⇒ Object
Remove and return a random member from a set.
470 471 472 473 474 |
# File 'lib/redis.rb', line 470 def spop(key) wrapped do @client.call [:spop, key] end end |
#srandmember(key) ⇒ Object
Get a random member from a set.
526 527 528 529 530 |
# File 'lib/redis.rb', line 526 def srandmember(key) wrapped do @client.call [:srandmember, key] end end |
#srem(key, value) ⇒ Object
Remove a member from a set.
456 457 458 459 460 |
# File 'lib/redis.rb', line 456 def srem(key, value) wrapped do _bool @client.call [:srem, key, value] end end |
#strlen(key) ⇒ Object
Get the length of the value stored in a key.
219 220 221 222 223 |
# File 'lib/redis.rb', line 219 def strlen(key) wrapped do @client.call [:strlen, key] end end |
#subscribe(*channels, &block) ⇒ Object
Listen for messages published to the given channels.
1070 1071 1072 1073 1074 |
# File 'lib/redis.rb', line 1070 def subscribe(*channels, &block) wrapped do subscription(:subscribe, channels, block) end end |
#subscribed? ⇒ Boolean
1047 1048 1049 1050 1051 |
# File 'lib/redis.rb', line 1047 def subscribed? wrapped do @client.kind_of? SubscribedClient end end |
#substr(key, start, stop) ⇒ Object
212 213 214 215 216 |
# File 'lib/redis.rb', line 212 def substr(key, start, stop) wrapped do @client.call [:substr, key, start, stop] end end |
#sunion(*keys) ⇒ Object
Add multiple sets.
498 499 500 501 502 |
# File 'lib/redis.rb', line 498 def sunion(*keys) wrapped do @client.call [:sunion, *keys] end end |
#sunionstore(destination, *keys) ⇒ Object
Add multiple sets and store the resulting set in a key.
505 506 507 508 509 |
# File 'lib/redis.rb', line 505 def sunionstore(destination, *keys) wrapped do @client.call [:sunionstore, destination, *keys] end end |
#sync ⇒ Object
Internal command used for replication.
840 841 842 843 844 |
# File 'lib/redis.rb', line 840 def sync wrapped do @client.call [:sync] end end |
#ttl(key) ⇒ Object
Get the time to live for a key.
730 731 732 733 734 |
# File 'lib/redis.rb', line 730 def ttl(key) wrapped do @client.call [:ttl, key] end end |
#type(key) ⇒ Object
Determine the type stored at key.
958 959 960 961 962 |
# File 'lib/redis.rb', line 958 def type(key) wrapped do @client.call [:type, key] end end |
#unsubscribe(*channels) ⇒ Object
Stop listening for messages posted to the given channels.
1054 1055 1056 1057 1058 1059 |
# File 'lib/redis.rb', line 1054 def unsubscribe(*channels) wrapped do raise RuntimeError, "Can't unsubscribe if not subscribed." unless subscribed? @client.unsubscribe(*channels) end end |
#unwatch ⇒ Object
Forget about all watched keys.
1010 1011 1012 1013 1014 |
# File 'lib/redis.rb', line 1010 def unwatch wrapped do @client.call [:unwatch] end end |
#watch(*keys) ⇒ Object
Watch the given keys to determine execution of the MULTI/EXEC block.
1003 1004 1005 1006 1007 |
# File 'lib/redis.rb', line 1003 def watch(*keys) wrapped do @client.call [:watch, *keys] end end |
#with_retry(&block) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/redis.rb', line 62 def with_retry(&block) @with_retry ||= (@options.keys & Redis::Retry::DEFAULT_OPTS.keys).any? if ! @with_retry block.call else @retry ||= Redis::Retry.new(client, @options) @retry.execute(&block) end end |
#with_thread_safety(&block) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/redis.rb', line 73 def with_thread_safety(&block) if @options[:thread_safe] != false synchronize(&block) else block.call end end |
#without_reconnect(&block) ⇒ Object
Run code without the client reconnecting
82 83 84 85 86 |
# File 'lib/redis.rb', line 82 def without_reconnect(&block) wrapped do @client.without_reconnect(&block) end end |
#wrapped ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/redis.rb', line 54 def wrapped with_retry do with_thread_safety do yield end end end |
#zadd(key, score, member) ⇒ Object
Add a member to a sorted set, or update its score if it already exists.
533 534 535 536 537 |
# File 'lib/redis.rb', line 533 def zadd(key, score, member) wrapped do _bool @client.call [:zadd, key, score, member] end end |
#zcard(key) ⇒ Object
Get the number of members in a sorted set.
562 563 564 565 566 |
# File 'lib/redis.rb', line 562 def zcard(key) wrapped 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.
594 595 596 597 598 |
# File 'lib/redis.rb', line 594 def zcount(key, start, stop) wrapped do @client.call [:zcount, key, start, stop] end end |
#zincrby(key, increment, member) ⇒ Object
Increment the score of a member in a sorted set.
555 556 557 558 559 |
# File 'lib/redis.rb', line 555 def zincrby(key, increment, member) wrapped 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.
657 658 659 660 661 662 663 664 665 666 |
# File 'lib/redis.rb', line 657 def zinterstore(destination, keys, = {}) command = CommandOptions.new() do |c| c.splat :weights c.value :aggregate end wrapped 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.
569 570 571 572 573 574 575 576 577 578 |
# File 'lib/redis.rb', line 569 def zrange(key, start, stop, = {}) command = CommandOptions.new() do |c| c.bool :withscores c.bool :with_scores end wrapped 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.
581 582 583 584 585 586 587 588 589 590 591 |
# File 'lib/redis.rb', line 581 def zrangebyscore(key, min, max, = {}) command = CommandOptions.new() do |c| c.splat :limit c.bool :withscores c.bool :with_scores end wrapped 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.
540 541 542 543 544 |
# File 'lib/redis.rb', line 540 def zrank(key, member) wrapped do @client.call [:zrank, key, member] end end |
#zrem(key, member) ⇒ Object
Remove a member from a sorted set.
649 650 651 652 653 |
# File 'lib/redis.rb', line 649 def zrem(key, member) wrapped 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.
635 636 637 638 639 |
# File 'lib/redis.rb', line 635 def zremrangebyrank(key, start, stop) wrapped 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.
628 629 630 631 632 |
# File 'lib/redis.rb', line 628 def zremrangebyscore(key, min, max) wrapped 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.
602 603 604 605 606 607 608 609 610 611 |
# File 'lib/redis.rb', line 602 def zrevrange(key, start, stop, = {}) command = CommandOptions.new() do |c| c.bool :withscores c.bool :with_scores end wrapped 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.
615 616 617 618 619 620 621 622 623 624 625 |
# File 'lib/redis.rb', line 615 def zrevrangebyscore(key, max, min, = {}) command = CommandOptions.new() do |c| c.splat :limit c.bool :withscores c.bool :with_scores end wrapped 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.
548 549 550 551 552 |
# File 'lib/redis.rb', line 548 def zrevrank(key, member) wrapped do @client.call [:zrevrank, key, member] end end |
#zscore(key, member) ⇒ Object
Get the score associated with the given member in a sorted set.
642 643 644 645 646 |
# File 'lib/redis.rb', line 642 def zscore(key, member) wrapped 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.
669 670 671 672 673 674 675 676 677 678 |
# File 'lib/redis.rb', line 669 def zunionstore(destination, keys, = {}) command = CommandOptions.new() do |c| c.splat :weights c.value :aggregate end wrapped do @client.call [:zunionstore, destination, keys.size, *(keys + command.to_a)] end end |