电商网站有哪些平台,最好的wordpress教程,安卓开发前景如何,58同城宿迁二手房文章目录 一、SpringBoot整合二、RedisAutoConfiguration自动配置类1、整合测试一下 三、自定义RedisTemplete1、在测试test中使用自定义的RedisTemplete2、自定义RedisTemplete后测试 四、在企业开发中#xff0c;大部分情况下都不会使用原生方式编写redis1、编写RedisUtils代… 文章目录 一、SpringBoot整合二、RedisAutoConfiguration自动配置类1、整合测试一下 三、自定义RedisTemplete1、在测试test中使用自定义的RedisTemplete2、自定义RedisTemplete后测试 四、在企业开发中大部分情况下都不会使用原生方式编写redis1、编写RedisUtils代替原生操作RedisTemplate的方式2、测试使用redisUtils操作redis 一、SpringBoot整合
SpringBoot 操作数据spring-data jpa jdbc mongodb redis SpringData 也是和 SpringBoot 齐名的项目
说明 在 SpringBoot2.x 之后原来使用的jedis 被替换为了 lettuce? jedis : 采用直连多个线程操作的话是不安全的如果想要避免不安全的使用 jedis pool 连接 池 更像 BIO 模式 lettuce : 采用netty实例可以在多个线程中进行共享不存在线程不安全的情况可以减少线程数据 了更像 NIO 模式
二、RedisAutoConfiguration自动配置类
自动配置类springboot已经帮我们写好来看看源码 源码分析下面是springboot中 RedisAutoConfiguration自动配置类 的源码中的方法
Bean
ConditionalOnMissingBean(name redisTemplate) // 我们可以自己定义一个
redisTemplate来替换这个默认的
public RedisTemplateObject, ObjectredisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException {// 默认的 RedisTemplate 没有过多的设置redis 对象都是需要序列化// 两个泛型都是 Object, Object 的类型我们后使用需要强制转换 String, ObjectRedisTemplateObject, 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、整合测试一下
导入springboot-redis的 依赖
!-- 操作redis --
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId
/dependency配置连接在application.properties中
# 配置redis
spring.redis.host127.0.0.1
spring.redis.port6379测试在测试类中自动装配RedisTemplate因为springboot已经通过RedisAutoConfiguration配置类帮我们注入了RedisTemplate然后通过RedisTemplate 去操作 Redis
SpringBootTest
class Redis02SpringbootApplicationTests {Autowiredprivate RedisTemplate redisTemplate;Testvoid contextLoads() {// redisTemplate 操作不同的数据类型api和我们的指令是一样的// 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(mykey,关注狂神说公众号);System.out.println(redisTemplate.opsForValue().get(mykey));}
}关于对象的保存 对象没有序列化传输保存会报错 序列化对象才能在redis的value中正常保存并获取对象 public class User implements Serializable {private String name;private int age;
}三、自定义RedisTemplete
默认序列化是jdk的序列化方式自定义RedisTemplete配置config可以自定义序列化方式 可以自己配置具体的序列化方式代码如下所示我们来编写一个自己的 RedisTemplete下面是一个固定的模板可以直接用
自己写一个配置类注入自己定义的RedisTemplate
package com.kuang.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
Configuration
public class RedisConfig {// 这是我给大家写好的一个固定模板大家在企业中拿去就可以直接使用// 自己定义了一个 RedisTemplateBeanSuppressWarnings(all)public RedisTemplateString, Object redisTemplate(RedisConnectionFactory factory) {// 我们为了自己开发方便一般直接使用 String,ObjectRedisTemplateString, Object template new RedisTemplateString,Object();template.setConnectionFactory(factory);//Json序列化配置,创建序列化方式的对象Jackson2JsonRedisSerializer jackson2JsonRedisSerializer newJackson2JsonRedisSerializer(Object.class);ObjectMapper om new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);// String 的序列化StringRedisSerializer stringRedisSerializer new StringRedisSerializer();// key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);// hash的key也采用String的序列化方式template.setHashKeySerializer(stringRedisSerializer);// value序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);// hash的value序列化方式采用jacksontemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}
}所有的redis操作其实对于java开发人员来说十分的简单更重要是要去理解redis的思想和每一种数据结构的用处和作用场景
1、在测试test中使用自定义的RedisTemplete 首先自动装配因为在配置类中注入过了 装配时要选择自定义的RedisTemplate而不是源码中的 2、自定义RedisTemplete后测试 //保存对象到redisTestpublic void test() throws JsonProcessingException {User user new User(hhb, 20);//String jsonUser new ObjectMapper().writeValueAsString(user); //转换为json字符串redisTemplate.opsForValue().set(user,user);System.out.println(redisTemplate.opsForValue().get(user));}结果 因为key 已经采用 String序列化方式 自定义RedisTemplate并为其设置序列化方式之前key是乱码 四、在企业开发中大部分情况下都不会使用原生方式编写redis
不会像下面这样
1、编写RedisUtils代替原生操作RedisTemplate的方式
在真实开发中或者公司一般都会看到一个封装的 RedisUtil工具类文件在文章标题处给出RedisUtil工具类附件因为文件代码太长所以提供文件附件百度csdn中应该也有很多容易搜到
示例部分代码如下都是封装了一些redis的操作把redis的操作封装为了RedisUtils的方法。
Component
SuppressWarnings({unchecked, all})
public class RedisUtils {private static final Logger log LoggerFactory.getLogger(RedisUtils.class);private RedisTemplateObject, Object redisTemplate;// Value(${jwt.online-key})// private String onlineKey;public RedisUtils(RedisTemplateObject, Object redisTemplate) {this.redisTemplate redisTemplate;this.redisTemplate.setHashKeySerializer(new StringRedisSerializer());this.redisTemplate.setKeySerializer(new StringRedisSerializer());this.redisTemplate.setStringSerializer(new StringRedisSerializer());}/*** 指定缓存失效时间** param key 键* param time 时间(秒)*/public boolean expire(String key, long time) {try {if (time 0) {redisTemplate.expire(key, time, TimeUnit.SECONDS);}} catch (Exception e) {log.error(e.getMessage(), e);return false;}return true;}/*** 指定缓存失效时间** param key 键* param time 时间(秒)* param timeUnit 单位*/public boolean expire(String key, long time, TimeUnit timeUnit) {try {if (time 0) {redisTemplate.expire(key, time, timeUnit);}} catch (Exception e) {log.error(e.getMessage(), e);return false;}return true;}/*** 根据 key 获取过期时间** param key 键 不能为null* return 时间(秒) 返回0代表为永久有效*/public long getExpire(Object key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}
}2、测试使用redisUtils操作redis
因为RedisUtils被注册成了组件所以可以Autowired自动装配 输出结果如下 文章转载自: http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.ywpwg.cn.gov.cn.ywpwg.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.spsqr.cn.gov.cn.spsqr.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.smmby.cn.gov.cn.smmby.cn http://www.morning.fgrcd.cn.gov.cn.fgrcd.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.fkwgk.cn.gov.cn.fkwgk.cn http://www.morning.srnhk.cn.gov.cn.srnhk.cn http://www.morning.lbcfj.cn.gov.cn.lbcfj.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.ggtkk.cn.gov.cn.ggtkk.cn http://www.morning.ghfrb.cn.gov.cn.ghfrb.cn http://www.morning.rytps.cn.gov.cn.rytps.cn http://www.morning.qshxh.cn.gov.cn.qshxh.cn http://www.morning.smj78.cn.gov.cn.smj78.cn http://www.morning.txlnd.cn.gov.cn.txlnd.cn http://www.morning.gwsfq.cn.gov.cn.gwsfq.cn http://www.morning.wjpsn.cn.gov.cn.wjpsn.cn http://www.morning.rfldz.cn.gov.cn.rfldz.cn http://www.morning.rxtxf.cn.gov.cn.rxtxf.cn http://www.morning.rfdqr.cn.gov.cn.rfdqr.cn http://www.morning.wjfzp.cn.gov.cn.wjfzp.cn http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.tntqr.cn.gov.cn.tntqr.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.rzmzm.cn.gov.cn.rzmzm.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.dpdr.cn.gov.cn.dpdr.cn http://www.morning.c7491.cn.gov.cn.c7491.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.mfsxd.cn.gov.cn.mfsxd.cn http://www.morning.gthwr.cn.gov.cn.gthwr.cn http://www.morning.yrgb.cn.gov.cn.yrgb.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.crkmm.cn.gov.cn.crkmm.cn http://www.morning.mlyq.cn.gov.cn.mlyq.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.kzdgz.cn.gov.cn.kzdgz.cn http://www.morning.qwfl.cn.gov.cn.qwfl.cn http://www.morning.alwpc.cn.gov.cn.alwpc.cn http://www.morning.jwxmn.cn.gov.cn.jwxmn.cn http://www.morning.rbkl.cn.gov.cn.rbkl.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.mzpd.cn.gov.cn.mzpd.cn http://www.morning.ttnfc.cn.gov.cn.ttnfc.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.gjtdp.cn.gov.cn.gjtdp.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.wrtw.cn.gov.cn.wrtw.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.ffwrq.cn.gov.cn.ffwrq.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.gbrdx.cn.gov.cn.gbrdx.cn http://www.morning.bylzr.cn.gov.cn.bylzr.cn http://www.morning.ltzkk.cn.gov.cn.ltzkk.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.mmhaoma.com.gov.cn.mmhaoma.com http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.zrgsg.cn.gov.cn.zrgsg.cn http://www.morning.zrnph.cn.gov.cn.zrnph.cn http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com http://www.morning.rwmft.cn.gov.cn.rwmft.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn