一、SpringDataRedis简介

1、Redis

redis是一款开源的Key-Value数据库,运行在内存中,由C语言编写。企业开发通常采用Redis来实现缓存。同类的产品还有memcache 、memcached 等。

2、Jedis

Jedis是Redis官方推出的一款面向Java的客户端,提供了很多接口供Java语言调用。可以在Redis官网下载,当然还有一些开源爱好者提供的客户端,如Jredis、SRP等等,推荐使用Jedis。

3、Spring Data Redis

Spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
spring-data-redis针对jedis提供了如下功能:
spring-data-redis针对jedis提供了如下功能:
连接池自动管理,提供了一个高度封装的“RedisTemplate”类
针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
ValueOperations:简单K-V操作
SetOperations:set类型数据操作
ZSetOperations:zset类型数据操作
HashOperations:针对map类型的数据操作
ListOperations:针对list类型的数据操作
提供了对key的“bound”(绑定)便捷化操作API,可以通过bound封装指定的key,然后进行一系列的操作而无须“显式”的再次指定Key,即BoundKeyOperations:
BoundValueOperations
BoundSetOperations
BoundListOperations
BoundSetOperations
BoundHashOperations
将事务操作封装,有容器控制。
针对数据的“序列化/反序列化”,提供了多种可选择策略(RedisSerializer)
JdkSerializationRedisSerializer:POJO对象的存取场景,使用JDK本身序列化机制,将pojo类通过ObjectInputStream/ObjectOutputStream进行序列化操作,最终redis-server中将存储字节序列。是目前最常用的序列化策略。

StringRedisSerializer:Key或者value为字符串的场景,根据指定的charset对数据的字节序列编码成string,是“new String(bytes, charset)”和“string.getBytes(charset)”的直接封装。是最轻量级和高效的策略。

JacksonJsonRedisSerializer:jackson-json工具提供了javabean与json之间的转换能力,可以将pojo实例序列化成json格式存储在redis中,也可以将json格式的数据转换成pojo实例。因为jackson工具在序列化和反序列化时,需要明确指定Class类型,因此此策略封装起来稍微复杂。【需要jackson-mapper-asl工具支持】

二、RedisTemplate中API使用

1、pom.xml依赖

  1. <!--Redis-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>

2、配置文件

  1. # Redis服务器连接端口
  2. spring.redis.port=6379
  3. # Redis服务器地址
  4. spring.redis.host=127.0.0.1
  5. # Redis数据库索引(默认为0)
  6. spring.redis.database=0
  7. # Redis服务器连接密码(默认为空)
  8. spring.redis.password=
  9. # 连接池最大连接数(使用负值表示没有限制)
  10. spring.redis.jedis.pool.max-active=8
  11. # 连接池最大阻塞等待时间(使用负值表示没有限制)
  12. spring.redis.jedis.pool.max-wait=-1ms
  13. # 连接池中的最大空闲连接
  14. spring.redis.jedis.pool.max-idle=8
  15. # 连接池中的最小空闲连接
  16. spring.redis.jedis.pool.min-idle=0
  17. # 连接超时时间(毫秒)
  18. spring.redis.timeout=5000ms

3、RedisTemplate的直接方法

首先使用@Autowired注入RedisTemplate(后面直接使用,就不特殊说明)

  1. @Autowired
  2. private RedisTemplate redisTemplate;

1、删除单个key

  1. // 删除key
  2. public void delete(String key){
  3. redisTemplate.delete(key);
  4. }

2、删除多个key

  1. // 删除多个key
  2. public void deleteKey (String ...keys){
  3. redisTemplate.delete(keys);
  4. }

3、指定key的失效时间

  1. // 指定key的失效时间
  2. public void expire(String key,long time){
  3. redisTemplate.expire(key,time,TimeUnit.MINUTES);
  4. }

4、根据key获取过期时间

  1. // 根据key获取过期时间
  2. public long getExpire(String key){
  3. Long expire = redisTemplate.getExpire(key);
  4. return expire;
  5. }

5、判断key是否存在

  1. // 判断key是否存在
  2. public boolean hasKey(String key){
  3. return redisTemplate.hasKey(key);
  4. }

4、String类型相关操作

1)、添加缓存(2/3是1的递进值)
  1. //1、通过redisTemplate设置值
  2. redisTemplate.boundValueOps("StringKey").set("StringValue");
  3. redisTemplate.boundValueOps("StringKey").set("StringValue",1, TimeUnit.MINUTES);
  4. //2、通过BoundValueOperations设置值
  5. BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
  6. stringKey.set("StringVaule");
  7. stringKey.set("StringValue",1, TimeUnit.MINUTES);
  8. //3、通过ValueOperations设置值
  9. ValueOperations ops = redisTemplate.opsForValue();
  10. ops.set("StringKey", "StringVaule");
  11. ops.set("StringValue","StringVaule",1, TimeUnit.MINUTES);

2)、设置过期时间(单独设置)
  1. redisTemplate.boundValueOps("StringKey").expire(1,TimeUnit.MINUTES);
  2. redisTemplate.expire("StringKey",1,TimeUnit.MINUTES);

3)、获取缓存值(2/3是1的递进值)
  1. //1、通过redisTemplate设置值
  2. String str1 = (String) redisTemplate.boundValueOps("StringKey").get();
  3. //2、通过BoundValueOperations获取值
  4. BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
  5. String str2 = (String) stringKey.get();
  6. //3、通过ValueOperations获取值
  7. ValueOperations ops = redisTemplate.opsForValue();
  8. String str3 = (String) ops.get("StringKey");

4)、删除key
  1. Boolean result = redisTemplate.delete("StringKey");

5)、顺序递增
  1. redisTemplate.boundValueOps("StringKey").increment(3L); 3是步长

6)、顺序递减
  1. redisTemplate.boundValueOps("StringKey").increment(-3L); increment是增长
  2. stringRedisTemplate.boundValueOps("StringKey").decrement(3L); decrement是减少

5、Hash类型相关操作

1)、添加缓存(2/3是1的递进值)
  1. //1、通过redisTemplate设置值
  2. redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashVaue");
  3. //2、通过BoundValueOperations设置值
  4. BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
  5. hashKey.put("SmallKey", "HashVaue");
  6. //3、通过ValueOperations设置值
  7. HashOperations hashOps = redisTemplate.opsForHash();
  8. hashOps.put("HashKey", "SmallKey", "HashVaue");

2)、设置过期时间(单独设置)
  1. redisTemplate.boundValueOps("HashKey").expire(1,TimeUnit.MINUTES);
  2. redisTemplate.expire("HashKey",1,TimeUnit.MINUTES);

3)、添加一个Map集合
  1. HashMap<String, String> hashMap = new HashMap<>();
  2. redisTemplate.boundHashOps("HashKey").putAll(hashMap );

4)、设置过期时间(单独设置)
  1. redisTemplate.boundValueOps("HashKey").expire(1,TimeUnit.MINUTES);
  2. redisTemplate.expire("HashKey",1,TimeUnit.MINUTES);

5)、提取所有的key
  1. //1、通过redisTemplate获取值
  2. Set keys1 = redisTemplate.boundHashOps("HashKey").keys();
  3. //2、通过BoundValueOperations获取值
  4. BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
  5. Set keys2 = hashKey.keys();
  6. //3、通过ValueOperations获取值
  7. HashOperations hashOps = redisTemplate.opsForHash();
  8. Set keys3 = hashOps.keys("HashKey");

6)、提取所有的value值
  1. //1、通过redisTemplate获取值
  2. List values1 = redisTemplate.boundHashOps("HashKey").values();
  3. //2、通过BoundValueOperations获取值
  4. BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
  5. List values2 = hashKey.values();
  6. //3、通过ValueOperations获取值
  7. HashOperations hashOps = redisTemplate.opsForHash();
  8. List values3 = hashOps.values("HashKey");

7)、根据key提取value值
  1. //1、通过redisTemplate获取
  2. String value1 = (String) redisTemplate.boundHashOps("HashKey").get("SmallKey");
  3. //2、通过BoundValueOperations获取值
  4. BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
  5. String value2 = (String) hashKey.get("SmallKey");
  6. //3、通过ValueOperations获取值
  7. HashOperations hashOps = redisTemplate.opsForHash();
  8. String value3 = (String) hashOps.get("HashKey", "SmallKey");

8)、获取所有的键值对集合
  1. //1、通过redisTemplate获取
  2. Map entries = redisTemplate.boundHashOps("HashKey").entries();
  3. //2、通过BoundValueOperations获取值
  4. BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
  5. Map entries1 = hashKey.entries();
  6. //3、通过ValueOperations获取值
  7. HashOperations hashOps = redisTemplate.opsForHash();
  8. Map entries2 = hashOps.entries("HashKey");

9)、删除
  1. //1、通过redisTemplate获取
  2. Map entries = redisTemplate.boundHashOps("HashKey").entries();
  3. //2、通过BoundValueOperations获取值
  4. BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
  5. Map entries1 = hashKey.entries();
  6. //3、通过ValueOperations获取值
  7. HashOperations hashOps = redisTemplate.opsForHash();
  8. Map entries2 = hashOps.entries("HashKey");

10)、判断Hash中是否含有该值
  1. Boolean isEmpty = redisTemplate.boundHashOps("HashKey").hasKey("SmallKey");

6、Set类型相关操作

1)、添加Set缓存(值可以是一个,也可是多个)(2/3是1的递进值)
  1. //1、通过redisTemplate设置值
  2. redisTemplate.boundSetOps("setKey").add("setValue1", "setValue2", "setValue3");
  3. //2、通过BoundValueOperations设置值
  4. BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
  5. setKey.add("setValue1", "setValue2", "setValue3");
  6. //3、通过ValueOperations设置值
  7. SetOperations setOps = redisTemplate.opsForSet();
  8. setOps.add("setKey", "SetValue1", "setValue2", "setValue3");

2)、设置过期时间(单独设置)
  1. redisTemplate.boundValueOps("setKey").expire(1,TimeUnit.MINUTES);
  2. redisTemplate.expire("setKey",1,TimeUnit.MINUTES);

3)、根据key获取Set中的所有值
  1. //1、通过redisTemplate获取值
  2. Set set1 = redisTemplate.boundSetOps("setKey").members();
  3. //2、通过BoundValueOperations获取值
  4. BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
  5. Set set2 = setKey.members();
  6. //3、通过ValueOperations获取值
  7. SetOperations setOps = redisTemplate.opsForSet();
  8. Set set3 = setOps.members("setKey");

4)、根据value从一个set中查询,是否存在
  1. Boolean isEmpty = redisTemplate.boundSetOps("setKey").isMember("setValue2");

5)、获取Set缓存的长度
  1. Long size = redisTemplate.boundSetOps("setKey").size();

6)、移除指定的元素
  1. Long result1 = redisTemplate.boundSetOps("setKey").remove("setValue1");

7)、移除指定的key
  1. Boolean result2 = redisTemplate.delete("setKey");

7、 LIST类型相关操作

1)、添加缓存(2/3是1的递进值)
  1. //1、通过redisTemplate设置值
  2. redisTemplate.boundListOps("listKey").leftPush("listLeftValue1");
  3. redisTemplate.boundListOps("listKey").rightPush("listRightValue2");
  4. //2、通过BoundValueOperations设置值
  5. BoundListOperations listKey = redisTemplate.boundListOps("listKey");
  6. listKey.leftPush("listLeftValue3");
  7. listKey.rightPush("listRightValue4");
  8. //3、通过ValueOperations设置值
  9. ListOperations opsList = redisTemplate.opsForList();
  10. opsList.leftPush("listKey", "listLeftValue5");
  11. opsList.rightPush("listKey", "listRightValue6");

2)、将List放入缓存
  1. ArrayList<String> list = new ArrayList<>();
  2. redisTemplate.boundListOps("listKey").rightPushAll(list);
  3. redisTemplate.boundListOps("listKey").leftPushAll(list);

3)、设置过期时间(单独设置)
  1. redisTemplate.boundValueOps("listKey").expire(1,TimeUnit.MINUTES);
  2. redisTemplate.expire("listKey",1,TimeUnit.MINUTES);

4)、获取List缓存全部内容(起始索引,结束索引)
  1. List listKey1 = redisTemplate.boundListOps("listKey").range(0, 10);

5)、从左或从右弹出一个元素
  1. String listKey2 = (String) redisTemplate.boundListOps("listKey").leftPop(); //从左侧弹出一个元素
  2. String listKey3 = (String) redisTemplate.boundListOps("listKey").rightPop(); //从右侧弹出一个元素

6)、根据索引查询元素
  1. String listKey4 = (String) redisTemplate.boundListOps("listKey").index(1);

7)、获取List缓存的长度
  1. Long size = redisTemplate.boundListOps("listKey").size();

8)、根据索引修改List中的某条数据(key,索引,值)
  1. redisTemplate.boundListOps("listKey").set(3L,"listLeftValue3");

9)、移除N个值为value(key,移除个数,值)
  1. redisTemplate.boundListOps("listKey").remove(3L,"value");

8、Zset类型的相关操作

1)、向集合中插入元素,并设置分数
  1. //1、通过redisTemplate设置值
  2. redisTemplate.boundZSetOps("zSetKey").add("zSetVaule", 100D);
  3. //2、通过BoundValueOperations设置值
  4. BoundZSetOperations zSetKey = redisTemplate.boundZSetOps("zSetKey");
  5. zSetKey.add("zSetVaule", 100D);
  6. //3、通过ValueOperations设置值
  7. ZSetOperations zSetOps = redisTemplate.opsForZSet();
  8. zSetOps.add("zSetKey", "zSetVaule", 100D);

2)、向集合中插入多个元素,并设置分数
  1. DefaultTypedTuple<String> p1 = new DefaultTypedTuple<>("zSetVaule1", 2.1D);
  2. DefaultTypedTuple<String> p2 = new DefaultTypedTuple<>("zSetVaule2", 3.3D);
  3. redisTemplate.boundZSetOps("zSetKey").add(new HashSet<>(Arrays.asList(p1,p2)));

3)、按照排名先后(从小到大)打印指定区间内的元素, -1为打印全部
  1. Set<String> range = redisTemplate.boundZSetOps("zSetKey").range(key, 0, -1);

4)、获得指定元素的分数
  1. Double score = redisTemplate.boundZSetOps("zSetKey").score("zSetVaule");

5)、返回集合内的成员个数
  1. Long size = redisTemplate.boundZSetOps("zSetKey").size();

6)、返回集合内指定分数范围的成员个数(Double类型)
  1. Long COUNT = redisTemplate.boundZSetOps("zSetKey").count(0D, 2.2D);

7)、返回集合内元素在指定分数范围内的排名(从小到大)
  1. Set byScore = redisTemplate.boundZSetOps("zSetKey").rangeByScore(0D, 2.2D);

8)、带偏移量和个数,(key,起始分数,最大分数,偏移量,个数)