如何通过@Cacheable注解记录缓存命中?

Ani*_* B. 3 java spring ehcache jcache spring-boot

下面的类中有一个方法:

    @Override
    @Transactional
    @Cacheable(value = "products", key = "#id")
    public Product getProduct(long id) throws ApplicationException {
        Product product = null;
        try {
            ProductEntity productEntity = productDAO.getProduct(id);
            product = productTransformer.toProduct(productEntity);
        } catch (SystemException ex) {
            throw new ApplicationException(ex.getCode(), ex.getMessage(), "Problem in DataLayer", "Data Layer Error",
                    new Object[] { ex });
        }
        return product;
    }
Run Code Online (Sandbox Code Playgroud)

该应用程序运行良好。但我希望在数据放入缓存时有一个缓存命中日志。我想通过 log4j.properties 记录它。

如何配置 application.properties 以便可以记录它?

Deh*_*oos 6

Spring 在 TRACE 级别内部记录其缓存工作流程。要启用此功能,请在您的 application.properties 文件中包含以下内容。

logging.level.org.springframework.cache=TRACE