This topic describes the enhanced commands that you can run to process strings on performance-enhanced instances of ApsaraDB for Redis Enhanced Edition. These commands include check-and-set (CAS) and compare-and-delete (CAD).

Prerequisites

The commands described in this topic take effect only if the following conditions are met:

  • A performance-enhanced instance of ApsaraDB for Redis Enterprise Edition is used.
  • The Redis strings to be managed are stored on the performance-enhanced instance.
    Note You can manage both Redis strings and TairStrings on a performance-enhanced instance. However, CAS and CAD commands are applicable to only Redis strings.

Commands

Table 1. Enhanced commands that are applicable to Redis strings
Statement Syntax Description
CAS CAS <key> <oldvalue> <newvalue> Changes the value of a specified key to newvalue if the current value of the key matches the oldvalue parameter. If the current value of the key does not match the oldvalue parameter, the value is not changed.
Note The CAS command applies to only Redis strings. To change TairString values, use the EXCAS command.
CAD CAD <key> <value> Deletes a specified key if the current value of the key matches the oldvalue parameter. The key is not deleted if the current value of the key does not match the oldvalue parameter.
Note The CAD command applies to only Redis strings. To delete TairString keys, use the EXCAD command.

CAS

  • Syntax

    CAS <key> <oldvalue> <newvalue>

  • Time complexity

    O(1)

  • Description

    This command can be used to change the value of a specified key to a new value if the current value of the key matches a specified value. The value is not changed if the current value of the key does not match the specified value.

  • Parameters and options
    Parameter/option Description
    key The key of the Redis string that you want to manage with the command.
    oldvalue The value that you compare with the current value of the specified key.
    newvalue Changes the value of the specified key to the value of this parameter if the current value of the key matches the specified value.
  • Returned values
    • 1: the operation is successful.
    • -1: the specified key does not exist.
    • 0: the operation fails.
    • Otherwise, an error message is returned.
  • Examples
    127.0.0.1:6379> SET foo bar
    OK
    127.0.0.1:6379> CAS foo baa bzz
    (integer) 0
    127.0.0.1:6379> GET foo
    "bar"
    127.0.0.1:6379> CAS foo bar bzz
    (integer) 1
    127.0.0.1:6379> GET foo
    "bzz"

CAD

  • Syntax

    CAD <key> <value>

  • Time complexity

    O(1)

  • Description

    This command can be used to delete a specified key if the current value of the key matches a specified value. The key is not deleted if the current value of the key does not match the specified value.

  • Parameters and options
    Parameter/option Description
    key The key of the Redis string that you want to manage with the command.
    value The value that you compare with the current value of the specified key.
  • Returned values
    • 1: the operation is successful.
    • -1: the specified key does not exist.
    • 0: the operation fails.
    • Otherwise, an error message is returned.
  • Examples
    127.0.0.1:6379> SET foo bar
    OK
    127.0.0.1:6379> CAD foo bzz
    (integer) 0
    127.0.0.1:6379> CAD not-exists xxx
    (integer) -1
    127.0.0.1:6379> CAD foo bar
    (integer) 1
    127.0.0.1:6379> GET foo
    (nil)