This topic describes the enhanced commands that you can run to process strings on performance-enhanced instances of ApsaraDB for Redis Enhanced Edition (Tair). These commands include Compare 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 Enhanced Edition (Tair) is used.
  • The native Redis string data to be managed is stored on the performance-enhanced instance.
    Note You can manage both native Redis string data and TairString data on a performance-enhanced instance. Only native Redis string data supports the commands described in this topic.

Commands

Table 1. Commands to improve the management of Redis strings
Command Syntax Description
CAS CAS <key> <oldvalue> <newvalue> This command can be used to change 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 CAS is only applicable to Redis strings. To change TairString values, use the EXCAS command.
CAD CAD <key> <value> This command can be used to delete 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 CAD is only applicable to 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 Description
    key The key of the native Redis string data 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
    • Expected result: 1.
    • A value of -1 is returned if the specified key does not exist.
    • A value of 0 is returned if the change fails.
    • An error message is returned as another unexpected result.
  • 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 Description
    key The key of the native Redis string data that you want to manage with the command.
    value The value that you compare with the current value of the specified key.
  • Returned values
    • Expected result: 1.
    • A value of -1 is returned if the specified key does not exist.
    • A value of 0 is returned if the deletion fails.
    • An error message is returned as another unexpected result.
  • 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)