gpt4 book ai didi

mysql - 如何使用 spring 应用程序中的 key 将缓存的 mysql 数据作为 redis 缓存中的单个项目读取

转载 作者:行者123 更新时间:2023-11-29 15:39:17 25 4
gpt4 key购买 nike

在第一次运行时缓存单项数据后,后续运行会生成无法转换为错误。我想解决将单项存储在缓存中后获取单项的问题。

这是一个gradle Spring应用程序,它将数据存储在MySQL数据库中,并使用redis作为缓存管理器。

服务实现者类

@Autowired
private ClientRepository clientRepository;
//This doesn't work
@Override
@Cacheable(value = "clientCache", key = "{#clientId}")
public Client getClientById(long clientId) {
System.out.println("--- Inside getClientById() ---");
return clientRepository.findById(clientId).get();
}

//This works
@Override
@Cacheable(value = "allClientsCache")
public List<Client> getAllClients() {
System.out.println("--- Inside getAllClients() ---");
List<Client> list = new ArrayList<>();
clientRepository.findAll().forEach(e -> list.add(e));
return list;
}

Controller 类别

@GetMapping("/client/{clientId}")
@ResponseBody
public ResponseEntity<Client> getClientById (@PathVariable Long clientId){
Client client = clientService.getClientById(clientId);
return new ResponseEntity<>(client, HttpStatus.OK);
}
@GetMapping("/clients")
public ResponseEntity<List<Client>> getAllClients() {
List<Client> list = clientService.getAllClients();
return new ResponseEntity<>(list, HttpStatus.OK);
}

REDIS 配置类

@Autowired
private Environment env;

@Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisConf = new RedisStandaloneConfiguration();
return new LettuceConnectionFactory(redisConf);
}

@Bean
public RedisCacheManager cacheManager() {
RedisCacheManager rcm = RedisCacheManager.create(redisConnectionFactory());
rcm.setTransactionAware(true);
return rcm;
}

@Bean
public RedisTemplate<?, ?> redisTemplate() {
final RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}

我希望在浏览器上我会看到一个 json 客户端数据

查询单个项目时显示的错误是:

    There was an unexpected error (type=Internal Server Error, status=500).
com.example.redis.Models.ClientModel.Client cannot be cast to com.example.redis.Models.ClientModel.Client
java.lang.ClassCastException: com.example.redis.Models.ClientModel.Client cannot be cast to com.example.redis.Models.ClientModel.Client
at com.example.redis.ServiceImplementers.Client.ClientServiceImpl$$EnhancerBySpringCGLIB$$4620f242.getClientById(<generated>)
at com.example.redis.Controllers.ClientController.ClientController.getClientById(ClientController.java:23)
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:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

在终端上,redis 已将该项目存储为

redis 127.0.0.1:6379> mget "clientCache::2"
1) "\xac\xed\x00\x05sr\x00\x1ecom.example.redis.Model.Client\x00\x00\x00\x00\x00\x00\x00\x01\x02\x00\nI\x00\x06activeJ\x00\bclientIdI\x00\x0bclient_codeI\x00\ncountry_idJ\x00\x06msisdnI\x00\x05phoneL\x00\aaddresst\x00\x12Ljava/lang/String;L\x00\x0bclient_nameq\x00~\x00\x01L\x00\x0bdescriptionq\x00~\x00\x01L\x00\x05emailq\x00~\x00\x01xp\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\a\x9b\x00\x00\x00\x03\x00\x00\x00hI\xa8\x00N)\xcb\xfcHt\x00\aKampalat\x00\x0cMTN Holdingst\x03wLorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.t\x00\x0fkainfo@host.com"

最佳答案

关于mysql - 如何使用 spring 应用程序中的 key 将缓存的 mysql 数据作为 redis 缓存中的单个项目读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57848542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com