ZCached

Attributes
Methods
class zcached.ZCached(host, port=7556, pool_size=1, buffer_size=2048, connection_attempts=3, reconnect=True, timeout_limit=10, **kwargs)[source]

Bases: object

ZCached client to connect to the server and send commands.

Parameters:
  • host (str) – Server host address.

  • port (int) – Server port number.

  • pool_size (int) – Number of connections to be created in the connection pool. If you do not send a large number of requests simultaneously in other threads, then you do not need more connections than 1.

  • 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. This value is also considered while reconnecting.

  • 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.

  • kwargs (ConnectionPool) – Optional keyword arguments.

connection_pool

The connection pool used by the client to manage connections to the server.

dbsize()[source]

Retrieve the size of the database.

Return type:

Result[int]

delete(key)[source]

Method to delete a database record by key.

Parameters:

key (str) – Key of the record being deleted.

Return type:

Result[str]

exists(key)[source]

Checks if the specified key exists in the database.

Parameters:

key (str) – The key to check for existence in the database.

Return type:

bool

Notes

Using this method directly may be unsafe as it does not verify the connection status. When the key exists in the database, but the connection is broken, the value False will be returned. Because of this it’s recommended to use this method only when the connection to the server is guaranteed.

flush()[source]

Method to flush all database records.

Return type:

Result[str]

classmethod from_connection_pool(connection_pool)[source]

Creates a client instance from an existing connection pool.

Parameters:

connection_pool (ConnectionPool) – The connection pool to be used by the client.

Return type:

Self

get(key)[source]

Method to send a get command to the database.

Parameters:

key (str) – The key to retrieve the value from the database.

Return type:

Result

get_connection()[source]

Retrieves the least loaded connection from the connection pool. None if there is no any running connections.

Return type:

Connection | None

is_alive()[source]

Checks if the client is currently connected to the server. :rtype: bool

Note

This method sends a ping command to the connected server.

Return type:

bool

keys()[source]

Retrieve the keys of the database.

Return type:

Result[List[str]]

lastsave()[source]

Method to retrieve the Unix timestamp of the last successful database save.

Return type:

Result[int]

mget(*keys)[source]

Method to send a mget command to the database. This command allows you to retrieve multiple values simultaneously.

Example usage: client.mget("key1", "key2", "key3")

Note

For every key that does not hold a string value or does not exist, the special value None is returned. Because of this, the operation never fails.

Parameters:

keys (str) – Keys to retrieve values from the database.

Return type:

Result[dict[str, Any]]

mset(**params)[source]

Method to set multiple database records simultaneously.

Example usage: client.mset(key1="value1", key2=True, key3=9999)

Parameters:

params (Union[str, int, float, bool, list, tuple, dict, set, None]) – Keyword arguments representing key-value pairs to be set in the database.

Return type:

Result[str]

ping()[source]

Send a ping command to the database.

Return type:

Result[str]

run()[source]

Establishes connections with the database server.

Return type:

None

save()[source]

Method to save all database records.

Return type:

Result[str]

send(data)[source]

Method to send data to the server.

Return type:

Result

Parameters:

data (bytes)

set(key, value)[source]

Method to create a new database record.

Parameters:
Return type:

Result[str]