EXHASH Commands
EXHASH, a hash data type, allows you to specify expiration times and version numbers for fields. EXHASH is more flexible than HASH and can help you simplify service development in various scenarios.
Highlights
- The expiration time and version number can be specified for each field.
- Efficient, flexible active and passive expiration strategies are supported for fields.
- The syntax is similar to that native Redis hashes use.
Commands
Command |
Syntax |
Description |
---|---|---|
EXHSET |
EXHSET key field value [EX time] [EXAT time] [PX time] [PXAT time] [NX | XX] [VER | ABS | GT version] [KEEPTTL] |
Adds a field to an EXHASH key. If there are no keys available, one will be created automatically. If the field has a value, this command overwrites the value. |
EXHGET |
EXHGET key field |
Obtains the value of a field from an EXHASH key. If the key or field does not exist, nil is returned. |
EXHPTTL |
EXHPTTL key field |
Queries the remaining expiration time of a field in an EXHASH key. Unit: milliseconds. |
EXHTTL |
EXHTTL key field |
Queries the expiration time of a field in an EXHASH key. Unit: seconds. |
EXHVER |
EXHVER key field |
Queries the current version of a field in an EXHASH key. |
EXHINCRBY |
EXHINCRBY key field num [EX time] [EXAT time] [PX time] [PXAT time] [VER | ABS | GT version] [MIN minval] [MAX maxval] [KEEPTTL] |
Adds num to the value of a field in an EXHASH key. num is an integer. If no keys are found, a key will be created. If the field does not exist, this command adds the field and sets the value of the field to 0 before increasing the value of the field.
NOTE:
If you do not specify an expiration time for a field when running this command, the field will never expire. |
EXHINCRBYFLOAT |
EXHINCRBYFLOAT key field num [EX time] [EXAT time] [PX time] [PXAT time] [VER | ABS | GT version] [MIN minval] [MAX maxval] [KEEPTTL] |
Adds num to the value of a field in an EXHASH key. num is a floating point number. If no keys are found, a key will be created. If the field does not exist, this command adds the field and sets the value of the field to 0 before increasing the value of the field.
NOTE:
If you do not specify an expiration time for a field when running this command, the field will never expire. |
EXHMGET |
EXHMGET key field [field ...] |
Obtains multiple field values from an EXHASH key. If the key or field does not exist, nil is returned. |
EXHLEN |
EXHLEN key [NOEXP] |
Obtains the number of fields in an EXHASH key. The returned results may include the number of expired fields that are not deleted, because this command does not delete or filter out expired fields. To obtain only the number of fields that have not expired, you can set parameter NOEXP in the command. |
EXHGETALL |
EXHGETALL key |
Obtains all fields from an EXHASH key and their values. |
EXHDEL |
EXHDEL key field [field ...] |
Deletes a field from an EXHASH key. If the key or field does not exist, 0 is returned. If the field is deleted, 1 is returned. |
DEL |
DEL <key> [key ...] |
Deletes one or more EXHASH keys. |
EXISTS |
EXISTS <key> [key ...] |
Checks whether there is one or more EXHASH data records. |
Complex Commands and Options
- EXHSET
Table 2 EXHSET commands Item
Description
Syntax
EXHSET key field value [EX time] [EXAT time] [PX time] [PXAT time] [NX | XX] [VER | GT | ABS version] [KEEPTTL]
Description
Adds a field to an EXHASH key. If no keys exist, a key will be created. If the field has a value, this command overwrites the value.
To add a field that does not expire, you can run this command to add the field without specifying an expiration time.
Parameters
Key: Object that you want to manage by running this command.
field: An element in the EXHASH command. An EXHASH key can have multiple fields.
value: Value of the field. A field can have only one value.
EX: Relative expiration time of the field, in seconds. 0 indicates that the field will expire immediately. If this parameter is not specified, the field does not expire.
EXAT: Absolute expiration time of the field, in seconds. 0 indicates that the field will expire immediately. If this parameter is not specified, the field does not expire.
PX: Relative expiration time of the field, in milliseconds. 0 indicates that the field will expire immediately. If this parameter is not specified, the field does not expire.
PXAT: Absolute expiration time of the field, in milliseconds. 0 indicates that the field will expire immediately. If this parameter is not specified, the field does not expire.
NX: This parameter is added only when the field does not exist.
XX: This parameter is inserted only if the field exists.
VER: Version number of the field. If the field exists, the version number specified by this parameter is compared with the current version number. If they are the same, the system continues to run this command and increases the version number by 1. If they are different, an error message will be returned. If the field does not exist or the current version of the field is 0, the system ignores this parameter and runs the command. After the operation completes, the version number changes to 1.
GT: Version later than the current one. If the set version number is earlier than the current one, a failure message is returned.
ABS: Absolute version number of the field. When a field is inserted, the version number is the version number specified by this parameter.
KEEPTTL: The current expiration setting of the field is retained if none of the EX, EXAT, PX, and PXAT parameters are specified.
Returned values
If a field is created and a value is set for it, 1 is returned.
If a field already exists and the specified value overwrites the current value, 0 is returned.
If XX is specified and the field does not exist, -1 is returned.
If NX is specified and the field exists, -1 is returned.
If VER is specified and the value does not match the current version, the error message "ERR update version is stale" is returned.
Error messages are returned in other cases.
- Example commands
Setting the expiration time for a field
127.0.0.1:6579> EXHSET k1 f1 v1 ex 10 (integer) 1 127.0.0.1:6579> EXHGET k1 f1 "v1" 127.0.0.1:6579> EXHSET k1 f2 v2 ex 10 (integer) 1 127.0.0.1:6579> EXHGET k1 f1 (nil) 127.0.0.1:6579> EXHGETALL k1 127.0.0.1:6579> EXHGETALL k1 (empty array)
Setting a version number for a field
127.0.0.1:6579> EXHSET k1 f1 v1 (integer) 1 127.0.0.1:6579> EXHVER k1 f1 (integer) 1 127.0.0.1:6579> EXHSET k1 f1 v1 ver 2 (error) ERR update version is stale 127.0.0.1:6579> EXHSET k1 f1 v1 ver 1 (integer) 0 127.0.0.1:6579> EXHVER k1 f1 (integer) 2 127.0.0.1:6579> EXHSET k1 f1 v1 (integer) 0 127.0.0.1:6579> EXHVER k1 f1 (integer) 3 127.0.0.1:6579> EXHSET k1 f1 v1 GT 3 (error) ERR update version is stale 127.0.0.1:6579> EXHSET k1 f1 v1 GT 2 (error) ERR update version is stale 127.0.0.1:6579> EXHSET k1 f1 v1 GT 4 (integer) 0 127.0.0.1:6579> EXHVER k1 f1 (integer) 4 127.0.0.1:6579> EXHSET k1 f1 v1 abs 2 (integer) 0 127.0.0.1:6579> EXHVER k1 f1 (integer) 2
- Example commands
- EXHINCRBY
Table 3 EXHINCRBY commands Item
Description
Syntax
EXHINCRBY key field num [EX time] [EXAT time] [PX time] [PXAT time] [VER | GT | ABS version] [MIN minval] [MAX maxval] [KEEPTTL]
Description
Adds num to the value of a field in an EXHASH key. num is an integer. If no keys are found, a key will be created. If the field does not exist, this command adds the field and sets the value of the field to 0 before increasing the value of the field.
To add a field that does not expire, you can run this command to add the field without specifying an expiration time.
Parameters
Key: Object that you want to manage by running this command.
field: An element in the EXHASH command. An EXHASH key can have multiple fields.
num: Integer to be added to the value of the field.
EX: Relative expiration time of the field, in seconds. 0 indicates that the field will expire immediately. If this parameter is not specified, the field does not expire.
EXAT: Absolute expiration time of the field, in seconds. 0 indicates that the field will expire immediately. If this parameter is not specified, the field does not expire.
PX: Relative expiration time of the field, in milliseconds. 0 indicates that the field will expire immediately. If this parameter is not specified, the field does not expire.
PXAT: Absolute expiration time of the field, in milliseconds. 0 indicates that the field will expire immediately. If this parameter is not specified, the field does not expire.
VER: Version number of the field. If the field exists, compare the version number specified by this parameter with the current version number. If they are the same, use this command and add 1 to the version number. If they are different, an error message will be displayed. If the field does not exist or the current version of the field is 0, ignore this parameter and run the command. After the operation completes, the version number changes to 1.
GT: Version later than the current one. If the set version number is earlier than the current one, a failure message will be returned.
ABS: Absolute version number of the field. When a field is inserted, the version number is the version number specified by this parameter.
KEEPTTL: The current expiration setting of the field is retained if none of the EX, EXAT, PX, and PXAT parameters are specified.
MIN: Minimum value of the field. If the field value is less than the minimum value, an error message is returned.
MAX: Maximum value of the field. If the field value is greater than the maximum value, an error message is returned.
Returned values
If the operation is successful, the value after num is added to is returned.
Otherwise, an error message is returned.
- Example command
An example of using the MIN and MAX parameters
127.0.0.1:6579> EXHINCRBY k1 f1 5 min 6 (error) ERR increment or decrement would overflow 127.0.0.1:6579> EXHINCRBY k1 f1 5 min 4 (integer) 5 127.0.0.1:6579> EXHINCRBY k1 f1 5 max 9 (error) ERR increment or decrement would overflow 127.0.0.1:6579> EXHINCRBY k1 f1 3 max 9 (integer) 8
- Example command
Example of Using ExHash Commands
JAVA(Jedis)
package nosql.cloud.huawei.jedis; import redis.clients.jedis.*; import redis.clients.jedis.util.SafeEncoder; import java.util.ArrayList; public class Main{ public static void main(String[] args) throws InterruptedException { // Initialize the Jedis resource pool configuration. JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); // Set the maximum number of connections in the resource pool. jedisPoolConfig.setMaxTotal(10); // Set the maximum number of idle connections allowed by the pool. jedisPoolConfig.setMaxIdle(10); // Set the minimum number of idle connections retained in the pool. jedisPoolConfig.setMinIdle(2); // Initialize JedisPool. // Note: If the version does not support Access Control List (ACL), the value of user must be null. JedisPool jedisPool = new JedisPool(jedisPoolConfig, "127.0.0.1", 8635, null, "******"); // Obtain connections from the pool. try (Jedis jedis = jedisPool.getResource()) { // example for: EXHSET key field value [EX time] [EXAT time] [PX time] [PXAT time] [NX | XX] [VER | ABS | GT version] [KEEPTTL] jedis.sendCommand(() -> SafeEncoder.encode("exhset"), "key", "field1", "value1"); jedis.sendCommand(() -> SafeEncoder.encode("exhset"), "key", "field2", "value2", "EX", "5"); // example for: EXHGET key field byte[] byteArray = (byte[]) jedis.sendCommand(() -> SafeEncoder.encode("exhget"), "key", "field1"); System.out.println(new String(byteArray)); byteArray = (byte[]) jedis.sendCommand(() -> SafeEncoder.encode("exhget"), "key", "field2"); System.out.println(new String(byteArray)); // example for: EXHGETALL key ArrayList<byte[]> byteArrayList = (ArrayList<byte[]>) jedis.sendCommand(() -> SafeEncoder.encode("exhgetall"), "key"); for (byte[] ba : byteArrayList) { System.out.print(new String(ba)); System.out.print(" "); } System.out.println(); // sleep for 5 seconds Thread.sleep(5000); // exhgetall after sleeping byteArrayList = (ArrayList<byte[]>) jedis.sendCommand(() -> SafeEncoder.encode("exhgetall"), "key"); for (byte[] ba : byteArrayList) { System.out.print(new String(ba)); System.out.print(" "); } } // Disable the pool. jedisPool.close(); } }
Click EXHASH for Ad Frequency Control to view the best practices of the EXHASH command.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot