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 
相关推荐
互联网搬砖老肖35 分钟前
运维打铁:Centos 7使用yum安装 Redis 5
运维·redis·centos
南玖yy1 小时前
C++ 的未来战场:从技术深耕到职业破局
c语言·开发语言·c++·后端·c++未来
科技万象1 小时前
SpringBoot终极形态:AI生成带OAuth2鉴权的微服务模块(节省20人日)
人工智能·spring boot·微服务
Alsn861 小时前
9.idea中创建springboot项目_jdk1.8
java·spring boot·intellij-idea
计算机毕设定制辅导-无忧学长2 小时前
Spring Boot 集成 ActiveMQ 实现异步消息通信(二)
spring boot·activemq·java-activemq
weixin_456588152 小时前
【Spring Boot 注解】@ConfigurationProperties
spring boot·后端
豆沙沙包?2 小时前
2025年-redis(p1-p10)
数据库·redis·缓存
小安同学iter3 小时前
苍穹外卖心得体会
java·开发语言·spring boot·servlet·intellij-idea·mybatis
chunfeng—3 小时前
纯C协程框架NtyCo
linux·c++·后端·协程·ntyco
biubiubiu07063 小时前
ElaticSearch
spring boot·后端·jenkins