Spring-Data-Redis之RedisTemplate的使用

来源:互联网 发布:黄金烤鸡腿堡 知乎 编辑:程序博客网 时间:2024/05/28 04:53

        上篇博客是Spring-Data-Redis的实例,接着上篇的内容,这篇博客介绍一下RedisTemplate的详细方法。


功能介绍


        大部分的用户都喜欢用RedisTemplate,它相应的包是org.springframework.data.redis.core。该模板实际是Redis模块的核心类,因为它的功能丰富。模板为Redis交互提供了高级抽象。虽然RedisConnection提供接受和返回二进制值(字节数组)的低级方法,但该模板可以处理序列化和连接管理,使得用户不需要处理太多的细节。


        此外,模板提供了操作视图(按照Redis命令参考分组),它们提供了丰富的、现成的接口用于对特定类型或者特定键的操作(通过KeyBound接口),如下所述:


接口描述

Key类型操作

ValueOperations

操作Redis String(或者Value)类型数据

ListOperations

操作Redis List类型数据

SetOperations

操作Redis Set类型数据

ZSetOperations

操作Redis ZSet(或者Sorted Set)类型数据

HashOperations

操作Redis Hash类型数据

HyperLogLogOperations

操作Redis HyperLogLog类型数据,比如:pfadd,pfcount,...

GeoOperations

操作Redis Geospatial类型数据,比如:GEOADD,GEORADIUS,…​)

Key绑定操作

BoundValueOperations

Redis字符串(或值)键绑定操作

BoundListOperations

Redis列表键绑定操作

BoundSetOperations 

Redis Set键绑定操作

BoundZSetOperations

Redis ZSet(或Sorted Set)键绑定操作

BoundHashOperations

Redis Hash键绑定操作

BoundGeoOperations

Redis Geospatial 键绑定操作

        一旦经过配置,该模板就是线程安全的,它可以被多个实例重复使用。


        开箱即用,RedisTemplate使用了基于Java的串行器来进行大部分的操作。这就意味着,任何对象通过模板的读写都会通过Java来进行序列化/反序列化。该模板的序列化机制改变起来也很容易,并且Redis模块在org.springframework.data.redis.serializer包中提供了多种可用的实现,详情请参考Serializers。你也可以通过设置enableDefaultSerializer属性为false,将其他的序列化实现都设置成null,并将RedisTemplate和原生的字节数组一起使用。注意该模板的key不允许为null值,除非底层序列化程序可以接受。获取更多序列化器的信息,请阅读javadoc。


使用实例


        上面是官网上介绍的大概的功能,RedisTemplate提供了很多方法,详细具体的方法请参考官网:http://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/RedisTemplate.html。以上篇博客中的实例为基础,下面代码介绍常用的各种操作。


public class RedisTemplateTest {@SuppressWarnings("rawtypes")@Autowiredprivate RedisTemplate redisTemplate;@SuppressWarnings("unchecked")public void findAll() {// -----------------String类型数据操作 start--------------------ValueOperations<String, String> stringOperations = redisTemplate        .opsForValue();// String类型数据存储,不设置过期时间,永久性保存stringOperations.set("string1", "fiala");// String类型数据存储,设置过期时间为80秒,采用TimeUnit控制时间单位stringOperations.set("string2", "fiala", 80, TimeUnit.SECONDS);// 判断key值是否存在,存在则不存储,不存在则存储stringOperations.setIfAbsent("string1", "my fiala");stringOperations.setIfAbsent("string3", "my fiala");String value1 = stringOperations.get("string1");String value2 = stringOperations.get("string3");System.out.println(value1);System.out.println(value2);// -----------------String类型数据操作 end--------------------// -----------------其他值类型数据操作 start--------------------Demo demo = new Demo();demo.setId("1");demo.setName("fiala");List<Demo> demos = new ArrayList<Demo>();ValueOperations<String, Object> valueOperations = redisTemplate        .opsForValue();// 设置value为对象类型,且不设置过期时间,默认永久valueOperations.set("value1", demo);// 设置value为对象类型,设置过期时间为80秒,时间单位由TimeUnit控制valueOperations.set("value2", demos, 80, TimeUnit.SECONDS);Demo demo1 = (Demo) valueOperations.get("value1");System.out.println(demo1.toString());// -----------------其他值类型数据操作 end--------------------// -----------------List数据类型操作 start------------------ListOperations<String, Object> listOperations = redisTemplate        .opsForList();for (int i = 0; i < 5; i++) {Demo listDemo = new Demo();listDemo.setId("\"" + i + "\"");listDemo.setName("fiala" + i);listOperations.leftPush("list1", listDemo);listOperations.rightPush("list2", listDemo);}// 可给数据排序Demo demo2 = (Demo) listOperations.leftPop("list1");Demo demo3 = (Demo) listOperations.rightPop("list2");System.out.println(demo2.toString());System.out.println(demo3.toString());// -----------------List数据类型操作 end------------------// -----------------set数据类型操作 start------------------SetOperations<String, Object> setOperations = redisTemplate.opsForSet();for (int i = 0; i < 5; i++) {Demo setDemo = new Demo();setDemo.setId("\"" + i + "\"");setDemo.setName("fiala" + i);setOperations.add("set1", setDemo);}Demo demo4 = (Demo) setOperations.pop("set1");System.out.println(demo4.toString());// -----------------set数据类型操作 end------------------// -----------------zset数据类型操作 start------------------ZSetOperations<String, Object> zSetOperations = redisTemplate        .opsForZSet();zSetOperations.add("zset", "fiala", 0);zSetOperations.add("zset", "my fiala", 1);System.out.println(zSetOperations.rangeByScore("zset", 0, 1));// -----------------zset数据类型操作 end------------------// -----------------hash数据类型操作 start------------------HashOperations<String, Object, Object> hashOperations = redisTemplate        .opsForHash();Map<String, String> map = new HashMap<String, String>();map.put("map1", "fiala1");map.put("map2", "fiala2");hashOperations.putAll("hash", map);System.out.println(hashOperations.entries("hash"));// -----------------hash数据类型操作 start------------------}}

原创粉丝点击