TairHash (exHash) is a hash data structure that lets you set an expiration time and version for each field. This capability enhances the flexibility of the hash data structure and simplifies development in many scenarios.
Introduction to TairHash
TairHash provides rich data interfaces and high performance, similar to Redis Hash. However, unlike Redis Hash, which only allows setting an expiration time at the key level, TairHash enables you to set an expiration time and version for each individual field. This significantly improves the flexibility of the hash data structure and streamlines development. TairHash uses an efficient Active Expire algorithm that checks for and deletes expired fields without noticeably affecting response time.
Main features
Set an expiration time and version for each field.
Supports efficient and flexible active and passive expiration policies for fields.
Uses syntax similar to the native Redis Hash data type.
This module is open source. For more information, see TairHash.
Prerequisites
The instance is Tair.
Persistent memory-optimized (minor version 1.2.6 or later)
The latest minor version provides additional features and improved stability. Update your instance to the latest minor version. For more information, see Minor version update. If your instance uses a cluster architecture or read/write splitting architecture, update the proxy nodes to the latest minor version. Otherwise, some commands may not be recognized.
Precautions
The commands operate on TairHash data in a Tair instance.
Commands
Table 1. TairHash commands
Command | Syntax | Description |
| Adds a field to the specified TairHash key. If the key does not exist, a key is automatically created. If the field already exists, its value is overwritten. | |
| Gets the value of a field in the specified TairHash key. Returns nil if the key or field does not exist. | |
| Adds multiple fields to the specified TairHash key. If the key does not exist, a key is automatically created. If a field already exists, its value is overwritten. | |
| Sets an absolute expiration time for a field in the specified TairHash key. The time is accurate to the millisecond. | |
| Sets a relative expiration time in milliseconds for a field in the specified TairHash key. | |
| Sets an absolute expiration time for a field in the specified TairHash key. The time is accurate to the second. | |
| Sets a relative expiration time in seconds for a field in the specified TairHash key. | |
| Gets the remaining TTL of a field in the specified TairHash key. The time is in milliseconds. | |
| Gets the remaining TTL of a field in the specified TairHash key. The time is in seconds. | |
| Gets the current version number of a field in the specified TairHash key. | |
| Sets the version number of a field in the specified TairHash key. | |
| Increments the integer value of a field in the specified TairHash key by a number. If the key does not exist, a key is automatically created. If the specified field does not exist, the field is created with a value of 0 before the increment operation. Note After you set a timeout for a field, if you run this command again on the field without specifying a timeout, the field is set to never expire. | |
| Increments the floating-point value of a field in the specified TairHash key by a number. If the key does not exist, a key is automatically created. If the specified field does not exist, the field is created with a value of 0 before the increment operation. Note After you set a timeout for a field, if you run this command again on the field without specifying a timeout, the field is set to never expire. | |
| Gets the value and version of a field in the specified TairHash key. Returns nil if the key or field does not exist. | |
| Gets the values of multiple fields in the specified TairHash key. Returns nil if the key or a field does not exist. | |
| Gets the values and versions of multiple fields in the specified TairHash key. | |
| Gets the number of fields in the specified TairHash key. This command does not trigger passive eviction or filter out expired fields. Therefore, the result may include fields that have expired but have not been deleted. To return only the number of non-expired fields, specify the NOEXP option. | |
| Checks whether the specified field exists in the TairHash key. | |
| Gets the length of the value of a field in the specified TairHash key. | |
| Gets all fields in the specified TairHash key. | |
| Gets the values of all fields in the specified TairHash key. | |
| Gets all fields and their values in the specified TairHash key. | |
| Scans the specified TairHash key. Note This command is supported only for memory-optimized instances. | |
| Deletes a field from the specified TairHash key. Returns 0 if the key or field does not exist. Returns 1 if the deletion is successful. | |
| Deletes one or more TairHash keys using the native Redis DEL 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.
EXHSET
Category | Description |
Syntax |
|
time complexity | O(1) |
Command description | Adds a field to the specified TairHash key. If the key does not exist, a key is automatically created. If the field already exists, its value is overwritten. Note
|
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
EXHGET
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Gets the value of a field in the specified TairHash key. Returns nil if the key or field does not exist. |
Options |
|
Return value |
|
Example | Run the Sample command: Sample return value: |
EXHMSET
Category | Description |
Syntax |
|
Time complexity | O(N) |
Command description | Adds multiple fields to the specified TairHash key. If the key does not exist, a key is automatically created. If a field already exists, its value is overwritten. Note After you create a key, you can use the EXHPEXPIREAT, EXHPEXPIRE, EXHEXPIREAT, or EXHEXPIRE command to set an expiration time for a field. You can also use the EXPIRE or EXPIREAT command to set an expiration time for the key. |
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
EXHPEXPIREAT
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Sets an absolute expiration time for a field in the specified TairHash key. The time is accurate to the millisecond. |
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
EXHPEXPIRE
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Sets a relative expiration time in milliseconds for a field in the specified TairHash key. |
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
EXHEXPIREAT
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Sets an absolute expiration time for a field in the specified TairHash key. The time is accurate to the second. |
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
EXHEXPIRE
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Sets a relative expiration time in seconds for a field in the specified TairHash key. |
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
EXHPTTL
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Gets the remaining TTL of a field in the specified TairHash key. The time is in milliseconds. |
Options |
|
Return value |
|
Example | Run the Sample command: Sample return value: |
EXHTTL
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Gets the remaining TTL of a field in the specified TairHash key. The time is in seconds. |
Options |
|
Return value |
|
Example | Run the Sample command: Sample return value: |
EXHVER
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Gets the current version number of a field in the specified TairHash key. |
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
EXHSETVER
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Sets the version number of a field in the specified TairHash key. |
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
EXHINCRBY
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Increments the integer value of a field in the specified TairHash key by a number. If the key does not exist, a key is automatically created. If the specified field does not exist, the field is created with a value of 0 before the increment operation. Note After you set a timeout for a field, if you run this command again on the field without specifying a timeout, the field is set to never expire. |
Options |
|
Return value |
|
Example | Run the Sample command: Sample return value: |
EXHINCRBYFLOAT
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Increments the floating-point value of a field in the specified TairHash key by a number. If the key does not exist, a key is automatically created. If the specified field does not exist, the field is created with a value of 0 before the increment operation. Note After you set a timeout for a field, if you run this command again on the field without specifying a timeout, the field is set to never expire. |
Options |
|
Return value |
|
Example | Run the Sample command: Sample return value: |
EXHGETWITHVER
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Gets the value and version of a field in the specified TairHash key. Returns nil if the key or field does not exist. |
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
EXHMGET
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Gets the values of multiple fields in the specified TairHash key. Returns nil if the key or a field does not exist. |
Options |
|
Return value |
|
Example | Run the Sample command: Sample response: |
EXHMGETWITHVER
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Gets the values and versions of multiple fields in the specified TairHash key. |
Options |
|
Return value |
|
Example | Run the Sample command: Example response: |
EXHLEN
Category | Description |
Syntax |
|
Time complexity | O(1) if the NOEXP option is not specified. O(N) if the NOEXP option is specified. |
Command description | Gets the number of fields in the specified TairHash key. This command does not trigger passive eviction or filter out expired fields. Therefore, the result may include fields that have expired but have not been deleted. To return only the number of non-expired fields, specify the NOEXP option. |
Options |
|
Return value |
|
Example | Sample command: Sample response: |
EXHEXISTS
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Checks whether the specified field exists in the TairHash key. |
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
EXHSTRLEN
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Gets the length of the value of a field in the specified TairHash key. |
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
EXHKEYS
Category | Description |
Syntax |
|
Time complexity | O(N) |
Command description | Gets all fields in the specified TairHash key. |
Options |
|
Return value |
|
Example | Run the Sample command: Sample return value: |
EXHVALS
Category | Description |
Syntax |
|
Time complexity | O(N) |
Command description | Gets the values of all fields in the specified TairHash key. |
Options |
|
Return value |
|
Example | Run the Sample command: Sample return value: |
EXHGETALL
Category | Description |
Syntax |
|
Time complexity | O(N) |
Command description | Gets all fields and their values in the specified TairHash key. |
Options |
|
Return value |
|
Example | Run the Sample command: Example response: |
EXHSCAN
Category | Description |
Syntax |
|
Time complexity | O(1) for each call, and O(N) for a full traversal. |
Command description | Scans the specified TairHash key. Note This command is supported only for memory-optimized instances. |
Options |
|
Return value |
|
Example | Run the Sample command: Sample return value: |
EXHDEL
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Deletes a field from the specified TairHash key. Returns 0 if the key or field does not exist. Returns 1 if the deletion is successful. |
Options |
|
Return value |
|
Example | Sample command: Sample return value: |
FAQ
Q: Why does exHash use more memory than the standard hash of Redis Open-Source Edition for the same data?
A: exHash lets you set expiration times and versions for individual fields. This requires storing additional metadata in the data structure, resulting in higher memory usage compared to the standard hash of Redis Open-Source Edition.