class Jylis::Connection

A connection to the database.

Public Class Methods

new(server_uri) click to toggle source

@param server_uri [URI, String] uri of the server to connect to

@raise [UnsupportedSchemaError] if the server URI schema is not supported @raise [HostMissingError] if the host is missing from the server URI

# File lib/jylis-rb/connection.rb, line 15
def initialize(server_uri)
  server_uri = URI.parse(server_uri) unless server_uri.is_a?(URI)

  unless server_uri.scheme.downcase == "jylis"
    raise UnsupportedSchemaError.new(
      "#{server_uri.scheme} is not a supported schema"
    )
  end

  unless server_uri.host
    raise HostMissingError.new("No host specified")
  end

  @server_host = server_uri.host
  @server_port = server_uri.port || 6379
  @connection  = Hiredis::Connection.new

  connect
end

Public Instance Methods

connected?() click to toggle source

@return [Boolean] true if a connection to the server is established

# File lib/jylis-rb/connection.rb, line 36
def connected?
  @connection.connected?
end
disconnect() click to toggle source

Disconnect from the server.

# File lib/jylis-rb/connection.rb, line 48
def disconnect
  @connection.disconnect
end
gcount() click to toggle source

GCOUNT functions

@return [Jylis::DataType::GCOUNT]

# File lib/jylis-rb/connection.rb, line 87
def gcount
  @gcount ||= Jylis::DataType::GCOUNT.new(self)
end
mvreg() click to toggle source

MVREG functions

@return [Jylis::DataType::MVREG]

# File lib/jylis-rb/connection.rb, line 101
def mvreg
  @mvreg ||= Jylis::DataType::MVREG.new(self)
end
pncount() click to toggle source

PNCOUNT functions

@return [Jylis::DataType::PNCOUNT]

# File lib/jylis-rb/connection.rb, line 94
def pncount
  @pncount ||= Jylis::DataType::PNCOUNT.new(self)
end
query(*args) click to toggle source

Make a query to the database.

@param args data type function args. Can be an args list or array.

@return [Array] query response

@see jemc.github.io/jylis/docs/types/

# File lib/jylis-rb/connection.rb, line 59
def query(*args)
  if args.count == 1 && args.first.is_a?(Array)
    args = *args.first
  end

  @connection.write(args)
  @connection.read
end
reconnect() click to toggle source

Reconnect to the server.

# File lib/jylis-rb/connection.rb, line 41
def reconnect
  disconnect if connected?

  connect
end
tlog() click to toggle source

TLOG functions

@return [Jylis::DataType::TLOG]

# File lib/jylis-rb/connection.rb, line 80
def tlog
  @tlog ||= Jylis::DataType::TLOG.new(self)
end
treg() click to toggle source

TREG functions

@return [Jylis::DataType::TREG]

# File lib/jylis-rb/connection.rb, line 73
def treg
  @treg ||= Jylis::DataType::TREG.new(self)
end
ujson() click to toggle source

UJSON functions

@return [Jylis::DataType::UJSON]

# File lib/jylis-rb/connection.rb, line 108
def ujson
  @ujson ||= Jylis::DataType::UJSON.new(self)
end

Private Instance Methods

connect() click to toggle source

Connect to the server.

# File lib/jylis-rb/connection.rb, line 115
def connect
  @connection.connect(@server_host, @server_port)
end