Class: Redis

Inherits:
Object
  • Object
show all
Includes:
Commands
Defined in:
lib/redis.rb,
lib/redis/client.rb,
lib/redis/errors.rb,
lib/redis/version.rb,
lib/redis/commands.rb,
lib/redis/pipeline.rb,
lib/redis/hash_ring.rb,
lib/redis/subscribe.rb,
lib/redis/distributed.rb,
lib/redis/commands/geo.rb,
lib/redis/commands/keys.rb,
lib/redis/commands/sets.rb,
lib/redis/commands/lists.rb,
lib/redis/commands/hashes.rb,
lib/redis/commands/pubsub.rb,
lib/redis/commands/server.rb,
lib/redis/commands/bitmaps.rb,
lib/redis/commands/cluster.rb,
lib/redis/commands/streams.rb,
lib/redis/commands/strings.rb,
lib/redis/commands/scripting.rb,
lib/redis/commands/connection.rb,
lib/redis/commands/sorted_sets.rb,
lib/redis/commands/transactions.rb,
lib/redis/commands/hyper_log_log.rb

Defined Under Namespace

Modules: Commands, Connection Classes: BaseConnectionError, BaseError, CannotConnectError, Client, CommandError, ConnectionError, Distributed, Future, FutureNotReady, HashRing, InheritedError, InvalidClientOptionError, MultiConnection, MultiFuture, OutOfMemoryError, PermissionError, PipelinedConnection, ProtocolError, ReadOnlyError, SubscribedClient, Subscription, SubscriptionError, TimeoutError, WrongTypeError

Constant Summary collapse

BASE_PATH =
__dir__
Deprecated =
Class.new(StandardError)
SERVER_URL_OPTIONS =
%i(url host port path).freeze
VERSION =
'5.3.0'

Constants included from Commands

Commands::Boolify, Commands::BoolifySet, Commands::Floatify, Commands::FloatifyPairs, Commands::Hashify, Commands::HashifyClusterNodeInfo, Commands::HashifyClusterNodes, Commands::HashifyClusterSlaves, Commands::HashifyClusterSlots, Commands::HashifyInfo, Commands::HashifyStreamAutoclaim, Commands::HashifyStreamAutoclaimJustId, Commands::HashifyStreamEntries, Commands::HashifyStreamPendingDetails, Commands::HashifyStreamPendings, Commands::HashifyStreams, Commands::Noop, Commands::Pairify

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commands

#call, #sentinel

Methods included from Commands::Transactions

#discard, #exec, #multi, #unwatch, #watch

Methods included from Commands::Strings

#append, #decr, #decrby, #get, #getdel, #getex, #getrange, #getset, #incr, #incrby, #incrbyfloat, #mapped_mget, #mapped_mset, #mapped_msetnx, #mget, #mset, #msetnx, #psetex, #set, #setex, #setnx, #setrange, #strlen

Methods included from Commands::Streams

#xack, #xadd, #xautoclaim, #xclaim, #xdel, #xgroup, #xinfo, #xlen, #xpending, #xrange, #xread, #xreadgroup, #xrevrange, #xtrim

Methods included from Commands::SortedSets

#bzmpop, #bzpopmax, #bzpopmin, #zadd, #zcard, #zcount, #zdiff, #zdiffstore, #zincrby, #zinter, #zinterstore, #zlexcount, #zmpop, #zmscore, #zpopmax, #zpopmin, #zrandmember, #zrange, #zrangebylex, #zrangebyscore, #zrangestore, #zrank, #zrem, #zremrangebyrank, #zremrangebyscore, #zrevrange, #zrevrangebylex, #zrevrangebyscore, #zrevrank, #zscan, #zscan_each, #zscore, #zunion, #zunionstore

Methods included from Commands::Sets

#sadd, #sadd?, #scard, #sdiff, #sdiffstore, #sinter, #sinterstore, #sismember, #smembers, #smismember, #smove, #spop, #srandmember, #srem, #srem?, #sscan, #sscan_each, #sunion, #sunionstore

Methods included from Commands::Server

#bgrewriteaof, #bgsave, #client, #config, #dbsize, #debug, #flushall, #flushdb, #info, #lastsave, #monitor, #save, #shutdown, #slaveof, #slowlog, #sync, #time

Methods included from Commands::Scripting

#eval, #evalsha, #script

Methods included from Commands::Pubsub

#psubscribe, #psubscribe_with_timeout, #publish, #pubsub, #punsubscribe, #spublish, #ssubscribe, #ssubscribe_with_timeout, #subscribe, #subscribe_with_timeout, #subscribed?, #sunsubscribe, #unsubscribe

Methods included from Commands::Lists

#blmove, #blmpop, #blpop, #brpop, #brpoplpush, #lindex, #linsert, #llen, #lmove, #lmpop, #lpop, #lpush, #lpushx, #lrange, #lrem, #lset, #ltrim, #rpop, #rpoplpush, #rpush, #rpushx

Methods included from Commands::Keys

#copy, #del, #dump, #exists, #exists?, #expire, #expireat, #expiretime, #keys, #migrate, #move, #object, #persist, #pexpire, #pexpireat, #pexpiretime, #pttl, #randomkey, #rename, #renamenx, #restore, #scan, #scan_each, #sort, #ttl, #type, #unlink

Methods included from Commands::HyperLogLog

#pfadd, #pfcount, #pfmerge

Methods included from Commands::Hashes

#hdel, #hexists, #hget, #hgetall, #hincrby, #hincrbyfloat, #hkeys, #hlen, #hmget, #hmset, #hrandfield, #hscan, #hscan_each, #hset, #hsetnx, #hvals, #mapped_hmget, #mapped_hmset

Methods included from Commands::Geo

#geoadd, #geodist, #geohash, #geopos, #georadius, #georadiusbymember

Methods included from Connection

drivers

Methods included from Commands::Cluster

#asking, #cluster

Methods included from Commands::Bitmaps

#bitcount, #bitop, #bitpos, #getbit, #setbit

Constructor Details

#initialize(options = {}) ⇒ Redis

Create a new client instance

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :url (String) — default: value of the environment variable REDIS_URL

    a Redis URL, for a TCP connection: ‘redis://:@[hostname]:/[db]` (password, port and database are optional), for a unix socket

    connection: `unix://[path to Redis socket]`. This overrides all other options.
    
  • :host (String) — default: "127.0.0.1"

    server hostname

  • :port (Integer) — default: 6379

    server port

  • :path (String)

    path to server socket (overrides host and port)

  • :timeout (Float) — default: 1.0

    timeout in seconds

  • :connect_timeout (Float) — default: same as timeout

    timeout for initial connect in seconds

  • :username (String)

    Username to authenticate against server

  • :password (String)

    Password to authenticate against server

  • :db (Integer) — default: 0

    Database to select after connect and on reconnects

  • :driver (Symbol)

    Driver to use, currently supported: ‘:ruby`, `:hiredis`

  • :id (String)

    ID for the client connection, assigns name to current connection by sending ‘CLIENT SETNAME`

  • :reconnect_attempts (Integer, Array<Integer, Float>)

    Number of attempts trying to connect, or a list of sleep duration between attempts.

  • :inherit_socket (Boolean) — default: false

    Whether to use socket in forked process or not

  • :name (String)

    The name of the server group to connect to.

  • :sentinels (Array)

    List of sentinels to contact



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/redis.rb', line 63

def initialize(options = {})
  @monitor = Monitor.new
  @options = options.dup
  @options[:reconnect_attempts] = 1 unless @options.key?(:reconnect_attempts)
  if ENV["REDIS_URL"] && SERVER_URL_OPTIONS.none? { |o| @options.key?(o) }
    @options[:url] = ENV["REDIS_URL"]
  end
  inherit_socket = @options.delete(:inherit_socket)
  @subscription_client = nil

  @client = initialize_client(@options)
  @client.inherit_socket! if inherit_socket
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Redis::Commands

Class Attribute Details

.raise_deprecationsObject

Returns the value of attribute raise_deprecations.



12
13
14
# File 'lib/redis.rb', line 12

def raise_deprecations
  @raise_deprecations
end

.silence_deprecationsObject

Returns the value of attribute silence_deprecations.



12
13
14
# File 'lib/redis.rb', line 12

def silence_deprecations
  @silence_deprecations
end

Class Method Details

.deprecate!(message) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/redis.rb', line 14

def deprecate!(message)
  unless silence_deprecations
    if raise_deprecations
      raise Deprecated, message
    else
      ::Kernel.warn(message)
    end
  end
end

Instance Method Details

#_clientObject



98
99
100
# File 'lib/redis.rb', line 98

def _client
  @client
end

#closeObject Also known as: disconnect!

Disconnect the client as quickly and silently as possible.



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

def close
  @client.close
  @subscription_client&.close
end

#connected?Boolean

Test whether or not the client is connected

Returns:

  • (Boolean)


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

def connected?
  @client.connected? || @subscription_client&.connected?
end

#connectionObject



122
123
124
125
126
127
128
129
130
# File 'lib/redis.rb', line 122

def connection
  {
    host: @client.host,
    port: @client.port,
    db: @client.db,
    id: id,
    location: "#{@client.host}:#{@client.port}"
  }
end

#dupObject



118
119
120
# File 'lib/redis.rb', line 118

def dup
  self.class.new(@options)
end

#idObject



110
111
112
# File 'lib/redis.rb', line 110

def id
  @client.id || @client.server_url
end

#inspectObject



114
115
116
# File 'lib/redis.rb', line 114

def inspect
  "#<Redis client v#{Redis::VERSION} for #{id}>"
end

#pipelined(exception: true) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/redis.rb', line 102

def pipelined(exception: true)
  synchronize do |client|
    client.pipelined(exception: exception) do |raw_pipeline|
      yield PipelinedConnection.new(raw_pipeline, exception: exception)
    end
  end
end

#with {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Redis)

    the object that the method was called on



94
95
96
# File 'lib/redis.rb', line 94

def with
  yield self
end

#without_reconnect(&block) ⇒ Object

Run code without the client reconnecting



78
79
80
# File 'lib/redis.rb', line 78

def without_reconnect(&block)
  @client.disable_reconnection(&block)
end