参考:
- https://blog.csdn.net/u012702547/article/details/103210490
- redis详细教程参考-松哥:https://mp.weixin.qq.com/s/0Bukv4bFGMWosG8SWWoDDw
- 5种数据类型的操作API:https://blog.csdn.net/ooyhao/article/details/83628336
- refer:强烈推荐:https://blog.csdn.net/yangshangwei/article/details/113827617
前言
何为SpringDataRedis?
使用 Java 操作 Redis 的方案很多,可以直接使用原生的操作方案:Jedis、Lettuce
。也可以使用 Spring Data Redis
。
SpringDataRedis 对Redis的操作过程进行了封装、简化了Redis的使用,是对原生操作方案(Jedis和Lettuce)的一种简化封装。
在 Spring Boot 中,默认集成的 Redis操作方案 就是 Spring Data Redis
,
spring boot在1.x.x的版本时默认使用的是 jedis来操作Redis,现在是2.x.x版本默认使用的 Lettuce来操作Redis。开发者也可以将操作方案由Lettuce改为 Jedis
。(这样需要额外的pom配置和修改application.yml)。
SpringDataRedis 针对 Redis 提供了非常方便的操作模板 RedisTemplate
来操作Redis。
一、SpringBoot整合RedisTemplate来操作Redis
当然也可以在SSM项目中使用Spring-Data-Redis。
1 创建工程——引入 Spring-Data-Redis依赖:
这里主要引入spring-data-redis
依赖,还需要手动引入 commos-pool2
的依赖,因此最终完整的 pom.xml
依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2 配置 Redis 信息
接下来配置 Redis 的信息,信息包含两方面:一方面是 Redis 的基本信息,另一方面则是连接池信息:
# redis基本信息
spring.redis.database=0
spring.redis.password=123
spring.redis.port=6379
spring.redis.host=192.168.66.128
## redis的客户端连接池配置
spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=1ms
spring.redis.lettuce.shutdown-timeout=100ms
自动配置
当开发者在项目中引入了 Spring-Data-Redis依赖 ,并在配置文件中 配置了 Redis 的相关配置后,此时,自动化配置就会生效。
3 使用StringDataRedis来操作Redis:
接下来,可以直接在 Service ·
中注入·StringRedisTemplate 或者 RedisTemplate·
来使用:
@Service
public class HelloService {
@Autowired
RedisTemplate redisTemplate;
public void hello() {
ValueOperations ops = redisTemplate.opsForValue();
ops.set("k1", "v1");
Object k1 = ops.get("k1");
System.out.println(k1);
}
}
调用上面方法,就会把数据存储到Redis中了。
SpringDataRedis
中对数据的操作,大致分为2种:
1、 对key的操作:相关方法都在 RedisTemplate
类中。
2、 对不同类型的value的操作:需要先使用 redisTemplate.opsForXxx()
获取对应的value类型,然后再进行操作。
二、【辨析】StringRedisTemplate与RedisTemplate 区别
RedisTemplate
默认的序列化方案存储的数据是下面这样的::
使用RedisTemplate
来序列化数据,若数据是String类型的,则数据会有额外的前缀字符串:这是由于RedisTemplate
它对key和value默认的序列化方案是jdk序列化方案。而StringRedisTemplate
它对key和value默认的序列化方案是String序列化法。因此使用StringRedisTemplate
操作redis的话,在redis中的展示的数据就不会有额外的前缀字符串。
RedisTemplate
和 StringRedisTemplate
的关系是什么?
StringRedisTemplate
继承RedisTemplate
。- 两者的数据是不共通的;也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。
- 他们默认采用的序列化策略不同,一种是String的序列化策略,一种是JDK的序列化策略。
StringRedisTemplate
默认采用的是String的序列化策略来序列化key和value的。而RedisTemplate
默认采用的是JDK的序列化策略来序列化key和value的。
三、【原理】SpringDataRedis自动配置过程解析:
我们从 Spring Boot 中 Redis 的自动化配置类中就可以看出:
自动配置类,给我们提供了2个Redis操作的模板对象。都可以用来操作Redis,但是有一些区别。
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
这个自动化配置类很好理解:
- 首先标记这个是一个配置类,同时该配置在项目中引入了 SpringDataRedis依赖时才会生效
- 然后导入在 application.properties 中配置的属性
- 然后再导入连接池信息(如果存在的话)
- 最后,提供了两个
Bean
,RedisTemplate
和StringRedisTemplate
,其中 StringRedisTemplate 是 RedisTemplate 的子类,两个的方法基本一致,不同之处主要体现在操作的数据类型不同
,RedisTemplate 中的两个泛型都是 Object ,意味者存储的 key 和 value 都可以是一个对象,而 StringRedisTemplate 的 两个泛型都是 String ,意味者 StringRedisTemplate 的 key 和 value 都只能是字符串。如果开发者没有提供相关的 Bean ,这两个配置就会生效,否则不会生效。
四、RedisTemplate中定义了对5种value的操作:
先获取对应的value的数据结构才能进行操作。
redisTemplate.opsForValue();
//value是一个对象类型Object类型
redisTemplate.opsForHash();
//操作hash
redisTemplate.opsForList();
//操作list
redisTemplate.opsForSet();
//操作set
redisTemplate.opsForZSet();
//操作有序set
value为Object类型
推荐使用RedisTemplate,且value的序列化方式为jackson方式。
1、String数据结构(推荐使用StringRedisTemplate类型的Bean)
- void set(K key, V value);
使用:redisTemplate.opsForValue().set("name","tom");
结果:redisTemplate.opsForValue().get("name") 输出结果为tom
使用:redisTemplate.opsForValue().set("name","tom",10, TimeUnit.SECONDS);
结果:redisTemplate.opsForValue().get("name")由于设置的是10秒失效,十秒之内查询有结果,十秒之后返回为null
2、Redis的List数据结构
- Long leftPush(K key, V value);
将所有指定的值插入存储在键的列表的头部。如果键不存在,则在执行推送操作之前将其创建为空列表。(从左边插入)
使用:template.opsForList().leftPush("list","java");
template.opsForList().leftPush("list","python");
template.opsForList().leftPush("list","c++");
结果:返回的结果为推送操作后的列表的长度
1
2
3
- Long rightPush(K key, V value);
将所有指定的值插入存储在键的列表的头部。如果键不存在,则在执行推送操作之前将其创建为空列表。(从右边插入)
使用:template.opsForList().rightPush("listRight","java");
template.opsForList().rightPush("listRight","python");
template.opsForList().rightPush("listRight","c++");
结果:
1
2
3
3、Redis的Hash数据机构
Redis的散列可以让用户将多个键值对存储到一个Redis键里面。
public interface HashOperations
HashOperations提供一系列方法操作hash:
- void put(H key, HK hashKey, HV value);
设置散列hashKey的值
使用:template.opsForHash().put("redisHash","name","tom");
template.opsForHash().put("redisHash","age",26);
template.opsForHash().put("redisHash","class","6");
System.out.println(template.opsForHash().entries("redisHash"));
结果:{age=26, class="6", name=tom}
template.opsForHash().put("redisHash","name","tom");
template.opsForHash().put("redisHash","age",26);
template.opsForHash().put("redisHash","class","6");
Map<String,Object> testMap = new HashMap();
testMap.put("name","jack");
testMap.put("age",27);
testMap.put("class","1");
template.opsForHash().putAll("redisHash1",testMap);
- HV get(H key, Object hashKey);
从键中的哈希获取给定hashKey的值
使用:System.out.println(template.opsForHash().get("redisHash","age"));
结果:26
4、Redis的Set数据结构
Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。
Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。
public interface SetOperations
SetOperations提供了对无序集合的一系列操作:
- Long add(K key, V… values);
无序集合中添加元素,返回添加个数
也可以直接在add里面添加多个值 如:template.opsForSet().add(“setTest”,“aaa”,“bbb”)
使用:String[] strarrays = new String[]{"strarr1","sgtarr2"};
System.out.println(template.opsForSet().add("setTest", strarrays));
结果:2
Redis 中的数据操作,大体上来说,可以分为两种:
- 1、针对
· key
的操作,相关的方法就在RedisTemplate
中 - 2、针对
具体数据类型(value)
的操作,相关的方法需要首先获取对应的数据类型,获取相应数据类型的操作方法是opsForXXX
调用该方法就可以将数据存储到 Redis 中去了.
另外需要注意 ,Spring Boot 的自动化配置,只能配置单机的 Redis ,如果是 Redis 集群,则所有的东西都需要自己手动配置。
后记:方案二、Spring Cache方式操作SpringDataRedis
通过 Spring Cache 的形式来操作 Redis,Spring Cache 统一了缓存江湖的门面,这种方案,可以移步这里:Spring Boot中,Redis缓存还能这么用!。https://mp.weixin.qq.com/s/UpTewC66iJyzq0osm_0cfw