SpringBoot集成Redis的思路详解
2023-06-13 09:19:49 时间
Redis(Remote Dictionary Server ),即远程字典服务。
是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
Redis能该干什么?
内存存储、持久化,内存是断电即失的,所以需要持久化(RDB、AOF)高效率、用于高速缓冲发布订阅系统地图信息分析计时器、计数器(eg:浏览量)… …
特性
多样的数据类型
持久化
集群
事务
…
2、测试RedisSpringBoot操作数据,Spring-Data、 jbdc、redis… …
SpringData与SpringBoot齐名的项目!
说明:在SpringBoot2.x之后,原来使用的jedis被替换为lettuce
jedis:采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,需使用jedis pool连接池!像BIO模式
lettuce:采用netty,实例可以再多个线程中进行共享,不存在线程不安全的情况!可以减少线程数据,更像NIO模式
新建一个项目
注意:
查看底层
源码分析:
@Bean @ConditionalOnMissingBean( //如果未注入组件条件,我们自己可以定义一个redisTemplate来替换这个默认的 name = {"redisTemplate"} public RedisTemplate Object, Object redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { //默认的 RedisTemplate 没有过多的设置 redis 都是需要序列化的 ! //两个泛型都是 Object Object的类型,我们往后使用需要强制转换 String,String RedisTemplate Object, Object template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; @Bean @ConditionalOnMissingBean //由于String 是redis 中最常用的类型 所有说单独提出来一个bean! public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; }
1、导入依赖
2、配置连接
# SpringBoot 所有的配置类 都有一个自动配置类 RedisAutoConfiguration # 自动配置类都会绑定一个 properties 配置文件 RedisProperties #配置 redis spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis
3、测试!
package com.kk; 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 Redis01SpringbootApplicationTests { @Autowired private RedisTemplate redisTemplate; @Test void contextLoads() { redisTemplate opsForValue 操作字符串的 类似String opsForList 操作List 类似List opsForSet opsForHash opsForZSet opsForGeo opsForHyperLogLog 除了基本的操作 ,我们常用的方法都可以直接通过redisTemplate 比如事务和基本的CRUD
//获取redis的连接对象 // RedisConnection connection = redisTemplate.getConnectionFactory().getConnection(); // connection.flushDb(); // connection.flushAll(); redisTemplate.opsForValue().set("kk1","kk2"); System.out.println(redisTemplate.opsForValue().get("kk1")); }
首先先建一个实体类,测试
User类
package com.kk.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; import java.io.Serializable; @Component @Data @AllArgsConstructor @NoArgsConstructor //在企业中,我们所有的pojo都会序列化 public class User implements Serializable { private String name; private int age;
测试:
@Test public void test() throws JsonProcessingException { //真实的开发一般都使用json来传递对象 User user = new User("kk", 17); String jsonUser = new ObjectMapper().writeValueAsString(user);//这样就变成了一个json对象了 redisTemplate.opsForValue().set("user",jsonUser); System.out.println(redisTemplate.opsForValue().get("user"));
r = new ObjectMapper().writeValueAsString(user);//这样就变成了一个json对象了 redisTemplate.opsForValue().set(“user”,jsonUser); System.out.println(redisTemplate.opsForValue().get(“user”)); }
==注意:如果不在User类中实现序列化,它会报错==
到此这篇关于SpringBoot集成Redis的文章就介绍到这了,更多相关SpringBoot集成Redis内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
我想要获取技术服务或软件
服务范围:MySQL、ORACLE、SQLSERVER、MongoDB、PostgreSQL 、程序问题
服务方式:远程服务、电话支持、现场服务,沟通指定方式服务
技术标签:数据恢复、安装配置、数据迁移、集群容灾、异常处理、其它问题
本站部分文章参考或来源于网络,如有侵权请联系站长。
数据库远程运维 SpringBoot集成Redis的思路详解
相关文章
- SpringBoot集成Eurake、Gateway和Redis+Token验证拦截器实现
- 使用SpringBoot集成redis的方法
- 深入理解:如何设置Redis中的key的过期时间(设置redis的过期时间)
- SpringBoot集成Redis及使用总结详解编程语言
- 重塑Redis:清理缓存数据(清除redis缓存数据)
- SSM集成Redis实现高效数据共享(ssm整合redis)
- SSM框架与Redis结合,开发更快更高效(ssm redis集成)
- 实现快速数据迁移:Redis 拷贝数据技巧大揭秘(redis拷贝数据)
- 如何在Redis中实现持久化存储(怎么持久化redis)
- 微服务架构下Redis实现数据集成(微服务中接入redis)
- 群聊构建Redis 技术实现新高度(群聊技术架构redis)
- 积分清零Redis解决方案(积分清零和redis)
- SSM框架集成Redis实现快速缓存优化(ssm框架集成redis)
- 加强Redis并发开启系统新可能(加大redis并发)
- 基于Redis的令牌桶实现流量控制(基于redis的令牌桶)
- 灵活实用的 Redis 集群管理(redis集群描述)
- 基于Redis的布尔过滤器实现高效精准查询(redis集成布尔过滤器)
- 利用Redis让项目更容易上手(redis集成到项目中)
- 框架基于SSM框架实现Redis高效缓存管理(redis集成到ssm)
- Redis实现分布式锁管理(redis集成分布式锁)
- IDEA中集成兼容Redis缓存技术(redis集成idea)
- 些场景下使用分析Redis队列场景应用研究(redis队列在哪)
- 简单示范Redis配置文件指南(redis配置文件示例)
- 从零开始搭建Redis集群并集成JWT认证(redis集群jwt)
- 变得更安全Redis让外网连接安全性大大提升(redis让外网连接)
- Redis缩小存储空间,提升存储性能(redis规格是什么)
- Redis缓存简单而有趣的集成方式(redis缓存集成专栏)
- 利用Redis简化缓存文件的删除(redis缓存文件删除)