Redis-14Redis超时命令

版权声明:【show me the code ,change the world】 https://blog.csdn.net/yangshangwei/article/details/82877781

概述

正如 Java 虚拟机,它提供 GC的功能,来保证 Java 程序使用过且不再使用的 Java 对象及时的从内存中释放掉,从而保证内存空间可用。当程序编写不当或考虑欠缺的时候(比如读入大文件),内存就可能存储不下运行所需要的数据,那么 Java虚拟机就会抛出内存溢出的异常而导致服务失败。同样, Redis 也是基于内存而运行的数据集合,也存在着对内存垃圾的回收和管理的问题。

Redis 基于内存 ,而内 存对于一个系统是最为宝贵的资源,而且它远远没有磁盘那么大,所以对于 Redis 的键值对的内存回收也是一个十分重要的问题,如果操作不当会产生 Redis岩机的问题,使得系统性能低下。

对于 Redis 而言, del 命令可以删除一些键值对,所以 Redis 比 Java 虚拟机更灵活,允许删除一部分的键值对。与此同时,当内存运行空间满了之后 ,它还会按照回收机制去自动回收一些键值对,这和 Java 虚拟机又有相似之处,但是当垃圾进行回收的时候,又有可能执行回收而引发系统停顿,因此选择适当的回收机制和时间将有利于系统性能的提高。


Redis 的超时命令

命令 说明 备注
persist key 持久化 key,取消超时时间 移除 key 的超时时间
ttl key 查看 key 的超时时间 以秒计算,-1 代表没有超时时间,如果不存在 key 或者key 已经超时则为-2
expire key seconds 设置跑时时间戳 以秒为单位
expireat key timestamp 设置超时时间点 用 uninx 时间戳确定
pptl key milliseconds 查看 key 的超时时间戳 用毫秒计算
pexpire key 设置键值超时的时间 以毫秒为单位
pexpireat key stamptimes 设置超时时间点 以毫秒为单位的 uninx 时间戳

Spring操作 Redis 超时命令

package com.artisan.redis.expire;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;

public class SpringRedisExpireDemo {

	@SuppressWarnings({ "unchecked", "rawtypes", "resource" })
	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/spring-redis-string.xml");
		RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);

		redisTemplate.execute((RedisOperations ops) -> {

			ops.boundValueOps("key1").set("value1");
			String value = (String) ops.boundValueOps("key1").get();
			System.out.println("value=" + value);

			long expSecond = ops.getExpire("key1");
			System.out.println("expSecond:" + expSecond);

			// 设置120秒
				Boolean flag = ops.expire("key1", 120L, TimeUnit.SECONDS);
				System.out.println("设置超时时间:" + flag);
				System.out.println("过期时间:" + ops.getExpire("key1") + "秒");

				// 持久化 key,取消超时时间
				flag = ops.persist("key1");
				System.out.println("取消超时时间:" + flag);
				System.out.println("过期时间:" + ops.getExpire("key1"));

				Date date = new Date();
				date.setTime(System.currentTimeMillis() + 120000);
				// 设置超时时间点
				flag = ops.expireAt("key1", date);
				System.out.println("设置超时时间:" + flag);
				System.out.println("过期时间:" + ops.getExpire("key1"));
				return null;
			});
	}

}


INFO : org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@73a8dfcc: startup date [Fri Sep 28 12:57:39 CST 2018]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring/spring-redis-string.xml]
value=value1
expSecond:-1
设置超时时间:true
过期时间:120秒
取消超时时间:true
过期时间:-1
设置超时时间:true
过期时间:155



如果 key, 超时了, Redis 会回收 key 的存储空间吗 ?

Redis 的 key 超时不会被其自动回收,它只会标识哪些键值对超时了

  • 好处: 如果一个很大的键值对超时,比如一个列表或者哈希结构,存在数以百万个元素,要对其回收需要很长的时间 如果采用超时回收,则可能产生停顿
  • 坏处: 超时的键值对会浪费比较多的空间。

Redis对超时键值对的回收策略

Redis 提供两种方式回收这些超 时键值对, 它们是定时回收和惰性回收。

  • 定时回收是指在确定的某个时间触发一段代码,回收超时的键值对 。
  • 惰性回收则是 当 一个超时的键,被再次用 get 命令访问时,将触发 Redis 将其从内
    存中清空。

定时回收

定时回收可以完全回收那些超时的键值对,但是缺点也很明显,如果这些键值对比较多, 则 Redis 需要运行较长的时间,从而导致停顿。所以系统设计者一般会选择在没有业务发生的时刻触发 Redis 的定时回收,以便清理超时的键值对


惰性回收

对于惰性回收而言,它的优势是可以指定回收超时的键值对。 它的缺点是要执行一个莫名其妙的 get 操作,或者在某些时候,我们也难以判断哪些键值对已经超时。

小结

无论是定时回收还是惰性回收,都要依据自身的特点去定制策略,如果一个键值对,存储的是数 以千万的数据 ,使用 expire 命令使其到达一个时间超时,然后用 get 命令访问触发其回收,显然会付出停顿代价,这是现实中需要考虑的 。


代码

代码托管到了 https://github.com/yangshangwei/redis_learn

猜你喜欢

转载自blog.csdn.net/yangshangwei/article/details/82877781