赞
踩
通过注解形式实现数据库和redis中数据同步 @Cacheable 用于方法上
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
spring.redis.database=10
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
重点: @Cacheable(cacheNames = “gatewayHandler”, key = “‘getGatewayHandler’”)
如果 key = “getGatewayHandler” 不加上 ‘getGatewayHandler’ 则会抛出以下异常
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Sep 24 10:07:01 CST 2021
There was an unexpected error (type=Internal Server Error, status=500).
EL1008E: Property or field 'getGatewayHandler' cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject' - maybe not public or not valid?
/** * @author sean * @Description TODO * @createTime 2021年09月23日 21:04:00 */ @RestController @RequestMapping("/redis/") public class RedisController { @Autowired private GatewayHandlerDbService gatewayHandlerDbService; /** * 注解版本缓存数据 * * @return */ @Cacheable(cacheNames = "gatewayHandler", key = "'getGatewayHandler'") @GetMapping("/set/handler/") public Object getGatewayHandler() { //如果 redis中没有则执行该方法去数据库查询,如果redis中有则无需执行该操作 return gatewayHandlerDbService.searchAll(); } }
/**
* @author sean
* @createTime 2021年09月23日 20:38:00
*/
@SpringBootApplication
@MapperScan("com.sean.springboot.design.mapper")
//开始redis缓存注解
@EnableCaching
public class RedisApp {
public static void main(String[] args) {
SpringApplication.run(RedisApp.class, args);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。