Springboot_Redis

Springboot默认使用lettuce操作redis,底层是netty

jdeis并发差些

Redis的Template

分为两种, 一种是StringRedisTemplate ,另一种是RedisTemplate

根据不同的数据类型,大致的操作也分为这5种,以StringRedisTemplate为例

复制代码
stringRedisTemplate.opsForValue()  --String
stringRedisTemplate.opsForList()  --List
stringRedisTemplate.opsForSet()  --Set
stringRedisTemplate.opsForHash()  --Hash
stringRedisTemplate.opsForZset()  -Zset

1、导入依赖

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、修改配置文件

bash 复制代码
spring.redis.host= ip地址
spring.redis.port=6379
# Redis服务器连接密码(默认为空)  
spring.redis.password= 

新版用Lettuce

3、添加测试类

java 复制代码
    @Autowired
    StringRedisTemplate stringRedisTemplate;//操作字符串【常用】

    @Autowired
    RedisTemplate redisTemplate;//操作k-v都是对象    
	@Test
    public void test01(){
//        stringRedisTemplate.opsForValue().append("msg", "hello");
        String msg = stringRedisTemplate.opsForValue().get("msg");
        System.out.println(msg);
    }

3、测试保存对象

对象需要序列化

1、实现序列化类

java 复制代码
public class Employee implements Serializable {

2、将对象存储到Redis

java 复制代码
@Test
public  void test02(){
    Employee emp = employeeMapper.getEmpById(2);
    redisTemplate.opsForValue().set("emp-01", emp);
}

4、以json方式传输对象

1、新建一个Redis的配置类MyRedisConfig,

java 复制代码
@Configuration
public class MyRedisConfig {
    @Bean
    public RedisTemplate<Object, Employee> empRedisTemplate(
            RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<Object, Employee> template = new RedisTemplate<Object, Employee>();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Employee> jsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
        template.setDefaultSerializer(jsonRedisSerializer);
        return template;
    }

2、编写测试类

java 复制代码
 @Autowired
 RedisTemplate<Object,Employee> empRedisTemplate;
@Test
public  void test02(){
    Employee emp = employeeMapper.getEmpById(2);
    empRedisTemplate.opsForValue().set("emp-01", emp);

}

Redis集成springboot

源码分析

java 复制代码
@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
    public RedisAutoConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean(
        name = {"redisTemplate"}
    )//我们可以自定义template
    @ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        //两个泛型都是Object,后续我们使用需要强转<String ,Object>
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    @ConditionalOnMissingBean//String是最常使用的类型,所以单独弄了个bean
    @ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

自定义template

要记得把对象序列化,不然会报错。

默认用的是jdk序列化

java 复制代码
@Configuration
public class RedisConfig {

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
       template.setValueSerializer(jackson2JsonRedisSerializer);

        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

工具类

https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html

https://www.cnblogs.com/zhzhlong/p/11434284.html

序列化

默认RedisTemplate都是jdk序列化

  • 占用高,前面带个类型
  • 不可阅读

字符串序列化方式:StringRedisTemplate

json序列化方式:用RedisTemplate,自定义序列化方式

附录

其他配置

bash 复制代码
# Redis数据库索引(默认为0)  
spring.redis.database=0  
# 连接池最大连接数(使用负值表示没有限制)  
spring.redis.pool.max-active=200  
# 连接池最大阻塞等待时间(使用负值表示没有限制)  
spring.redis.pool.max-wait=-1  
# 连接池中的最大空闲连接  
spring.redis.pool.max-idle=10 
# 连接池中的最小空闲连接  
spring.redis.pool.min-idle=0  
# 连接超时时间(毫秒)  
spring.redis.timeout=1000 
相关推荐
你的人类朋友几秒前
【Node】认识multer库
前端·javascript·后端
lang201509281 小时前
Spring Boot 官方文档精解:构建与依赖管理
java·spring boot·后端
007php0072 小时前
百度面试题解析:微服务架构、Dubbo、Redis及其一致性问题(一)
redis·百度·docker·微服务·容器·职场和发展·架构
why技术2 小时前
从18w到1600w播放量,我的一点思考。
java·前端·后端
间彧2 小时前
Redis Cluster vs Sentinel模式区别
后端
间彧3 小时前
🛡️ 构建高可用缓存架构:Redis集群与Caffeine多级缓存实战
后端
间彧3 小时前
构建本地缓存(如Caffeine)+ 分布式缓存(如Redis集群)的二级缓存架构
后端
长安城没有风3 小时前
从入门到精通【Redis】Redis 典型应⽤ --- 分布式锁
数据库·redis·分布式
程序猿DD4 小时前
Java 25 中的 6 个新特性解读
java·后端
稻草猫.4 小时前
文件 IO
java·笔记·后端·java-ee·idea