The key command is used to manage the keys of redis
1. Used to delete the key DEL key when the key exists
2. Check if the given key exists. Returns 1 if key exists, otherwise returns 0 EXISTS key
3. Set the expiration time EXPIRE key seconds for the given key
4. The role of EXPIREAT is similar to that of EXPIRE, which is used to set the expiration time for the key. The difference is that the time parameter accepted by the EXPIREAT command is UNIX timestamp (unix timestamp) EXPIREAT key timestamp
5. Find all key KEYS pattern matching the given pattern
6. Remove the expiration time of the key, and the key will persist as the PERSIST key
7. Returns the remaining time to live (TTL, time to live) for a given key in seconds TTL key
Returns -2 when key does not exist. Returns -1 when the key exists but no remaining time to live is set. Otherwise, in seconds, returns the remaining time to live for the key
8. Modify the name of the key RENAME key newkey
Returns an error when OLD_KEY_NAME and NEW_KEY_NAME are the same, or when OLD_KEY_NAME does not exist. When NEW_KEY_NAME already exists, the RENAME command will overwrite the old value
9. Returns the type of the value stored by key. TYPE key
key code operation:
package com.study.util; import java.util.Set; import redis.clients.jedis.Jedis; public class RedisKeys { public static void main(String[] args) { Jedis jedis = RedisUtil.getJedis(); //设置一下key值 jedis.mset("a","av","aa","aav","b","bv","c","cv","d","dv","e","ev"); //获取以a开头的key Set<String> keys = jedis.keys("a*"); System.out.print( "Keys starting with a are: " ); for (String key : keys) { System.out.print(key+","); } System.out.println(); // Delete the key named aa jedis.del("aa" ); keys = jedis.keys("*"); System.out.print( "The deleted key has" ); for (String key : keys) { System.out.print(key+","); } System.out.println(); // Check if the key named aa exists boolean he = jedis.exists("aa" ); System.out.println( "Does the key named aa exist: "+ he); // Set an expiration time of 300 seconds for a jedis.expire("a", 300 ); // Set a timestamp for b jedis.expireAt("b", 2293840000L ); // Set the expiration time of c to 5* 60*1000 milliseconds jedis.pexpire("c", 300000 ); // Return the remaining time of a in seconds long attl = jedis.ttl("a" ); System.out.println( "Remaining time of a (seconds):" + attl); // Return the remaining time of b in milliseconds long bpttl = jedis.pttl("b" ); System.out.println( "b's remaining time (milliseconds):" + bpttl); // Remove c's expiration time jedis.persist("c" ); // When the key does not exist, return -2. Returns -1 when the key exists but no remaining time to live is set. Otherwise, return the remaining lifetime of the key in seconds long cttl = jedis.ttl("c" ); System.out.println( "Remaining time of c (seconds):" + cttl); // Modify the name of a to aa String result = jedis.rename("a", "aa" ); System.out.println( "Whether changing the name of a to aa is successful: " + result); // Return the type of aa value String type = jedis.type("aa" ); System.out.println( "The type of the value stored in aa:" + type); jedis.close(); //RedisUtil.closeJedisPool(); } }
Code git address: https://gitee.com/sjcq/redis.git