使用Spring @Cacheable注解

2022-03-18 07:10:22 浏览数 (1)

1、性能优化临时处理方案使用Spring @Cacheable注解

1.1、随着服务器的QPS值的提高,导致很多用户在我们项目多个页面数据加载很慢。在赶别的版本临时使用Spring @Cacheable注解以及Redis做了常调接口以及短期变动不大接口的响应缓存。出现了以下BUG做个解决记录方案。

1.2、获取缓存异常:java.util.LinkedHashMap cannot be cast to XXX.XXX

代码语言:javascript复制
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* 2021-11-16 18:17:27,225 ERROR com.zhili.common.controller.BaseController - [39] - java.util.LinkedHashMap cannot be cast to com.zhili.common.result.ResultListBo2021-11-16 18:17:27,225 ERROR com.zhili.common.controller.BaseController - [39] - java.util.LinkedHashMap cannot be cast to com.zhili.common.result.ResultVo2021-11-16 18:17:27,225 ERROR com.zhili.common.controller.BaseController - [39] - java.util.LinkedHashMap cannot be cast to com.zhili.common.result.ResultListVo2021-11-16 18:17:27,229 ERROR com.zhili.common.exception.ExceptionHandler - [37] - Exception -- 500 >java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.zhili.common.result.ResultVo	at com.zhili.kanli.controller.talent.TalentUserController$$EnhancerBySpringCGLIB$$ac8b3bc0.getTalentUserCenter(<generated>)	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)	at java.lang.reflect.Method.invoke(Method.java:498)	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)	at javax.servlet.http.HttpServlet.service(HttpServlet.java:503)	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)	at javax.servlet.http.HttpServlet.service(HttpServlet.java:590)	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:760)	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1617)	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:226)	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:108)	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:545)	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:536)	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235)	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1592)	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1296)	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:485)	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1562)	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1211)	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)	at org.eclipse.jetty.server.Server.handle(Server.java:500)	at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:386)	at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:562)	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:378)	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:270)	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:117)	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336)	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313)	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171)	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129)	at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:388)	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:806)	at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:938)	at java.lang.Thread.run(Thread.java:748)
*/

1.3、根据报错结果显示就是获取缓存的时候转换成我们自己定义的Object出现了错误。于是便检查新增处理缓存的方法。

代码语言:javascript复制
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {    // 设置 json 序列化    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);    ObjectMapper om = new ObjectMapper();    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);    jackson2JsonRedisSerializer.setObjectMapper(om);     RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();    redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(                    RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).            // 设置过期时间                    entryTtl(Duration.ofSeconds(seconds));     return redisCacheConfiguration;}

出现这种异常,需要向自定义ObjectMapper,设置一些参数,而不是直接使用Jackson2JsonRedisSerializer类中黙认的ObjectMapper,看源代码可以知道,Jackson2JsonRedisSerializer中的ObjectMapper是直接使用new ObjectMapper()创建的,这样ObjectMapper会将Redis中的字符串反序列化为java.util.LinkedHashMap类型,导致后续Spring对其进行转换成报错。其实我们只要它返回Object类型就可以了。

以下修改了ObjectMapper一些参数,构建一个Jackson2JsonRedisSerializer对象,将其注入RedisCacheConfiguration即可。

代码语言:javascript复制
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {    // 设置 json 序列化    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);    ObjectMapper om = new ObjectMapper();    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);    om.configure(MapperFeature.USE_ANNOTATIONS, false);    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);    // 此项必须配置,否则会报java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to XXX    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);    om.setSerializationInclusion(JsonInclude.Include.NON_NULL);    jackson2JsonRedisSerializer.setObjectMapper(om);     RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();    redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(                    RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).            // 设置过期时间                    entryTtl(Duration.ofSeconds(seconds));     return redisCacheConfiguration;}

0 人点赞