32springboot——缓存之整合redis

Arbitrary233      2022-05-07     493

关键词:

springboot缓存默认使用ConcurrentMapCacheManager 将数据保存在下面的Map中

1、docker中开启Redis

 2、添加Redis相关依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

我还多加了下面这个依赖才不会报错,具体原因尚未了解

<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
</dependency>

此时redis就引入再容器中

可以查看自动配置的类(RedisAutoConfiguration.class)分别注入了操作对象的类和操作字符串的类
public class RedisAutoConfiguration {
    public 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;
    }
}

4、在配置文件中引入redis服务所在的ip地址:

 5、测试

测试:
此时两个操作redis的类都在容器中,在测试的时候直接进行注入使用
    @Autowired
    StringRedisTemplate stringRedisTemplate;    //操作k-v都是字符串的
    @Autowired
    RedisTemplate redisTemplate;        //操作k-v都是对象的

    @Test
    public void testRedis(){
        //给redis中保存数据
        stringRedisTemplate.opsForValue().append("name","lisi");
        //读数据
        String name = stringRedisTemplate.opsForValue().get("name");
        System.out.println(name);
        //放列表
        stringRedisTemplate.opsForList().leftPush("myList","1");
        stringRedisTemplate.opsForList().leftPush("myList","2");
        stringRedisTemplate.opsForList().leftPush("myList","3");
    }

在Redis Desk Manager中可以看到存入成功

 使用redisTemplate测试添加对象:

  插入的对象实现类需要实现序列化

public class Employee  implements Serializable {
//测试保存对象
@Test
public  void test2(){
    //保存的是emp的对象
    Employee emp = employeeMapper.getEmpById(1);
    //保存的是employee的对象
    //默认如果使用保存对象,使用jdk序列化机制,序列化后的数据保存在redis中
    redisTemplate.opsForValue().set("emp01",emp);
}

插入后的数据如下:

 因为这个redisTemplate默认使用的是jdk的序列化机制

 想要保存为json序列化格式则可以自定义一个redisTemplate进行设置json的序列化机制

  创建一个MyRedisConfig类进行Redis自定义的相关配置

@Configuration
public class MyRedisConfig {
    //专门以json格式序列化Employee
    @Bean
    public RedisTemplate<Object, Employee> empRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Employee> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Employee> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
        template.setDefaultSerializer(jackson2JsonRedisSerializer);
        return template;
    }
}

下面的代码是RedisAutoConfiguration源码中注入默认RedisTemplate的代码

    @Bean
    @ConditionalOnMissingBean(
        name = {"redisTemplate"}
    )
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

进行对比可以发现我们只是增加了json格式的序列化机制(红色部分代码)

还有其他的序列化机制(如下图),需要的时候进行配置即可

 此时在测试代码中再使用我们自己注入的empRedisTemplate

@Autowired              //注入
RedisTemplate<Object, Employee> empRedisTemplate;
//测试保存对象
@Test
public  void test2(){
    //保存的是emp的对象
    Employee emp = employeeMapper.getEmpById(1);
    //保存的是employee的对象
    //默认如果使用保存对象,使用jdk序列化机制,序列化后的数据保存在redis中
    empRedisTemplate.opsForValue().set("emp01",emp);
}

可以看到插入的数据就是json格式了

 

springboot2.0实战(23)整合redis之集成缓存springdatacache

SpringBoot2.0实战(23)整合Redis之集成缓存SpringDataCache来源:https://www.toutiao.com/a6803168397090619908/?timestamp=1584450221&app=news_article_lite&group_id=6803168397090619908&req_id=20200317210341 查看详情

springboot整合redis入门之缓存数据(代码片段)

前言Redis是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。从2013年5月开始,Redis的开发由Pivotal赞助。... 查看详情

springboot(二十七)整合redis之分布式锁

在之前的一篇文章(《Java分布式锁,搞懂分布式锁实现看这篇文章就对了》),已经介绍过几种java分布式锁,今天来个Redis分布式锁的demo。redis现在已经成为系统缓存的必备组件,针对缓存读取更新操作,通常我们希望当缓存过期... 查看详情

重学springboot系列之redis与springcache缓存(代码片段)

重学SpringBoot系列之redis缓存使用docker安装redis准备工作获取redis镜像创建容器创建持久化存储目录获取redis的默认配置文件模版使用镜像创建一个容器查看活跃的容器访问redis容器服务开启防火墙端口,提供外部访问redis数据结... 查看详情

springboot整合redis缓存

...非常流行的NoSQL数据库(Redis)来实现我们的缓存需求。SpringBoot整合Redis是非常方便快捷的,我用的是Mybatis,这里就不说Springboot整合Mybatis了网上有很多,同样非常简单。下面进入正题: 查看详情

springboot整合redis实现缓存

...纲一、缓存的应用场景二、更新缓存的策略三、运行 springboot-mybatis-redis 工程案例四、springboot-mybatis-redis 工程代码配置详解 运行环境:MacOS10.12.xJDK8+Redis3.2.8SpringBoot1.5.1.RELEASE 一、缓存的应用场景什么是缓存?... 查看详情

精通springboot--整合redis实现缓存

今天我们来讲讲怎么在springboot中整合redis实现对数据库查询结果的缓存。首先第一步要做的就是在pom.xml文件添加spring-boot-starter-data-redis。要整合缓存,必不可少的就是我们要继承一个父类CachingConfigurerSupport。我们先看看这个类... 查看详情

springboot整合redis缓存

使用springBoot添加redis缓存需要在POM文件里引入<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency 查看详情

springboot之缓存

一、准备工作首先整合使用Spring整合MyBatis。可参阅:SpringBoot整合MyBatis SpringBoot整合MyBatis完后后,我们需要在pom.xml中添加缓存相关的依赖。<!--cache--><dependency><groupId>org.springframework.boot</groupId><ar 查看详情

springboot之整合redis

一、SpringBoot整合单机版Redis1、在pom.xml文件中加入redis的依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId><version>1.3.5 查看详情

springboot整合redis实现缓存

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>1.5.2.RELEASE</version></dependency& 查看详情

springboot整合shiro集成redis缓存

简介:由于考虑到项目后期分布式部署,所以缓存由ehcache改为redis,而redis既有单机版部署,也有分布式部署,所以二者需要兼容。1.maven依赖<dependency><groupId>org.crazycake</groupId><artifactId>shiro-redis</artifactId><... 查看详情

springboot(二十四)整合redis

...介绍了Redis的安装和Redis的常用操作。今天主要介绍介绍springboot整合Redis。v应用场景现 查看详情

shiro整合springboot缓存之ehcache实现(代码片段)

Shiro整合Springboot缓存之EhCache实现Cache作用引入shiro和ehcache的整合依赖开启缓存Cache作用1.Cache缓存:计算机内存中一段数据2.作用:用来减轻DB的访问压力,从而提高系统的查询效率3.流程:引入shiro和ehcache的整合依... 查看详情

缓存springboot整合redis01(代码片段)

eclipse创建springboot要注意,基本的包名一定要与java中的包名一致,这样才能找到springgboot主程序的入口;主程序是在com.lazy.cache;那么test的包也要是com.lazy.cachesrc/main/java com.lazy.cache CacheApplication.javasrc/test/java com.lazy.cahche UserTe 查看详情

springboot整合mybatis,redis,代码

一说明接着上篇讲述redis缓存配置的用法: 二正文首先要使用缓存就必须要开开启缓存,第二步是需要开redis-server下载redis包之后,点击图中两个都可以开启redis怎么看是否启动呢,见下图:  在application中加入连接地址,和相... 查看详情

springboot中使用redistemplate实现redis数据缓存

SpringBoot整合Redis数据库实现数据缓存的本质是整合Redis数据库,通过对需要“缓存”的数据存入Redis数据库中,下次使用时先从Redis中获取,Redis中没有再从数据库中获取,这样就实现了Redis做数据缓存。??按照惯例,下面一步一步... 查看详情

springboot整合springseesion实现redis缓存

参考技术A使用SpringBoot开发项目时我们经常需要存储Session,因为Session中会存一些用户信息或者登录信息。传统的web服务是将session存储在内存中的,一旦服务挂了,session也就消失了,这时候我们就需要将session存储起来,而Redis就... 查看详情