Java 为什么不是';我的Redis缓存在我的Spring Boot/Spring数据应用程序中不工作吗?
我正在向Spring Boot/Spring数据应用程序添加一个本地Redis缓存。据我所知,如果我做三件事,缓存应该可以工作:Java 为什么不是';我的Redis缓存在我的Spring Boot/Spring数据应用程序中不工作吗?,java,spring-boot,redis,spring-data,Java,Spring Boot,Redis,Spring Data,我正在向Spring Boot/Spring数据应用程序添加一个本地Redis缓存。据我所知,如果我做三件事,缓存应该可以工作: 我已经用@Cacable注释了我的应用程序类: @Cacheable(cacheNames = "menuItems") @SpringBootApplication @ComponentScan(basePackages = { ... }) public class OpenAPI2SpringBoot implements CommandLi
@Cacheable(cacheNames = "menuItems")
@SpringBootApplication
@ComponentScan(basePackages = { ... })
public class OpenAPI2SpringBoot implements CommandLineRunner { ... }
#Redis cache
spring.cache.type=redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
@Component
public class MenuItemRepository {
static final String MENU_ITEM_CACHE = "menuItems";
// this is declared like this:
// class MenuItemUncachedRepository extends JpaRepository<MenuItem, Integer>
private final MenuItemUncachedRepository menuItemRepository; // actual repo.
@Autowired
public MenuItemRepository(MenuItemUncachedRepository repository) {
menuItemRepository = repository;
}
public MenuItem findOne(Integer id) {
return menuItemRepository.findOne(id);
}
@Cacheable(cacheNames = MENU_ITEM_CACHE, key = "all")
public List<MenuItem> findAll() {
return menuItemRepository.findAll();
}
@CacheEvict(cacheNames = MenuItemRepository.MENU_ITEM_CACHE)
public <M extends MenuItem> M save(M menuItem) {
return menuItemRepository.save(menuItem);
}
}
@组件
公共类MenuItemRepository{
静态最终字符串菜单\u项\u CACHE=“menuItems”;
//声明如下:
//类menuitemucachedepository扩展了JpaRepository
private final MenuItemUncachedRepository menuItemRepository;//实际回购。
@自动连线
公共菜单项存储库(菜单项存储库){
menuItemRepository=存储库;
}
公共菜单项findOne(整数id){
返回menuItemRepository.findOne(id);
}
@可缓存(cacheNames=菜单项缓存,key=“all”)
公共列表findAll(){
返回menuItemRepository.findAll();
}
@CacheExit(cacheNames=MenuItemRepository.MENU\u ITEM\u CACHE)
公共M保存(M菜单项){
返回menuItemRepository.save(menuItem);
}
}
我的实际存储库不是公共的,所以所有调用都需要经过带注释的包装器类。日志记录确认正在调用带注释的方法
因此,我启动了缓存监视器,如下所示:
redisclimonitor
我正在从应用程序获取数据,因此我知道存储库工作正常。当我执行findAll()方法时,我应该看到在监视器中设置了我的值,但什么也没有发生。我是Redis新手,所以它可能很小。问题在于您的OpenAPI2SpringBoot
类。您不需要在其上添加@Cacheable
注释,您需要@EnableCaching