class MiniRedis

Overview

A light-weight low-level Redis client.

Defined in:

mini_redis/errors.cr
mini_redis/pool.cr
mini_redis/value.cr
mini_redis.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(uri : URI = URI.parse("redis://localhost:6379"), logger : Logger? = nil, logger_severity : Logger::Severity = Logger::Severity::INFO, dns_timeout : Time::Span? = 5.seconds, connect_timeout : Time::Span? = 5.seconds, read_timeout : Time::Span? = nil, write_timeout : Time::Span? = 5.seconds) #

Initialize with Redis uri and optional logger. The logger would log outcoming commands with logger_severity level.

redis = MiniRedis.new(URI.parse(ENV["REDIS_URL"]), logger: Logger.new(STDOUT))

[View source]
def self.new(socket : Socket, logger : Logger? = nil, logger_severity : Logger::Severity = Logger::Severity::INFO) #

Initialize with raw Crystal Socket and optional logger. The logger would log outcoming commands with logger_severity level.


[View source]

Instance Method Detail

def ==(other : self) #

def close #

Close the underlying socket.


[View source]
def hash(hasher) #

def logger : Logger? #

The logger which logs commands.


[View source]
def logger=(logger : Logger?) #

The logger which logs commands.


[View source]
def logger_severity : Logger::Severity #

The #logger severity.


[View source]
def logger_severity=(logger_severity : Logger::Severity) #

The #logger severity.


[View source]
def pipeline(&block : self -> ) : Array(Value) #

Yield self, accumulate requests and then flush them all in one moment. See Pipelining docs.

It returns an Array of Values.

response = redis.pipeline do |pipe|
  # WARNING: Do not try to access its return value while
  # within the pipeline block. See the explaination below
  pipe.send("PING")
end

pp response # => Array([MiniRedis::Value(@raw="PONG")])

WARNING: #send returns an uninitalized Value when in pipeline mode. Trying to access it would crash the program. Use #pipeline? if you want to be sure.

# When you're not sure about the `redis` type...

# Wrong ✖️
puts redis.send("PING") # May crash with `Invalid memory access`

# Right ✔️
unless redis.pipeline?
  puts redis.send("PING")
end

[View source]
def pipeline? : Bool #

Whether is current connection in pipeline mode. See #pipeline.


[View source]
def send(commands : Enumerable) : Value #

Send the commands marshalled according to the Redis Protocol Specification.

redis.send("PING")       # MiniRedis::Value(@raw="PONG")
redis.send("GET", "foo") # MiniRedis::Value(@raw=Bytes)

[View source]
def send(*commands) : Value? #

Send the commands marshalled according to the Redis Protocol Specification.

redis.send("PING")       # MiniRedis::Value(@raw="PONG")
redis.send("GET", "foo") # MiniRedis::Value(@raw=Bytes)

[View source]
def socket : Socket #

The underlying socket.


[View source]
def transaction(&block : self -> ) : Value #

Send "MULTI" command, yield self and then send "EXEC" command. See Transactions docs.

It returns a Value containing an Array of Values.

response = redis.transaction do |tx|
  pp tx.send("SET", "foo", "bar") # => MiniRedis::Value(@raw="QUEUED")
end

pp response # => MiniRedis::Value(@raw=[MiniRedis::Value(@raw=Bytes)])

[View source]
def transaction? : Bool #

Whether is current connection in transaction mode. See #transaction.


[View source]