云服务器网:购买云服务器和VPS必上的网站!

时间解决Redis Java中设置过期时间的方法

Redis is an open source, in-memory data structure store, used as a database, cache and message broker. It supports data structures

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.

本文来源:https://www.yuntue.com/post/214337.html | 云服务器网,转载请注明出处!

关于作者: yuntue

云服务器(www.yuntue.com)是一家专门做阿里云服务器代金券、腾讯云服务器优惠券的网站,这里你可以找到阿里云服务器腾讯云服务器等国内主流云服务器优惠价格,以及海外云服务器、vps主机等优惠信息,我们会为你提供性价比最高的云服务器和域名、数据库、CDN、免费邮箱等企业常用互联网资源。

为您推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注