栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot集成redis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot集成redis

说明:在SpringBoot2.x之后,原来使用的jedis被替换成了lettuce
jedis:采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用jedis pool连接池!更像BIO模式
lettuce:采用netty,实例可以在多个线程中进行共享,不存在线程不安全的情况!可以减少线程数据了,更像NIO模式

源码分析

	@Bean
	@ConditionalOnMissingBean(name = "redisTemplate") //我们可以自己定义一个redisTemplate来替换这个默认的!
	@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
	public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
		//默认的redisTemplate没有过多的设置,redis对象都是需要序列化!
		//两个泛型都是Object,Object的类型,我们后面使用需要强制转换
		RedisTemplate template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

	@Bean
	@ConditionalOnMissingBean //由于string是redis中最常用的类型,所以说单独提出来了一个bean
	@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
		return new StringRedisTemplate(redisConnectionFactory);
	}
第一步:创建项目,添加依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.example
    redis-02-springboot
    0.0.1-SNAPSHOT
    redis-02-springboot
    redis-02-springboot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    



第二步:application.yml
#SpringBoot 所有的配置类,都有一个自动配置类 RedisAutoConfiguration
#自动配置类都会绑定一个properties配置文件 RedisProperties

#配置redis
spring.redis.host=192.168.222.200
spring.redis.port=6379
第三步:测试代码
package com.example;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
class Redis02SpringbootApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void contextLoads() {
        //redisTemplate 操作不同的数据类型,api和redis指令是一样的
        //opsForValue 操作字符串
        //opsForList 操作list
        //......


        //除了基本的操作,我们常用的方法都可以直接通过redisTemplate操作,比如事务和基本的curd

        //获取redis的连接对象
        //RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
        //connection.flushAll();
        //connection.flushDb();

        redisTemplate.opsForValue().set("mykey","张三");
        System.out.println(redisTemplate.opsForValue().get("mykey"));
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/754381.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号