Skip to content

Standalone Transaction

Home / python / standalone_transaction

glide.async_commands.transaction.Transaction

Bases: BaseTransaction

Extends BaseTransaction class for standalone commands that are not supported in cluster mode.

Command Response

The response for each command depends on the executed command. Specific response types are documented alongside each method.

Example

transaction = Transaction() transaction.set("key", "value") transaction.select(1) # Standalone command transaction.get("key") await client.exec(transaction) [OK , OK , None]

Source code in glide/async_commands/transaction.py
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
class Transaction(BaseTransaction):
    """
    Extends BaseTransaction class for standalone commands that are not supported in cluster mode.

    Command Response:
        The response for each command depends on the executed command. Specific response types
        are documented alongside each method.

    Example:
        >>> transaction = Transaction()
        >>> transaction.set("key", "value")
        >>> transaction.select(1)  # Standalone command
        >>> transaction.get("key")
        >>> await client.exec(transaction)
        [OK , OK , None]

    """

    # TODO: add SLAVEOF and all SENTINEL commands
    def move(self, key: TEncodable, db_index: int) -> "Transaction":
        """
        Move `key` from the currently selected database to the database specified by `db_index`.

        See [valkey.io](https://valkey.io/commands/move/) for more details.

        Args:
            key (TEncodable): The key to move.
            db_index (int): The index of the database to move `key` to.

        Commands response:
            bool: True if `key` was moved.

            False if the `key` already exists in the destination database
            or does not exist in the source database.
        """
        return self.append_command(RequestType.Move, [key, str(db_index)])

    def select(self, index: int) -> "Transaction":
        """
        Change the currently selected database.

        See [valkey.io](https://valkey.io/commands/select/) for details.

        Args:
            index (int): The index of the database to select.

        Command response:
            A simple OK response.
        """
        return self.append_command(RequestType.Select, [str(index)])

    def copy(
        self,
        source: TEncodable,
        destination: TEncodable,
        destinationDB: Optional[int] = None,
        replace: Optional[bool] = None,
    ) -> "Transaction":
        """
        Copies the value stored at the `source` to the `destination` key. If `destinationDB`
        is specified, the value will be copied to the database specified by `destinationDB`,
        otherwise the current database will be used. When `replace` is True, removes the
        `destination` key first if it already exists, otherwise performs no action.

        See [valkey.io](https://valkey.io/commands/copy) for more details.

        Args:
            source (TEncodable): The key to the source value.
            destination (TEncodable): The key where the value should be copied to.
            destinationDB (Optional[int]): The alternative logical database index for the destination key.
            replace (Optional[bool]): If the destination key should be removed before copying the value to it.

        Command response:
            bool: True if the source was copied.

            Otherwise, return False.

        Since: Valkey version 6.2.0.
        """
        args = [source, destination]
        if destinationDB is not None:
            args.extend(["DB", str(destinationDB)])
        if replace is not None:
            args.append("REPLACE")

        return self.append_command(RequestType.Copy, args)

    def publish(self, message: TEncodable, channel: TEncodable) -> "Transaction":
        """
        Publish a message on pubsub channel.

        See [valkey.io](https://valkey.io/commands/publish) for more details.

        Args:
            message (TEncodable): Message to publish
            channel (TEncodable): Channel to publish the message on.

        Command Respose:
            int: Number of subscriptions in that shard that received the message.

        """
        return self.append_command(RequestType.Publish, [channel, message])

copy(source, destination, destinationDB=None, replace=None)

Copies the value stored at the source to the destination key. If destinationDB is specified, the value will be copied to the database specified by destinationDB, otherwise the current database will be used. When replace is True, removes the destination key first if it already exists, otherwise performs no action.

See valkey.io for more details.

Parameters:

Name Type Description Default
source TEncodable

The key to the source value.

required
destination TEncodable

The key where the value should be copied to.

required
destinationDB Optional[int]

The alternative logical database index for the destination key.

None
replace Optional[bool]

If the destination key should be removed before copying the value to it.

None
Command response

bool: True if the source was copied.

Otherwise, return False.

Since: Valkey version 6.2.0.

Source code in glide/async_commands/transaction.py
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
def copy(
    self,
    source: TEncodable,
    destination: TEncodable,
    destinationDB: Optional[int] = None,
    replace: Optional[bool] = None,
) -> "Transaction":
    """
    Copies the value stored at the `source` to the `destination` key. If `destinationDB`
    is specified, the value will be copied to the database specified by `destinationDB`,
    otherwise the current database will be used. When `replace` is True, removes the
    `destination` key first if it already exists, otherwise performs no action.

    See [valkey.io](https://valkey.io/commands/copy) for more details.

    Args:
        source (TEncodable): The key to the source value.
        destination (TEncodable): The key where the value should be copied to.
        destinationDB (Optional[int]): The alternative logical database index for the destination key.
        replace (Optional[bool]): If the destination key should be removed before copying the value to it.

    Command response:
        bool: True if the source was copied.

        Otherwise, return False.

    Since: Valkey version 6.2.0.
    """
    args = [source, destination]
    if destinationDB is not None:
        args.extend(["DB", str(destinationDB)])
    if replace is not None:
        args.append("REPLACE")

    return self.append_command(RequestType.Copy, args)

move(key, db_index)

Move key from the currently selected database to the database specified by db_index.

See valkey.io for more details.

Parameters:

Name Type Description Default
key TEncodable

The key to move.

required
db_index int

The index of the database to move key to.

required
Commands response

bool: True if key was moved.

False if the key already exists in the destination database or does not exist in the source database.

Source code in glide/async_commands/transaction.py
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
def move(self, key: TEncodable, db_index: int) -> "Transaction":
    """
    Move `key` from the currently selected database to the database specified by `db_index`.

    See [valkey.io](https://valkey.io/commands/move/) for more details.

    Args:
        key (TEncodable): The key to move.
        db_index (int): The index of the database to move `key` to.

    Commands response:
        bool: True if `key` was moved.

        False if the `key` already exists in the destination database
        or does not exist in the source database.
    """
    return self.append_command(RequestType.Move, [key, str(db_index)])

publish(message, channel)

Publish a message on pubsub channel.

See valkey.io for more details.

Parameters:

Name Type Description Default
message TEncodable

Message to publish

required
channel TEncodable

Channel to publish the message on.

required
Command Respose

int: Number of subscriptions in that shard that received the message.

Source code in glide/async_commands/transaction.py
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
def publish(self, message: TEncodable, channel: TEncodable) -> "Transaction":
    """
    Publish a message on pubsub channel.

    See [valkey.io](https://valkey.io/commands/publish) for more details.

    Args:
        message (TEncodable): Message to publish
        channel (TEncodable): Channel to publish the message on.

    Command Respose:
        int: Number of subscriptions in that shard that received the message.

    """
    return self.append_command(RequestType.Publish, [channel, message])

select(index)

Change the currently selected database.

See valkey.io for details.

Parameters:

Name Type Description Default
index int

The index of the database to select.

required
Command response

A simple OK response.

Source code in glide/async_commands/transaction.py
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
def select(self, index: int) -> "Transaction":
    """
    Change the currently selected database.

    See [valkey.io](https://valkey.io/commands/select/) for details.

    Args:
        index (int): The index of the database to select.

    Command response:
        A simple OK response.
    """
    return self.append_command(RequestType.Select, [str(index)])