Connection

class zcached.Connection(host, port, connection_attempts=3, reconnect=True, timeout_limit=15, buffer_size=2048)[source]

Bases: object

An object to establish and manage a connection with the database server.

Parameters:
  • host (str) – The host address of the server to connect to.

  • port (int) – The port number of the server to connect to.

  • buffer_size (int) – The size of the buffer for receiving data from the server, in bytes. Larger values for buff_size may allow for more data to be received in a single operation, while smaller values can be more memory-efficient but slower.

  • connection_attempts (int) – The maximum number of attempts to establish a connection with the server. If the maximum number of attempts is exceeded, an error will be raised.

  • reconnect (bool) – Flag indicating whether automatic reconnection attempt should be made in case of a broken connection.

  • timeout_limit (int) – The maximum time in seconds to wait for a response from the server.

socket

The socket object for communicating with the server.

buffer_size

The size of the buffer for receiving data from the server.

connection_attempts

The maximum number of attempts to establish a connection with the server.

reconnect

Flag indicating whether automatic reconnection attempt should be made in case of a broken connection.

timeout_limit

The maximum time in seconds to wait for a response from the server.

close()[source]

Method to close the connection.

Return type:

None

connect()[source]

Method to connect a socket to the database server.

Return type:

None

property host: str

Connection host.

property id: str

Unique identifier for the connection.

is_connected()[source]

A boolean indicating whether the connect method was successfully invoked. :rtype: bool

Note

This does not mean that the socket has a connection to the server. The socket connection may be broken, and we do not update it constantly.

Return type:

bool

is_locked()[source]

Whether the connection is locked.

Return type:

bool

property pending_requests: int

The number of pending requests.

property port: int

Connection port.

receive()[source]

Method to receive the response from the server. None if there is no data in the socket yet.

Return type:

bytes | None

send(data)[source]

Method to send data to the server.

THREAD SAFE.

Parameters:

data (bytes) – Bytes to send.

Return type:

Result

try_reconnect()[source]

A method to attempt to reconnect to the server if the connection is broken. :rtype: Result[bytes]

Note

If the connection is successfully reestablished, the method return a Result object with a failure status and an informational message indicating that the connection was terminated but managed to reestablish it.

Return type:

Result[bytes]

wait_for_response()[source]

A loop to wait for the response from the server.

NOT THREAD SAFE.

Return type:

Result