This topic describes the new CAS and CAD commands for Tair instances, which enhance Redis string functionality.
Prerequisites
The instance is a Tair DRAM-based or persistent memory-optimized instance whose minor version is 1.2.3 or later.
The latest minor version provides more features and higher stability. We recommend that you update the instance to the latest minor version. 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.
Usage notes
The commands in this topic apply to native Redis strings.
A Tair instance can contain both Redis strings and TairStrings. The commands in this topic do not apply to TairStrings.
Command list
Table 1. String enhancement commands
Command | Syntax | Description |
| CAS (Compare And Set) checks if the value of a target key is equal to the specified oldvalue. If they are equal, the command updates the value to the new newvalue. Otherwise, the command does nothing. Note This command applies only to Redis strings. To perform the same operation on TairStrings, use the EXCAS command. | |
| CAD (Compare And Delete) checks if the value of a target key is equal to the specified value. If they are equal, the command deletes the key. Otherwise, the command does nothing. Note This command applies only to Redis strings. To perform the same operation on TairStrings, use the EXCAD command. |
The following list describes the conventions for the command syntax used in this topic:
Uppercase keyword: indicates the command keyword.Italic text: indicates variables.[options]: indicates that the enclosed parameters are optional. Parameters that are not enclosed by brackets must be specified.A|B: indicates that the parameters separated by the vertical bars (|) are mutually exclusive. Only one of the parameters can be specified....: indicates that the parameter preceding this symbol can be repeatedly specified.
CAS
Item | Description |
Syntax |
|
Time complexity | O(1) |
Command description | CAS (Compare And Set) checks if the value of a target key is equal to the specified oldvalue. If they are equal, the command updates the value to the new newvalue. Otherwise, the command does nothing. Note This command applies only to Redis strings. To perform the same operation on TairStrings, use the EXCAS command. |
Options |
Note If the original string has a Time to Live (TTL) set, running the CAS command without a TTL parameter removes the TTL. The key will then not expire. |
Return value |
|
Example | Run the Sample command: Sample return value: If you then run the |
CAD
Item | Description |
Syntax |
|
Time complexity | O(1) |
Command description | CAD (Compare And Delete) checks if the value of a target key is equal to the specified value. If they are equal, the command deletes the key. Otherwise, the command does nothing. Note This command applies only to Redis strings. To perform the same operation on TairStrings, use the EXCAD command. |
Options |
|
Return value |
|
Example | Run the Sample command: Sample return value: The foo key is deleted. If you then run the |