redisTemplate介绍
spring封装了RedisTemplate<K,V>对象来进行对redis的各种操作,它支持所有的 redis 原生的api。 K一般是String 类型的key,V一般是Object或者String类型的JSON数据具体可以根据自己的业务制定Value
StringRedisTemplate和RedisTemplate的区别及使用方法
相同点:
StringRedisTemplate继承了RedisTemplate,所以两者对Redis的操作方法是一致的
不同点:
两者的数据是不共通的;也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。其实他们两者之间的区别主要在于:
他们使用的序列化类:RedisTemplate使用的是JdkSerializationRedisSerializer 存入数据会将数据先序列化成字节数组然后在存入Redis数据库。 StringRedisTemplate使用的是StringRedisSerializer
注意事项:
当你的redis数据库里面本来存的是字符串数据或者你要存取的数据就是字符串类型数据的时候,那么你就使用StringRedisTemplate即可。
但是如果你的数据是复杂的对象类型,而取出的时候又不想做任何的数据转换,直接从Redis里面取出一个对象,那么使用RedisTemplate是更好的选择。
redisTemplate 定义了对5种数据结构操作
redisTemplate.opsForValue(); //操作字符串
redisTemplate.opsForHash(); //操作hash
redisTemplate.opsForList(); //操作list
redisTemplate.opsForSet(); //操作set
redisTemplate.opsForZSet(); //操作有序set
Redis的String数据结构 (推荐使用StringRedisTemplate)
ValueOperations可以对String数据结构进行操作:
- set void set(K key, V value);
使用:redisTemplate.opsForValue().set("name","tom");
结果:redisTemplate.opsForValue().get("name") 输出结果为tom
- set void set(K key, V value, long timeout, TimeUnit unit);
使用:redisTemplate.opsForValue().set("name","tom",10, TimeUnit.SECONDS);
结果:redisTemplate.opsForValue().get("name")由于设置的是10秒失效,十秒之内查询有结果,十秒之后返回为null
- setIfAbsent Boolean setIfAbsent(K key, V value); 设置value时先判断该值原来是否存在
使用:System.out.println(template.opsForValue().setIfAbsent("multi1","multi1"));//false multi1之前已经存在
System.out.println(template.opsForValue().setIfAbsent("multi111","multi111"));//true multi111之前不存在
结果:false
true
- get V get(Object key); 获取key对应的值
使用:template.opsForValue().set("key","hello world");
System.out.println("***************"+template.opsForValue().get("key"));
结果:***************hello world
- getAndSet V getAndSet(K key, V value); 设置键的字符串值并返回其旧值
使用:template.opsForValue().set("getSetTest","test");
System.out.println(template.opsForValue().getAndSet("getSetTest","test2"));
结果:test
Redis的List数据结构
ListOperations专门操作list列表:
- List range(K key, long start, long end); 返回存储在键中的列表的指定元素。偏移开始和停止是基于零的索引,其中0是列表的第一个元素(列表的头部),1是下一个元素
使用:System.out.println(template.opsForList().range("list",0,-1));
结果:[c#, c++, python, java, c#, c#]
- void trim(K key, long start, long end); 修剪现有列表,使其只包含指定的指定范围的元素,起始和停止都是基于0的索引
使用:
System.out.println(template.opsForList().range("list",0,-1));
template.opsForList().trim("list",1,-1);//裁剪第一个元素
System.out.println(template.opsForList().range("list",0,-1));
结果:[c#, c++, python, java, c#, c#]
[c++, python, java, c#, c#]
- Long size(K key); 返回存储在键中的列表的长度。如果键不存在,则将其解释为空列表,并返回0。当key存储的值不是列表时返回错误。
使用:System.out.println(template.opsForList().size("list"));
结果:6
- Long leftPush(K key, V value); 将所有指定的值插入存储在键的列表的头部。如果键不存在,则在执行推送操作之前将其创建为空列表。(从左边插入)
使用: template.opsForList().leftPush("list","java");
template.opsForList().leftPush("list","python");
template.opsForList().leftPush("list","c++");
结果:返回的结果为推送操作后的列表的长度
- V leftPop(K key); 弹出最左边的元素,弹出之后该值在列表中将不复存在
使用: System.out.println(template.opsForList().range("list",0,-1));
System.out.println(template.opsForList().leftPop("list"));
System.out.println(template.opsForList().range("list",0,-1));
结果:
[c++, python, oc, java, c#, c#]
c++
[python, oc, java, c#, c#]
- V leftPop(K key, long timeout, TimeUnit unit);
移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
使用:用法与 leftPop(K key);一样
RedisTemplate操作key
- 操作key的失效时间
1.RedisTemplate.expire(String key, long timeout, TimeUnit unit)
设置key的失效时间,timeout是时间参数,timeunit是时间单位,返回值是布尔
2.RedisTemplate.expireAt(String key, Date date)
设置key在一个时间点失效,返回值是布尔
3.RedisTemplate.getExpire(String key)
获取key的存活时间,返回值是long
4.RedisTemplate.getExpire(String key, TimeUnit timeUnit)
获取key的存活时间,加上了时间单位,返回值是long
注:TimeUnit是java.util.concurrent包下面的一个类,表示给定单元粒度的时间段
常用的颗粒度
TimeUnit.DAYS //天
TimeUnit.HOURS //小时
TimeUnit.MINUTES //分钟
TimeUnit.SECONDS //秒
TimeUnit.MILLISECONDS //毫秒
如果想要更全的api可以参考 使用RedisTemplate访问Redis数据结构API大全