簡體   English   中英

為什么我的 Redis 緩存不能在我的 Spring-Boot/Spring-Data 應用程序中工作?

[英]Why isn't my Redis cache working in my Spring-Boot/Spring-Data application?

我正在向我的 Spring-Boot/Spring-Data 應用程序添加本地 Redis 緩存。 據我所知,如果我做三件事,緩存應該可以工作:

  1. 我已經用@Cachable 注釋了我的應用程序 class:

     @Cacheable(cacheNames = "menuItems") @SpringBootApplication @ComponentScan(basePackages = {... }) public class OpenAPI2SpringBoot implements CommandLineRunner {... }
  2. 我已將屬性添加到 application.properties:

     #Redis cache spring.cache.type=redis spring.redis.host=127.0.0.1 spring.redis.port=6379

我已經在包裝器 class 中注釋了我的存儲庫調用

@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);
  }
}

我的實際存儲庫不是公開的,因此所有調用都需要通過帶注釋的包裝器 class 調用 go。 記錄確認注釋的方法被調用。

所以我像這樣啟動了我的緩存監視器: redis-cli MONITOR我正在從應用程序中獲取數據,所以我知道存儲庫可以工作。 當我執行 findAll() 方法時,我應該在監視器中看到我的值被設置,但沒有任何反應。 我是 Redis 的新手,所以它可能很小。

問題出在您的OpenAPI2SpringBoot class 中。 你不需要@Cacheable注釋,你需要@EnableCaching

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM