Class: Redis
- Inherits:
-
Object
- Object
- Redis
- 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.0"
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 ⇒ 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.
-
#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.
-
#without_reconnect(&block) ⇒ Object
Run code without the client reconnecting.
-
#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
#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( = {}) @client = Client.new() if [: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
1066 1067 1068 1069 1070 |
# File 'lib/redis.rb', line 1066 def method_missing(command, *args) synchronize do @client.call(command, *args) end end |
Instance Attribute Details
#client ⇒ Object (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( = {}) = .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
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(, trace = caller[0]) $stderr.puts "\n#{} (in #{trace})" end |
Instance Method Details
#[](key) ⇒ Object
817 818 819 |
# File 'lib/redis.rb', line 817 def [](key) get(key) end |
#[]=(key, value) ⇒ Object
821 822 823 |
# File 'lib/redis.rb', line 821 def []=(key,value) set(key, value) end |
#append(key, value) ⇒ Object
Append a value to a key.
183 184 185 186 187 |
# File 'lib/redis.rb', line 183 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 |
#bgrewriteaof ⇒ Object
Asynchronously rewrite the append-only file.
141 142 143 144 145 |
# File 'lib/redis.rb', line 141 def bgrewriteaof synchronize do @client.call(:bgrewriteaof) end end |
#bgsave ⇒ Object
Asynchronously save the dataset to disk.
134 135 136 137 138 |
# File 'lib/redis.rb', line 134 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.
376 377 378 379 380 |
# File 'lib/redis.rb', line 376 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.
383 384 385 386 387 |
# File 'lib/redis.rb', line 383 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.
391 392 393 394 395 |
# File 'lib/redis.rb', line 391 def brpoplpush(source, destination, timeout) synchronize do @client.call_without_timeout(:brpoplpush, source, destination, timeout) end end |
#config(action, *args) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/redis.rb', line 100 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 |
#dbsize ⇒ Object
Return the number of keys in the selected database.
278 279 280 281 282 |
# File 'lib/redis.rb', line 278 def dbsize synchronize do @client.call(:dbsize) end end |
#debug(*args) ⇒ Object
804 805 806 807 808 |
# File 'lib/redis.rb', line 804 def debug(*args) synchronize do @client.call(:debug, *args) end end |
#decr(key) ⇒ Object
Decrement the integer value of a key by one.
915 916 917 918 919 |
# File 'lib/redis.rb', line 915 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.
922 923 924 925 926 |
# File 'lib/redis.rb', line 922 def decrby(key, decrement) synchronize do @client.call(:decrby, key, decrement) end end |
#del(*keys) ⇒ Object
Delete a key.
672 673 674 675 676 |
# File 'lib/redis.rb', line 672 def del(*keys) synchronize do @client.call(:del, *keys) end end |
#discard ⇒ Object
Discard all commands issued after MULTI.
784 785 786 787 788 |
# File 'lib/redis.rb', line 784 def discard synchronize do @client.call(:discard) end end |
#echo(value) ⇒ Object
Echo the given string.
257 258 259 260 261 |
# File 'lib/redis.rb', line 257 def echo(value) synchronize do @client.call(:echo, value) end end |
#exec ⇒ Object
Execute all commands issued after MULTI.
988 989 990 991 992 |
# File 'lib/redis.rb', line 988 def exec synchronize do @client.call(:exec) end end |
#exists(key) ⇒ Object
Determine if a key exists.
285 286 287 288 289 |
# File 'lib/redis.rb', line 285 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.
693 694 695 696 697 |
# File 'lib/redis.rb', line 693 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.
714 715 716 717 718 |
# File 'lib/redis.rb', line 714 def expireat(key, unix_time) synchronize do _bool @client.call(:expireat, key, unix_time) end end |
#flushall ⇒ Object
Remove all keys from all databases.
120 121 122 123 124 |
# File 'lib/redis.rb', line 120 def flushall synchronize do @client.call(:flushall) end end |
#flushdb ⇒ Object
Remove all keys from the current database.
113 114 115 116 117 |
# File 'lib/redis.rb', line 113 def flushdb synchronize do @client.call(:flushdb) end end |
#get(key) ⇒ Object
Get the value of a key.
148 149 150 151 152 |
# File 'lib/redis.rb', line 148 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.
155 156 157 158 159 |
# File 'lib/redis.rb', line 155 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.
162 163 164 165 166 |
# File 'lib/redis.rb', line 162 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.
169 170 171 172 173 |
# File 'lib/redis.rb', line 169 def getset(key, value) synchronize do @client.call(:getset, key, value) end end |
#hdel(key, field) ⇒ Object
Delete a hash field.
223 224 225 226 227 |
# File 'lib/redis.rb', line 223 def hdel(key, field) synchronize do @client.call(:hdel, key, field) end end |
#hexists(key, field) ⇒ Object
Determine if a hash field exists.
791 792 793 794 795 |
# File 'lib/redis.rb', line 791 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.
216 217 218 219 220 |
# File 'lib/redis.rb', line 216 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.
203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/redis.rb', line 203 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.
777 778 779 780 781 |
# File 'lib/redis.rb', line 777 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.
230 231 232 233 234 |
# File 'lib/redis.rb', line 230 def hkeys(key) synchronize do @client.call(:hkeys, key) end end |
#hlen(key) ⇒ Object
Get the number of fields in a hash.
763 764 765 766 767 |
# File 'lib/redis.rb', line 763 def hlen(key) synchronize do @client.call(:hlen, key) end end |
#hmget(key, *fields) ⇒ Object
Get the values of all the given hash fields.
746 747 748 749 750 |
# File 'lib/redis.rb', line 746 def hmget(key, *fields) synchronize do @client.call(:hmget, key, *fields) end end |
#hmset(key, *attrs) ⇒ Object
Set multiple hash fields to multiple values.
735 736 737 738 739 |
# File 'lib/redis.rb', line 735 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.
721 722 723 724 725 |
# File 'lib/redis.rb', line 721 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.
728 729 730 731 732 |
# File 'lib/redis.rb', line 728 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.
770 771 772 773 774 |
# File 'lib/redis.rb', line 770 def hvals(key) synchronize do @client.call(:hvals, key) end end |
#id ⇒ Object
1054 1055 1056 1057 1058 |
# File 'lib/redis.rb', line 1054 def id synchronize do @client.id end end |
#incr(key) ⇒ Object
Increment the integer value of a key by one.
901 902 903 904 905 |
# File 'lib/redis.rb', line 901 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.
908 909 910 911 912 |
# File 'lib/redis.rb', line 908 def incrby(key, increment) synchronize do @client.call(:incrby, key, increment) end end |
#info ⇒ Object
Get information and statistics about the server.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/redis.rb', line 88 def info synchronize do reply = @client.call(:info) if reply.kind_of?(String) Hash[*reply.split(/:|\r\n/).grep(/^[^#]/)] else reply end end end |
#inspect ⇒ Object
1060 1061 1062 1063 1064 |
# File 'lib/redis.rb', line 1060 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.
237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/redis.rb', line 237 def keys(pattern = "*") synchronize 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.
271 272 273 274 275 |
# File 'lib/redis.rb', line 271 def lastsave synchronize do @client.call(:lastsave) end end |
#lindex(key, index) ⇒ Object
Get an element from a list by its index.
313 314 315 316 317 |
# File 'lib/redis.rb', line 313 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.
320 321 322 323 324 |
# File 'lib/redis.rb', line 320 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.
292 293 294 295 296 |
# File 'lib/redis.rb', line 292 def llen(key) synchronize do @client.call(:llen, key) end end |
#lpop(key) ⇒ Object
Remove and get the first element in a list.
405 406 407 408 409 |
# File 'lib/redis.rb', line 405 def lpop(key) synchronize do @client.call(:lpop, key) end end |
#lpush(key, value) ⇒ Object
Prepend a value to a list.
355 356 357 358 359 |
# File 'lib/redis.rb', line 355 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.
362 363 364 365 366 |
# File 'lib/redis.rb', line 362 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.
299 300 301 302 303 |
# File 'lib/redis.rb', line 299 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.
334 335 336 337 338 |
# File 'lib/redis.rb', line 334 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.
327 328 329 330 331 |
# File 'lib/redis.rb', line 327 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.
306 307 308 309 310 |
# File 'lib/redis.rb', line 306 def ltrim(key, start, stop) synchronize do @client.call(:ltrim, key, start, stop) end end |
#mapped_hmget(key, *fields) ⇒ Object
752 753 754 755 756 757 758 759 760 |
# File 'lib/redis.rb', line 752 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
741 742 743 |
# File 'lib/redis.rb', line 741 def mapped_hmset(key, hash) hmset(key, *hash.to_a.flatten) end |
#mapped_mget(*keys) ⇒ Object
875 876 877 878 879 880 881 882 883 |
# File 'lib/redis.rb', line 875 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
860 861 862 |
# File 'lib/redis.rb', line 860 def mapped_mset(hash) mset(*hash.to_a.flatten) end |
#mapped_msetnx(hash) ⇒ Object
871 872 873 |
# File 'lib/redis.rb', line 871 def mapped_msetnx(hash) msetnx(*hash.to_a.flatten) end |
#mget(*keys) ⇒ Object
Get the values of all the given keys.
176 177 178 179 180 |
# File 'lib/redis.rb', line 176 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.
798 799 800 801 802 |
# File 'lib/redis.rb', line 798 def monitor(&block) synchronize do @client.call_loop(:monitor, &block) end end |
#move(key, db) ⇒ Object
Move a key to another database.
658 659 660 661 662 |
# File 'lib/redis.rb', line 658 def move(key, db) synchronize do _bool @client.call(:move, key, db) end end |
#mset(*args) ⇒ Object
Set multiple keys to multiple values.
854 855 856 857 858 |
# File 'lib/redis.rb', line 854 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.
865 866 867 868 869 |
# File 'lib/redis.rb', line 865 def msetnx(*args) synchronize do @client.call(:msetnx, *args) end end |
#multi ⇒ Object
Mark the start of a transaction block.
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 |
# File 'lib/redis.rb', line 995 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 |
#persist(key) ⇒ Object
Remove the expiration from a key.
700 701 702 703 704 |
# File 'lib/redis.rb', line 700 def persist(key) synchronize do _bool @client.call(:persist, key) end end |
#ping ⇒ Object
Ping the server.
264 265 266 267 268 |
# File 'lib/redis.rb', line 264 def ping synchronize do @client.call(:ping) end end |
#pipelined(options = {}) ⇒ Object
961 962 963 964 965 966 967 968 969 970 971 |
# File 'lib/redis.rb', line 961 def pipelined( = {}) synchronize 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.
1048 1049 1050 1051 1052 |
# File 'lib/redis.rb', line 1048 def psubscribe(*channels, &block) synchronize do subscription(:psubscribe, channels, block) end end |
#publish(channel, message) ⇒ Object
Post a message to a channel.
1012 1013 1014 1015 1016 |
# File 'lib/redis.rb', line 1012 def publish(channel, ) synchronize do @client.call(:publish, channel, ) end end |
#punsubscribe(*channels) ⇒ Object
Stop listening for messages posted to channels matching the given patterns.
1033 1034 1035 1036 1037 1038 |
# File 'lib/redis.rb', line 1033 def punsubscribe(*channels) synchronize do raise RuntimeError, "Can't unsubscribe if not subscribed." unless subscribed? @client.punsubscribe(*channels) end end |
#quit ⇒ Object
Close the connection.
936 937 938 939 940 941 942 943 944 945 |
# File 'lib/redis.rb', line 936 def quit synchronize do begin @client.call(:quit) rescue Errno::ECONNRESET ensure @client.disconnect end end end |
#randomkey ⇒ Object
Return a random key from the keyspace.
250 251 252 253 254 |
# File 'lib/redis.rb', line 250 def randomkey synchronize do @client.call(:randomkey) end end |
#rename(old_name, new_name) ⇒ Object
Rename a key.
679 680 681 682 683 |
# File 'lib/redis.rb', line 679 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.
686 687 688 689 690 |
# File 'lib/redis.rb', line 686 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.
369 370 371 372 373 |
# File 'lib/redis.rb', line 369 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.
398 399 400 401 402 |
# File 'lib/redis.rb', line 398 def rpoplpush(source, destination) synchronize do @client.call(:rpoplpush, source, destination) end end |
#rpush(key, value) ⇒ Object
Append a value to a list.
341 342 343 344 345 |
# File 'lib/redis.rb', line 341 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.
348 349 350 351 352 |
# File 'lib/redis.rb', line 348 def rpushx(key, value) synchronize do @client.call(:rpushx, key, value) end end |
#sadd(key, value) ⇒ Object
Add a member to a set.
426 427 428 429 430 |
# File 'lib/redis.rb', line 426 def sadd(key, value) synchronize do _bool @client.call(:sadd, key, value) end end |
#save ⇒ Object
Synchronously save the dataset to disk.
127 128 129 130 131 |
# File 'lib/redis.rb', line 127 def save synchronize do @client.call(:save) end end |
#scard(key) ⇒ Object
Get the number of members in a set.
454 455 456 457 458 |
# File 'lib/redis.rb', line 454 def scard(key) synchronize do @client.call(:scard, key) end end |
#sdiff(*keys) ⇒ Object
Subtract multiple sets.
489 490 491 492 493 |
# File 'lib/redis.rb', line 489 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.
496 497 498 499 500 |
# File 'lib/redis.rb', line 496 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.
826 827 828 829 830 |
# File 'lib/redis.rb', line 826 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.
833 834 835 836 837 |
# File 'lib/redis.rb', line 833 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.
840 841 842 843 844 |
# File 'lib/redis.rb', line 840 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.
665 666 667 668 669 |
# File 'lib/redis.rb', line 665 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.
847 848 849 850 851 |
# File 'lib/redis.rb', line 847 def setrange(key, offset, value) synchronize do @client.call(:setrange, key, offset, value) end end |
#shutdown ⇒ Object
Synchronously save the dataset to disk and then shut down the server.
948 949 950 951 952 |
# File 'lib/redis.rb', line 948 def shutdown synchronize do @client.call(:shutdown) end end |
#sinter(*keys) ⇒ Object
Intersect multiple sets.
461 462 463 464 465 |
# File 'lib/redis.rb', line 461 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.
468 469 470 471 472 |
# File 'lib/redis.rb', line 468 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.
419 420 421 422 423 |
# File 'lib/redis.rb', line 419 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.
955 956 957 958 959 |
# File 'lib/redis.rb', line 955 def slaveof(host, port) synchronize do @client.call(:slaveof, host, port) end end |
#smembers(key) ⇒ Object
Get all the members in a set.
412 413 414 415 416 |
# File 'lib/redis.rb', line 412 def smembers(key) synchronize do @client.call(:smembers, key) end end |
#smove(source, destination, member) ⇒ Object
Move a member from one set to another.
440 441 442 443 444 |
# File 'lib/redis.rb', line 440 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.
886 887 888 889 890 891 892 893 894 895 896 897 898 |
# File 'lib/redis.rb', line 886 def sort(key, = {}) command = CommandOptions.new() 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.
447 448 449 450 451 |
# File 'lib/redis.rb', line 447 def spop(key) synchronize do @client.call(:spop, key) end end |
#srandmember(key) ⇒ Object
Get a random member from a set.
503 504 505 506 507 |
# File 'lib/redis.rb', line 503 def srandmember(key) synchronize do @client.call(:srandmember, key) end end |
#srem(key, value) ⇒ Object
Remove a member from a set.
433 434 435 436 437 |
# File 'lib/redis.rb', line 433 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.
196 197 198 199 200 |
# File 'lib/redis.rb', line 196 def strlen(key) synchronize do @client.call(:strlen, key) end end |
#subscribe(*channels, &block) ⇒ Object
Listen for messages published to the given channels.
1041 1042 1043 1044 1045 |
# File 'lib/redis.rb', line 1041 def subscribe(*channels, &block) synchronize do subscription(:subscribe, channels, block) end end |
#subscribed? ⇒ Boolean
1018 1019 1020 1021 1022 |
# File 'lib/redis.rb', line 1018 def subscribed? synchronize do @client.kind_of? SubscribedClient end end |
#substr(key, start, stop) ⇒ Object
189 190 191 192 193 |
# File 'lib/redis.rb', line 189 def substr(key, start, stop) synchronize do @client.call(:substr, key, start, stop) end end |
#sunion(*keys) ⇒ Object
Add multiple sets.
475 476 477 478 479 |
# File 'lib/redis.rb', line 475 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.
482 483 484 485 486 |
# File 'lib/redis.rb', line 482 def sunionstore(destination, *keys) synchronize do @client.call(:sunionstore, destination, *keys) end end |
#sync ⇒ Object
Internal command used for replication.
811 812 813 814 815 |
# File 'lib/redis.rb', line 811 def sync synchronize do @client.call(:sync) end end |
#ttl(key) ⇒ Object
Get the time to live for a key.
707 708 709 710 711 |
# File 'lib/redis.rb', line 707 def ttl(key) synchronize do @client.call(:ttl, key) end end |
#type(key) ⇒ Object
Determine the type stored at key.
929 930 931 932 933 |
# File 'lib/redis.rb', line 929 def type(key) synchronize do @client.call(:type, key) end end |
#unsubscribe(*channels) ⇒ Object
Stop listening for messages posted to the given channels.
1025 1026 1027 1028 1029 1030 |
# File 'lib/redis.rb', line 1025 def unsubscribe(*channels) synchronize do raise RuntimeError, "Can't unsubscribe if not subscribed." unless subscribed? @client.unsubscribe(*channels) end end |
#unwatch ⇒ Object
Forget about all watched keys.
981 982 983 984 985 |
# File 'lib/redis.rb', line 981 def unwatch synchronize do @client.call(:unwatch) end end |
#watch(*keys) ⇒ Object
Watch the given keys to determine execution of the MULTI/EXEC block.
974 975 976 977 978 |
# File 'lib/redis.rb', line 974 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.
510 511 512 513 514 |
# File 'lib/redis.rb', line 510 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.
539 540 541 542 543 |
# File 'lib/redis.rb', line 539 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.
571 572 573 574 575 |
# File 'lib/redis.rb', line 571 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.
532 533 534 535 536 |
# File 'lib/redis.rb', line 532 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.
634 635 636 637 638 639 640 641 642 643 |
# File 'lib/redis.rb', line 634 def zinterstore(destination, keys, = {}) command = CommandOptions.new() 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.
546 547 548 549 550 551 552 553 554 555 |
# File 'lib/redis.rb', line 546 def zrange(key, start, stop, = {}) command = CommandOptions.new() 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.
558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/redis.rb', line 558 def zrangebyscore(key, min, max, = {}) command = CommandOptions.new() 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.
517 518 519 520 521 |
# File 'lib/redis.rb', line 517 def zrank(key, member) synchronize do @client.call(:zrank, key, member) end end |
#zrem(key, member) ⇒ Object
Remove a member from a sorted set.
626 627 628 629 630 |
# File 'lib/redis.rb', line 626 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.
612 613 614 615 616 |
# File 'lib/redis.rb', line 612 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.
605 606 607 608 609 |
# File 'lib/redis.rb', line 605 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.
579 580 581 582 583 584 585 586 587 588 |
# File 'lib/redis.rb', line 579 def zrevrange(key, start, stop, = {}) command = CommandOptions.new() 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.
592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/redis.rb', line 592 def zrevrangebyscore(key, max, min, = {}) command = CommandOptions.new() 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.
525 526 527 528 529 |
# File 'lib/redis.rb', line 525 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.
619 620 621 622 623 |
# File 'lib/redis.rb', line 619 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.
646 647 648 649 650 651 652 653 654 655 |
# File 'lib/redis.rb', line 646 def zunionstore(destination, keys, = {}) command = CommandOptions.new() do |c| c.splat :weights c.value :aggregate end synchronize do @client.call(:zunionstore, destination, keys.size, *(keys + command.to_a)) end end |