当前位置:Gxlcms > redis > redis数据过期时间设置

redis数据过期时间设置

时间:2021-07-01 10:21:17 帮助过:427人阅读

1、Redis中key的的过期时间

通过EXPIRE key seconds命令来设置数据的过期时间。返回1表明设置成功,返回0表明key不存在或者不能成功设置过期时间。在key上设置了过期时间后key将在指定的秒数后被自动删除。被指定了过期时间的key在Redis中被称为是不稳定的。

推荐:redis入门教程

当key被DEL命令删除或者被SET、GETSET命令重置后与之关联的过期时间会被清除

  1. 127.0.0.1:6379> setex s 20 1
  2. OK
  3. 127.0.0.1:6379> ttl s
  4. (integer) 17
  5. 127.0.0.1:6379> setex s 200 1
  6. OK
  7. 127.0.0.1:6379> ttl s
  8. (integer) 195
  9. 127.0.0.1:6379> setrange s 3 100
  10. (integer) 6
  11. 127.0.0.1:6379> ttl s
  12. (integer) 152
  13. 127.0.0.1:6379> get s
  14. "1\x00\x00100"
  15. 127.0.0.1:6379> ttl s
  16. (integer) 108
  17. 127.0.0.1:6379> getset s 200
  18. "1\x00\x00100"
  19. 127.0.0.1:6379> get s
  20. "200"
  21. 127.0.0.1:6379> ttl s
  22. (integer) -1

使用PERSIST可以清除过期时间

  1. 127.0.0.1:6379> setex s 100 test
  2. OK
  3. 127.0.0.1:6379> get s
  4. "test"
  5. 127.0.0.1:6379> ttl s
  6. (integer) 94
  7. 127.0.0.1:6379> type s
  8. string
  9. 127.0.0.1:6379> strlen s
  10. (integer) 4
  11. 127.0.0.1:6379> persist s
  12. (integer) 1
  13. 127.0.0.1:6379> ttl s
  14. (integer) -1
  15. 127.0.0.1:6379> get s
  16. "test"

使用rename只是改了key值

  1. 127.0.0.1:6379> expire s 200
  2. (integer) 1
  3. 127.0.0.1:6379> ttl s
  4. (integer) 198
  5. 127.0.0.1:6379> rename s ss
  6. OK
  7. 127.0.0.1:6379> ttl ss
  8. (integer) 187
  9. 127.0.0.1:6379> type ss
  10. string
  11. 127.0.0.1:6379> get ss
  12. "test"

说明:Redis2.6以后expire精度可以控制在0到1毫秒内,key的过期信息以绝对Unix时间戳的形式存储(Redis2.6之后以毫秒级别的精度存储),所以在多服务器同步的时候,一定要同步各个服务器的时间

2、Redis过期键删除策略

Redis key过期的方式有三种:

(1)、被动删除:当读/写一个已经过期的key时,会触发惰性删除策略,直接删除掉这个过期key

(2)、主动删除:由于惰性删除策略无法保证冷数据被及时删掉,所以Redis会定期主动淘汰一批已过期的key

(3)、当前已用内存超过maxmemory限定时,触发主动清理策略

被动删除

只有key被操作时(如GET),REDIS才会被动检查该key是否过期,如果过期则删除之并且返回NIL。

1、这种删除策略对CPU是友好的,删除操作只有在不得不的情况下才会进行,不会其他的expire key上浪费无谓的CPU时间。

2、但是这种策略对内存不友好,一个key已经过期,但是在它被操作之前不会被删除,仍然占据内存空间。如果有大量的过期键存在但是又很少被访问到,那会造成大量的内存空间浪费。expireIfNeeded(redisDb *db, robj *key)函数位于src/db.c。

  1. /*-----------------------------------------------------------------------------
  2. * Expires API
  3. *----------------------------------------------------------------------------*/
  4. int removeExpire(redisDb *db, robj *key) {
  5. /* An expire may only be removed if there is a corresponding entry in the
  6. * main dict. Otherwise, the key will never be freed. */
  7. redisAssertWithInfo(NULL,key,dictFind(db->dict,key->ptr) != NULL);
  8. return dictDelete(db->expires,key->ptr) == DICT_OK;
  9. }
  10. void setExpire(redisDb *db, robj *key, long long when) {
  11. dictEntry *kde, *de;
  12. /* Reuse the sds from the main dict in the expire dict */
  13. kde = dictFind(db->dict,key->ptr);
  14. redisAssertWithInfo(NULL,key,kde != NULL);
  15. de = dictReplaceRaw(db->expires,dictGetKey(kde));
  16. dictSetSignedIntegerVal(de,when);
  17. }
  18. /* Return the expire time of the specified key, or -1 if no expire
  19. * is associated with this key (i.e. the key is non volatile) */
  20. long long getExpire(redisDb *db, robj *key) {
  21. dictEntry *de;
  22. /* No expire? return ASAP */
  23. if (dictSize(db->expires) == 0 ||
  24. (de = dictFind(db->expires,key->ptr)) == NULL) return -1;
  25. /* The entry was found in the expire dict, this means it should also
  26. * be present in the main dict (safety check). */
  27. redisAssertWithInfo(NULL,key,dictFind(db->dict,key->ptr) != NULL);
  28. return dictGetSignedIntegerVal(de);
  29. }
  30. /* Propagate expires into slaves and the AOF file.
  31. * When a key expires in the master, a DEL operation for this key is sent
  32. * to all the slaves and the AOF file if enabled.
  33. *
  34. * This way the key expiry is centralized in one place, and since both
  35. * AOF and the master->slave link guarantee operation ordering, everything
  36. * will be consistent even if we allow write operations against expiring
  37. * keys. */
  38. void propagateExpire(redisDb *db, robj *key) {
  39. robj *argv[2];
  40. argv[0] = shared.del;
  41. argv[1] = key;
  42. incrRefCount(argv[0]);
  43. incrRefCount(argv[1]);
  44. if (server.aof_state != REDIS_AOF_OFF)
  45. feedAppendOnlyFile(server.delCommand,db->id,argv,2);
  46. replicationFeedSlaves(server.slaves,db->id,argv,2);
  47. decrRefCount(argv[0]);
  48. decrRefCount(argv[1]);
  49. }
  50. int expireIfNeeded(redisDb *db, robj *key) {
  51. mstime_t when = getExpire(db,key);
  52. mstime_t now;
  53. if (when < 0) return 0; /* No expire for this key */
  54. /* Don't expire anything while loading. It will be done later. */
  55. if (server.loading) return 0;
  56. /* If we are in the context of a Lua script, we claim that time is
  57. * blocked to when the Lua script started. This way a key can expire
  58. * only the first time it is accessed and not in the middle of the
  59. * script execution, making propagation to slaves / AOF consistent.
  60. * See issue #1525 on Github for more information. */
  61. now = server.lua_caller ? server.lua_time_start : mstime();
  62. /* If we are running in the context of a slave, return ASAP:
  63. * the slave key expiration is controlled by the master that will
  64. * send us synthesized DEL operations for expired keys.
  65. *
  66. * Still we try to return the right information to the caller,
  67. * that is, 0 if we think the key should be still valid, 1 if
  68. * we think the key is expired at this time. */
  69. if (server.masterhost != NULL) return now > when;
  70. /* Return when this key has not expired */
  71. if (now <= when) return 0;
  72. /* Delete the key */
  73. server.stat_expiredkeys++;
  74. propagateExpire(db,key);
  75. notifyKeyspaceEvent(REDIS_NOTIFY_EXPIRED,
  76. "expired",key,db->id);
  77. return dbDelete(db,key);
  78. }
  79. /*-----------------------------------------------------------------------------
  80. * Expires Commands
  81. *----------------------------------------------------------------------------*/
  82. /* This is the generic command implementation for EXPIRE, PEXPIRE, EXPIREAT
  83. * and PEXPIREAT. Because the commad second argument may be relative or absolute
  84. * the "basetime" argument is used to signal what the base time is (either 0
  85. * for *AT variants of the command, or the current time for relative expires).
  86. *
  87. * unit is either UNIT_SECONDS or UNIT_MILLISECONDS, and is only used for
  88. * the argv[2] parameter. The basetime is always specified in milliseconds. */
  89. void expireGenericCommand(redisClient *c, long long basetime, int unit) {
  90. robj *key = c->argv[1], *param = c->argv[2];
  91. long long when; /* unix time in milliseconds when the key will expire. */
  92. if (getLongLongFromObjectOrReply(c, param, &when, NULL) != REDIS_OK)
  93. return;
  94. if (unit == UNIT_SECONDS) when *= 1000;
  95. when += basetime;
  96. /* No key, return zero. */
  97. if (lookupKeyRead(c->db,key) == NULL) {
  98. addReply(c,shared.czero);
  99. return;
  100. }
  101. /* EXPIRE with negative TTL, or EXPIREAT with a timestamp into the past
  102. * should never be executed as a DEL when load the AOF or in the context
  103. * of a slave instance.
  104. *
  105. * Instead we take the other branch of the IF statement setting an expire
  106. * (possibly in the past) and wait for an explicit DEL from the master. */
  107. if (when <= mstime() && !server.loading && !server.masterhost) {
  108. robj *aux;
  109. redisAssertWithInfo(c,key,dbDelete(c->db,key));
  110. server.dirty++;
  111. /* Replicate/AOF this as an explicit DEL. */
  112. aux = createStringObject("DEL",3);
  113. rewriteClientCommandVector(c,2,aux,key);
  114. decrRefCount(aux);
  115. signalModifiedKey(c->db,key);
  116. notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",key,c->db->id);
  117. addReply(c, shared.cone);
  118. return;
  119. } else {
  120. setExpire(c->db,key,when);
  121. addReply(c,shared.cone);
  122. signalModifiedKey(c->db,key);
  123. notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"expire",key,c->db->id);
  124. server.dirty++;
  125. return;
  126. }
  127. }
  128. void expireCommand(redisClient *c) {
  129. expireGenericCommand(c,mstime(),UNIT_SECONDS);
  130. }
  131. void expireatCommand(redisClient *c) {
  132. expireGenericCommand(c,0,UNIT_SECONDS);
  133. }
  134. void pexpireCommand(redisClient *c) {
  135. expireGenericCommand(c,mstime(),UNIT_MILLISECONDS);
  136. }
  137. void pexpireatCommand(redisClient *c) {
  138. expireGenericCommand(c,0,UNIT_MILLISECONDS);
  139. }
  140. void ttlGenericCommand(redisClient *c, int output_ms) {
  141. long long expire, ttl = -1;
  142. /* If the key does not exist at all, return -2 */
  143. if (lookupKeyRead(c->db,c->argv[1]) == NULL) {
  144. addReplyLongLong(c,-2);
  145. return;
  146. }
  147. /* The key exists. Return -1 if it has no expire, or the actual
  148. * TTL value otherwise. */
  149. expire = getExpire(c->db,c->argv[1]);
  150. if (expire != -1) {
  151. ttl = expire-mstime();
  152. if (ttl < 0) ttl = 0;
  153. }
  154. if (ttl == -1) {
  155. addReplyLongLong(c,-1);
  156. } else {
  157. addReplyLongLong(c,output_ms ? ttl : ((ttl+500)/1000));
  158. }
  159. }
  160. void ttlCommand(redisClient *c) {
  161. ttlGenericCommand(c, 0);
  162. }
  163. void pttlCommand(redisClient *c) {
  164. ttlGenericCommand(c, 1);
  165. }
  166. void persistCommand(redisClient *c) {
  167. dictEntry *de;
  168. de = dictFind(c->db->dict,c->argv[1]->ptr);
  169. if (de == NULL) {
  170. addReply(c,shared.czero);
  171. } else {
  172. if (removeExpire(c->db,c->argv[1])) {
  173. addReply(c,shared.cone);
  174. server.dirty++;
  175. } else {
  176. addReply(c,shared.czero);
  177. }
  178. }
  179. }

但仅是这样是不够的,因为可能存在一些key永远不会被再次访问到,这些设置了过期时间的key也是需要在过期后被删除的,我们甚至可以将这种情况看作是一种内存泄露----无用的垃圾数据占用了大量的内存,而服务器却不会自己去释放它们,这对于运行状态非常依赖于内存的Redis服务器来说,肯定不是一个好消息

主动删除

先说一下时间事件,对于持续运行的服务器来说, 服务器需要定期对自身的资源和状态进行必要的检查和整理, 从而让服务器维持在一个健康稳定的状态, 这类操作被统称为常规操作(cron job)

在 Redis 中, 常规操作由 redis.c/serverCron 实现, 它主要执行以下操作:

更新服务器的各类统计信息,比如时间、内存占用、数据库占用情况等。

清理数据库中的过期键值对。

对不合理的数据库进行大小调整。

关闭和清理连接失效的客户端。

尝试进行 AOF 或 RDB 持久化操作。

如果服务器是主节点的话,对附属节点进行定期同步。

如果处于集群模式的话,对集群进行定期同步和连接测试。

Redis 将 serverCron 作为时间事件来运行, 从而确保它每隔一段时间就会自动运行一次, 又因为 serverCron 需要在 Redis 服务器运行期间一直定期运行, 所以它是一个循环时间事件: serverCron 会一直定期执行,直到服务器关闭为止。

在 Redis 2.6 版本中, 程序规定 serverCron 每秒运行 10 次, 平均每 100 毫秒运行一次。 从 Redis 2.8 开始, 用户可以通过修改 hz选项来调整 serverCron 的每秒执行次数。

也叫定时删除,这里的“定期”指的是Redis定期触发的清理策略,由位于src/redis.c的activeExpireCycle(void)函数来完成。

serverCron是由redis的事件框架驱动的定位任务,这个定时任务中会调用activeExpireCycle函数,针对每个db在限制的时间REDIS_EXPIRELOOKUPS_TIME_LIMIT内迟可能多的删除过期key,之所以要限制时间是为了防止过长时间 的阻塞影响redis的正常运行。这种主动删除策略弥补了被动删除策略在内存上的不友好。

因此,Redis会周期性的随机测试一批设置了过期时间的key并进行处理。测试到的已过期的key将被删除。典型的方式为,Redis每秒做10次如下的步骤:

(1)随机测试100个设置了过期时间的key

(2)删除所有发现的已过期的key

(3)若删除的key超过25个则重复步骤1

这是一个基于概率的简单算法,基本的假设是抽出的样本能够代表整个key空间,redis持续清理过期的数据直至将要过期的key的百分比降到了25%以下。这也意味着在任何给定的时刻已经过期但仍占据着内存空间的key的量最多为每秒的写操作量除以4.

Redis-3.0.0中的默认值是10,代表每秒钟调用10次后台任务。

除了主动淘汰的频率外,Redis对每次淘汰任务执行的最大时长也有一个限定,这样保证了每次主动淘汰不会过多阻塞应用请求,以下是这个限定计算公式:

  1. #define ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC 25 /* CPU max % for keys collection */
  2. ...
  3. timelimit = 1000000*ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC/server.hz/100;

hz调大将会提高Redis主动淘汰的频率,如果你的Redis存储中包含很多冷数据占用内存过大的话,可以考虑将这个值调大,但Redis作者建议这个值不要超过100。我们实际线上将这个值调大到100,观察到CPU会增加2%左右,但对冷数据的内存释放速度确实有明显的提高(通过观察keyspace个数和used_memory大小)。

可以看出timelimit和server.hz是一个倒数的关系,也就是说hz配置越大,timelimit就越小。换句话说是每秒钟期望的主动淘汰频率越高,则每次淘汰最长占用时间就越短。这里每秒钟的最长淘汰占用时间是固定的250ms(1000000*ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC/100),而淘汰频率和每次淘汰的最长时间是通过hz参数控制的。

从以上的分析看,当redis中的过期key比率没有超过25%之前,提高hz可以明显提高扫描key的最小个数。假设hz为10,则一秒内最少扫描200个key(一秒调用10次*每次最少随机取出20个key),如果hz改为100,则一秒内最少扫描2000个key;另一方面,如果过期key比率超过25%,则扫描key的个数无上限,但是cpu时间每秒钟最多占用250ms。

当REDIS运行在主从模式时,只有主结点才会执行上述这两种过期删除策略,然后把删除操作”del key”同步到从结点。

maxmemory

当前已用内存超过maxmemory限定时,触发主动清理策略:

volatile-lru:只对设置了过期时间的key进行LRU(默认值)

allkeys-lru : 删除lru算法的key

volatile-random:随机删除即将过期key

allkeys-random:随机删除

volatile-ttl : 删除即将过期的

noeviction : 永不过期,返回错误当mem_used内存已经超过maxmemory的设定,对于所有的读写请求,都会触发redis.c/freeMemoryIfNeeded(void)函数以清理超出的内存。注意这个清理过程是阻塞的,直到清理出足够的内存空间。所以如果在达到maxmemory并且调用方还在不断写入的情况下,可能会反复触发主动清理策略,导致请求会有一定的延迟。

当mem_used内存已经超过maxmemory的设定,对于所有的读写请求,都会触发redis.c/freeMemoryIfNeeded(void)函数以清理超出的内存。注意这个清理过程是阻塞的,直到清理出足够的内存空间。所以如果在达到maxmemory并且调用方还在不断写入的情况下,可能会反复触发主动清理策略,导致请求会有一定的延迟。

清理时会根据用户配置的maxmemory-policy来做适当的清理(一般是LRU或TTL),这里的LRU或TTL策略并不是针对redis的所有key,而是以配置文件中的maxmemory-samples个key作为样本池进行抽样清理。

maxmemory-samples在redis-3.0.0中的默认配置为5,如果增加,会提高LRU或TTL的精准度,redis作者测试的结果是当这个配置为10时已经非常接近全量LRU的精准度了,并且增加maxmemory-samples会导致在主动清理时消耗更多的CPU时间,建议:

(1)尽量不要触发maxmemory,最好在mem_used内存占用达到maxmemory的一定比例后,需要考虑调大hz以加快淘汰,或者进行集群扩容。

(2)如果能够控制住内存,则可以不用修改maxmemory-samples配置;如果Redis本身就作为LRU cache服务(这种服务一般长时间处于maxmemory状态,由Redis自动做LRU淘汰),可以适当调大maxmemory-samples。

以下是上文中提到的配置参数的说明

  1. # Redis calls an internal function to perform many background tasks, like
  2. # closing connections of clients in timeout, purging expired keys that are
  3. # never requested, and so forth.
  4. #
  5. # Not all tasks are performed with the same frequency, but Redis checks for
  6. # tasks to perform according to the specified "hz" value.
  7. #
  8. # By default "hz" is set to 10. Raising the value will use more CPU when
  9. # Redis is idle, but at the same time will make Redis more responsive when
  10. # there are many keys expiring at the same time, and timeouts may be
  11. # handled with more precision.
  12. #
  13. # The range is between 1 and 500, however a value over 100 is usually not
  14. # a good idea. Most users should use the default of 10 and raise this up to
  15. # 100 only in environments where very low latency is required.
  16. hz 10
  17. # MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
  18. # is reached. You can select among five behaviors:
  19. #
  20. # volatile-lru -> remove the key with an expire set using an LRU algorithm
  21. # allkeys-lru -> remove any key according to the LRU algorithm
  22. # volatile-random -> remove a random key with an expire set
  23. # allkeys-random -> remove a random key, any key
  24. # volatile-ttl -> remove the key with the nearest expire time (minor TTL)
  25. # noeviction -> don't expire at all, just return an error on write operations
  26. #
  27. # Note: with any of the above policies, Redis will return an error on write
  28. # operations, when there are no suitable keys for eviction.
  29. #
  30. # At the date of writing these commands are: set setnx setex append
  31. # incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
  32. # sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
  33. # zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
  34. # getset mset msetnx exec sort
  35. #
  36. # The default is:
  37. #
  38. maxmemory-policy noeviction
  39. # LRU and minimal TTL algorithms are not precise algorithms but approximated
  40. # algorithms (in order to save memory), so you can tune it for speed or
  41. # accuracy. For default Redis will check five keys and pick the one that was
  42. # used less recently, you can change the sample size using the following
  43. # configuration directive.
  44. #
  45. # The default of 5 produces good enough results. 10 Approximates very closely
  46. # true LRU but costs a bit more CPU. 3 is very fast but not very accurate.
  47. #
  48. maxmemory-samples 5

Replication link和AOF文件中的过期处理

为了获得正确的行为而不至于导致一致性问题,当一个key过期时DEL操作将被记录在AOF文件并传递到所有相关的slave。也即过期删除操作统一在master实例中进行并向下传递,而不是各salve各自掌控。

这样一来便不会出现数据不一致的情形。当slave连接到master后并不能立即清理已过期的key(需要等待由master传递过来的DEL操作),slave仍需对数据集中的过期状态进行管理维护以便于在slave被提升为master会能像master一样独立的进行过期处理。

以上就是redis数据过期时间设置的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行