This topic describes new commands supported by Tair instances that can be used to improve the string functionality. The commands include CAS and CAD commands.

Prerequisites

A performance-enhanced or persistent memory-optimized instance of Tair whose minor version is 1.2.3 or later is created. For more information about performance-enhanced instances and persistent memory-optimized instances, see DRAM-based instances and Persistent memory-optimized instances.

Note We recommend that you update your instance to the latest minor version for more features and higher stability. For more information, see Update the minor version of an instance. If your instance is a cluster instance or read/write splitting instance, we recommend that you update the proxy nodes in the instance to the latest minor version to ensure that all commands can be run as expected. For more information about cluster instances and read/write splitting instances, see Cluster architecture and Read/write splitting architecture.

Precautions

In this topic, the strings that you want to manage are native Redis strings.
Note You can manage Redis strings and TairStrings on a Tair instance. However, CAS and CAD commands are applicable only to Redis strings.

Supported commands

Table 1. Commands that enhance the string functionality
CommandSyntaxDescription
CASCAS key oldvalue newvalue
Changes the existing value of a specified key if the existing value matches a specified one. Otherwise, the existing value remains unchanged.
Note This command applies only to Redis strings. To change TairString values, run the EXCAS command.
CADCAD key value
Deletes a specified key if the existing value of the key matches a specified one. Otherwise, the key is not deleted.
Note This command applies only to Redis strings. To delete TairString keys, run the EXCAD command.
Note The following section describes command syntax used in this topic:
  • Uppercase keyword: the command keyword.
  • Italic: Words in italic indicate variable information that you supply.
  • [options]: optional parameters. Parameters that are not included in brackets are required.
  • A|B: specifies that these parameters are mutually exclusive. Select one of two or more parameters.
  • ...: specifies to repeat the preceding content.

CAS

ItemDescription
SyntaxCAS key oldvalue newvalue
Time complexityO(1)
Command description
Changes the existing value of a specified key if the existing value matches a specified one. Otherwise, the existing value remains unchanged.
Note This command applies only to Redis strings. To change TairString values, run the EXCAS command.
Parameter
  • Key: the key of the Redis string that you want to manage by running the command.
  • oldvalue: the specified value that you want to compare with the existing value of the key.
  • newvalue: the new value to which you want to change the existing value of the key if the existing value matches the specified one.
Output
  • If the operation is successful, a value of 1 is returned.
  • If the operation fails, a value of 0 is returned.
  • If the key does not exist, a value of -1 is returned.
  • Otherwise, an error message is returned.
Example

The SET foo bar command is run in advance.

Sample command:

CAS foo bar bzz

Sample output:

(integer) 1

If you run the GET foo command after this command is run, "bzz" is returned.

CAD

ItemDescription
SyntaxCAD key value
Time complexityO(1)
Command description
Deletes a specified key if the existing value of the key matches a specified one. Otherwise, the key is not deleted.
Note This command applies only to Redis strings. To delete TairString keys, run the EXCAD command.
Parameter
  • Key: the key of the Redis string that you want to manage by running the command.
  • value: the specified value that you want to compare with the existing value of the key.
Output
  • If the operation is successful, a value of 1 is returned.
  • If the operation fails, a value of 0 is returned.
  • If the key does not exist, a value of -1 is returned.
  • Otherwise, an error message is returned.
Example

The SET foo bar command is run in advance.

Sample command:

CAD foo bar

Sample output:

(integer) 1

After this command is run, the foo key is deleted. Then, if you run the GET foo command, (nil) is returned.