Desconecta o cliente do servidor Redis

Sintaxe

oRedisAfter := oReisdClient:Disconnect()

Parâmetros

Nenhum parâmetro.

Retorno

NomeTipoDescriçãoObservações
oRedisAfter
objeto tRedisClientCópia do objeto sobre o qual foi feita a chamada de ::Connect, já afetado pela tentativa de conexão.*

Isto permite acoplar outros métodos e propriedades à chamada de ::Disconnect()
   Por exemplo,
    oRedisClient:Disconnect():lConnected

   testará se a conexão foi bem sucedida, sem a necessidade de uma consulta à parte a ::lConnected sobre o objeto oRedisClient.

Observações

  • O método ::Disconnect() permite que um cliente interrompa sua conexão com um servidor Redis. Isto permite que sejam liberados recursos alocados para conexão tanto no servidor quanto no cliente.

    Deste modo, o servidor Redis pode atender a outros clientes.

    Naturalmente, uma desconexão só faz sentido caso o cliente tenha de fato conseguido se conectar.

Exemplos

Exemplo 1 – Chamada simples a ::Disconnect()

#include 'protheus.ch'
   
#define STD_REDIS_PORT 6379
#define DEFAULT_REDIS_AUTH ""
  
// Setup Redis
Static cRedisHost := "localhost"
   
User Function WoAuth()
  Local cMsg := ''
  
  // Creation of client object
  oRedisCli := tRedisClient():New()
  
  // Connection to the server, using default port 6379
  oRedisCli:Connect(cRedisHost, STD_REDIS_PORT)
  
  If oRedisCli:lConnected
    cMsg := "Successful connection to the server " + cRedisHost + ", at port "
    cMsg += cValToChar(STD_REDIS_PORT) + ", with authentication '" + DEFAULT_REDIS_AUTH + "'"
    ConOut(cMsg)
  
    oRedisCli:Disconnect()
    ConOut("The client disconnected from the server.")
    Return .T.
  EndIf
  
  cMsg := "Could not connect to the server " + cRedisHost + ", at port "
  cMsg += cValToChar(STD_REDIS_PORT) + ", with authentication '" + DEFAULT_REDIS_AUTH + "'"
  ConOut(cMsg)
  
Return .F.

Exemplo 2 – Consulta a ::lConnected associada a uma chamada de ::Disconnect

#include 'protheus.ch'
  
#define STD_REDIS_PORT 6379
#define DEFAULT_REDIS_AUTH ""
 
// Setup Redis
Static cRedisHost := "localhost"
  
User Function tDiscon()
  Local cMsg := ''
 
  // Creation of client object
  oRedisCli := tRedisClient():New()
 
  // Connection to the server, using default port 6379
  oRedisCli:Connect(cRedisHost, STD_REDIS_PORT)
 
  If oRedisCli:lConnected
    cMsg := "Successful connection to the server " + cRedisHost + ", at port "
    cMsg += cValToChar(STD_REDIS_PORT) + ", with authentication '" + DEFAULT_REDIS_AUTH + "'"
    ConOut(cMsg)
 
    If .Not. oRedisCli:Disconnect():lOk
      ConOut("The client could not disconnect from the server.")
      ConOut("*** This task will be aborted anyway ***")
      Return .F. 
    EndIf 
    
    ConOut("The client disconnected from the server.")
    Return .T.
  EndIf
 
  cMsg := "Could not connect to the server " + cRedisHost + ", at port "
  cMsg += cValToChar(STD_REDIS_PORT) + ", with authentication '" + DEFAULT_REDIS_AUTH + "'"
  ConOut(cMsg)
 
Return .F.