时间解决Redis Java中设置过期时间的方法(redisjava过期)
Redis is an open source, in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence. It can automatically detect master / slave replication and support automatic failover when configured as a cluster.
In Java applications, Redis is often used to store values used in the application, such as session token, user information , etc.In the Redis documentation, the primary form of data expiration is the use of TTL or time to live. TTL means that each key added to the Redis server has a time-limited existence, after which it is automatically deleted from the data set.
There are many ways to set time cost in Redis, let’s look at some examples.
The first way is to use the EXPIRE command, which requires two parameters: the key name and the timeout in seconds. By default, timeout for keys expires after seconds.
For example:
String key = “key”;
String value = “value”; jedis.set(key,value);
jedis.expire(key,timeout);
The EXPIREAT command is similar to EXPIRE , except that the expiry time is specified in UNIX seconds.
For example:
String key = “key2”;
String value = “value”; jedis.set(key,value);
jedis.expireAt(key,expiryTimeInSeconds);
The third way is to use PEXPIRE command, which is similar to EXPIRE command except that the timeout value is specified in milliseconds.
For example:
String key = “key3”;
String value = “value”; jedis.set(key,value);
jedis.pexpire(key,timeoutInMilliSeconds);
The fourth way is to use the PEXPIREAT command, which is similar to the EXPIREAT command, but the expiry time is specified in UNIX milliseconds.
For example:
String key = “key4”;
String value = “value”; jedis.set(key,value);
jedis.pexpireAt(key,expiryTimeInMilliSeconds);
In Redis Java, setting expiry time is an important action. Use the above commands to solve the problem of setting expiry time in Redis, effectively manage the remaining time of the values, and avoid longer holding time of the values.