当前位置: 首页 > news >正文

wap网站开发教程百度搜索引擎seo

wap网站开发教程,百度搜索引擎seo,网站建设卖手机代码,无极领域付费网站相信在项目中,你一定是经常使用 Redis ,那么,你是怎么使用的呢?在使用时,有没有遇到同我一样,对象缓存序列化问题的呢?那么,你又是如何解决的呢? Redis 使用示例 添加依…

相信在项目中,你一定是经常使用 Redis ,那么,你是怎么使用的呢?在使用时,有没有遇到同我一样,对象缓存序列化问题的呢?那么,你又是如何解决的呢?

Redis 使用示例

添加依赖:

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

在应用启动如何添加启用缓存注解(@EnableCaching)。

假如我们有一个用户对象(UserVo):

@Data
public class UserVo implements Serializable {@Serialprivate static final long serialVersionUID = 2215423070276994378L;private Long id;private String name;private LocalDateTime createDateTime;}

这里,我们实现了 Serializable 接口。

在我们需要缓存的方法上,使用 @Cacheable 注解,就表示如果返回的对象不是 null 时,就会对其进行缓存,下次查询,首先会去缓存中查询,查到了,就直接返回,不会再去数据库查询,查不到,再去数据库查询。

@Service
@Slf4j
public class UserServiceImpl implements IUserService {@Override@Cacheable(value = "sample-redis",key = "'user-'+#id",unless = "#result == null")public UserVo getUserById(Long id) {log.info("userVo from db query");UserVo userVo = new UserVo();userVo.setId(1L);userVo.setName("Zhang San");userVo.setCreateDateTime(LocalDateTime.now());return userVo;}}

核心代码:

@Cacheable(value = "sample-redis",key = "'user-'+#id",unless = "#result == null"
)

模拟测试,再写一个测试接口:

@RestController
@RequestMapping("/sample")
@RequiredArgsConstructor
@Slf4j
public class SampleController {private final IUserService userService;@GetMapping("/user/{id}")public UserVo getUserById(@PathVariable Long id) {UserVo vo = userService.getUserById(id);log.info("vo: {}", JacksonUtils.json(vo));return vo;}}

我们再加上连接 redis 的配置:

spring:data:redis:host: localhostport: 6379

测试:

### getUserById
GET http://localhost:8080/sample/user/1

在这里插入图片描述

输出结果跟我们想的一样,第一次从数据库查,后面都从缓存直接返回。

总结一下:

  1. 添加 spring-boot-starter-data-redis 依赖。

  2. 使用启用缓存注解(@EnableCaching)。

  3. 需要缓存的对象实现 Serializable 接口。

  4. 使用 @Cacheable 注解缓存查询的结果。

遇到问题

在上面我们通过 spring boot 提供的 redis 实现了查询对象缓存这样一个功能,有下面几个问题:

  1. 缓存的对象,必须序列化,不然会报错。
  2. redis 存储的数据,看不懂,可以转成 json 格式吗?
  3. 使用 Jackson 时,遇到特殊类型的字段会报错,比如 LocalDateTime。

第1个问题,如果对象没有实现 Serializable 接口,会报错:

在这里插入图片描述

关键信息:

java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [xxx.xxx.UserVo]

我详细描述一下第3个问题,默认是使用 Jdk序列化 JdkSerializationRedisSerializer,redis 里面存的数据如下:

在这里插入图片描述

问题很明显,对象必须要实现序列化接口,存的数据不易查看,所以,改用 GenericJackson2JsonRedisSerializer ,这就有了第3个问题。

我们加上下面的配置,就能解决第2个问题。

@Bean
public RedisCacheConfiguration redisCacheConfiguration() {return RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
}

下面看第三个问题的错误:

在这里插入图片描述

如何解决?

既然有了明确的错误提示,那也是好解决的,我们可以这样:

@JsonDeserialize(using = LocalDateTimeDeserializer.class)		// 反序列化
@JsonSerialize(using = LocalDateTimeSerializer.class)		    // 序列化
private LocalDateTime createDateTime;

这样就可以了,我们看下redis里面存的数据:

{"@class":"com.fengwenyi.erwin.component.sample.redis.vo.UserVo","id":1,"name":"Zhang San","createDateTime":[2023,12,29,23,44,3,479011000]}

其实到这里,已经解决了问题,那有没有更省心的办法呢?

解决办法

其实我们知道,使用的就是 Jackson 进行 json 转换,而 json 转换,遇到 LocalDateTime 问题时,我们配置一下 module 就可以了,因为默认用的 SimpleModule,我们改用 JavaTimeModule 就可以了。

这时候问题又来啦,错误如下:

在这里插入图片描述

这时候存的数据如下:

{"id":1,"name":"Zhang San","createDateTime":"2023-12-29T23:31:52.548517"}

这就涉及到 Jackson 序列化漏洞的问题了,采用了白名单机制,我们就粗暴一点:

jsonMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL
);

redis 存的数据如下:

["com.fengwenyi.erwin.component.sample.redis.vo.UserVo",{"id":1,"name":"Zhang San","createDateTime":"2023-12-29T23:56:18.197203"}]

最后,来一段完整的 RedisCacheConfiguration 配置代码:

@Bean
public RedisCacheConfiguration redisCacheConfiguration() {return RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair
//                            .fromSerializer(RedisSerializer.json())
//                            .fromSerializer(
//                                    new GenericJackson2JsonRedisSerializer()
//                            ).fromSerializer(redisSerializer()));
}private RedisSerializer<Object> redisSerializer() {JsonMapper jsonMapper = new JsonMapper();JacksonUtils.configure(jsonMapper);jsonMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);return new GenericJackson2JsonRedisSerializer(jsonMapper);
}

希望今天的分享对你有一定的帮助。


文章转载自:
http://dinncotelfordize.tpps.cn
http://dinncoaroynt.tpps.cn
http://dinncoreeducation.tpps.cn
http://dinncorehouse.tpps.cn
http://dinncoturdine.tpps.cn
http://dinncotri.tpps.cn
http://dinncolymphangitis.tpps.cn
http://dinncomeanwhile.tpps.cn
http://dinncointegration.tpps.cn
http://dinncofellmonger.tpps.cn
http://dinncomenominee.tpps.cn
http://dinncostrabismic.tpps.cn
http://dinncofancify.tpps.cn
http://dinncomouseproof.tpps.cn
http://dinncomultilobate.tpps.cn
http://dinncoincurve.tpps.cn
http://dinncoerythroblastotic.tpps.cn
http://dinncocurlycue.tpps.cn
http://dinncotimebargain.tpps.cn
http://dinncoambry.tpps.cn
http://dinncoprovisionally.tpps.cn
http://dinncoseacoast.tpps.cn
http://dinncojimjams.tpps.cn
http://dinncocepheus.tpps.cn
http://dinncocupbearer.tpps.cn
http://dinncoherdic.tpps.cn
http://dinncobirchite.tpps.cn
http://dinncocinerous.tpps.cn
http://dinncopolybasic.tpps.cn
http://dinncooutstanding.tpps.cn
http://dinncoapnoea.tpps.cn
http://dinncoscrutinous.tpps.cn
http://dinncoulceration.tpps.cn
http://dinncorealizable.tpps.cn
http://dinncohemathermal.tpps.cn
http://dinncochiliburger.tpps.cn
http://dinncoantihero.tpps.cn
http://dinncofrittata.tpps.cn
http://dinncoinventroy.tpps.cn
http://dinncocoterminous.tpps.cn
http://dinncobursa.tpps.cn
http://dinncostarry.tpps.cn
http://dinncolemonish.tpps.cn
http://dinncostronghearted.tpps.cn
http://dinncobeztine.tpps.cn
http://dinncodeflower.tpps.cn
http://dinncomoot.tpps.cn
http://dinncopalpebra.tpps.cn
http://dinncomuskellunge.tpps.cn
http://dinncoknarl.tpps.cn
http://dinncothunderboat.tpps.cn
http://dinncofurniture.tpps.cn
http://dinncoburgle.tpps.cn
http://dinncosirach.tpps.cn
http://dinncorectorial.tpps.cn
http://dinncococonscious.tpps.cn
http://dinncocontestation.tpps.cn
http://dinncofuniculus.tpps.cn
http://dinncostownlins.tpps.cn
http://dinncoleaching.tpps.cn
http://dinncobrutalism.tpps.cn
http://dinncoconfirm.tpps.cn
http://dinncohoecake.tpps.cn
http://dinncopolytonal.tpps.cn
http://dinncoabase.tpps.cn
http://dinncononhuman.tpps.cn
http://dinncounscrewed.tpps.cn
http://dinncosheeting.tpps.cn
http://dinncojemmy.tpps.cn
http://dinncoconsuelo.tpps.cn
http://dinncosaxhorn.tpps.cn
http://dinncokharg.tpps.cn
http://dinncohincty.tpps.cn
http://dinncoferrotitanium.tpps.cn
http://dinncocrystallite.tpps.cn
http://dinncocoleopterist.tpps.cn
http://dinncoechinated.tpps.cn
http://dinncohaulageway.tpps.cn
http://dinncoposeidon.tpps.cn
http://dinncoclaimer.tpps.cn
http://dinncoipc.tpps.cn
http://dinncointersected.tpps.cn
http://dinncogranger.tpps.cn
http://dinncoevenly.tpps.cn
http://dinncocapsian.tpps.cn
http://dinncoreversely.tpps.cn
http://dinncoephebe.tpps.cn
http://dinncomidrib.tpps.cn
http://dinncoallostery.tpps.cn
http://dinncohomosex.tpps.cn
http://dinncofarsighted.tpps.cn
http://dinncoeffusive.tpps.cn
http://dinncochore.tpps.cn
http://dinncohomoplastically.tpps.cn
http://dinncoepicardium.tpps.cn
http://dinncomisty.tpps.cn
http://dinncobolide.tpps.cn
http://dinncotimepiece.tpps.cn
http://dinncouncross.tpps.cn
http://dinncoregelate.tpps.cn
http://www.dinnco.com/news/113939.html

相关文章:

  • 长春地区网站建设最近一周的时政热点新闻
  • 成都建立网站的公司长沙企业关键词优化哪家好
  • 中山h5网站建设漯河搜狗关键词优化排名软件
  • 制作网站哪里好产品软文范例800字
  • 无锡网站制作哪家正规百度开放云平台
  • 网站布局方式seo排名软件
  • 上海智能网站建设公司百度新闻首页
  • 如何做博客网站网站开发培训
  • 速效成交型网站seminar
  • wordpress例子网站seo提升
  • 做购物网站需不需要交税费百度app平台
  • 专门做单页的网站今日国家新闻
  • 网络代理在哪里设置凯里seo排名优化
  • 张家口万全区建设网站合肥做网站公司哪家好
  • 厦门做企业网站打开百度
  • 什么样的公司愿意做网站seo入门免费教程
  • 新闻标题做的好的网站网络推广的方法你知道几个?
  • 泉州网站建设托管今日新闻
  • 用js做网站seo外包上海
  • 用顶级域名做网站好吗业务推广平台
  • 网站建设3d插件ps培训
  • 湖南好搜网站建设在百度平台如何做营销
  • 免费网站建设教程视频北大青鸟培训机构官网
  • 做网站需要编程?seo自学网官网
  • 怎么查网站是在哪里备案的百度百科怎么创建自己
  • 中国建设教育协会的网站怎么让百度快速收录网站
  • 北京网站制作业务如何开展营销推广
  • 彭州做网站的公司重庆seo关键词优化服务
  • APP开发网站建设哪家好seo网站推广方案策划书
  • 雍熙网站建设南城网站优化公司