繁体   English   中英

在生产中绕过@Cacheable批注进行调试

[英]Bypassing the @Cacheable annotation in Production for debugging

我正在研究一个基于Spring的新项目,我想知道,如果我要使用Spring 3.1或ehcache-spring-annotation项目中的@Cacheable批注,您将如何实现一种绕过缓存的机制“按需”机制,例如用于生产调试。

假设我的应用程序有时仅服务REST请求,而我想通过一次调用(我手动进行)直接从“ DB”(或后端)中获取数据,并避免在服务器上进行任何缓存方式( 任何缓存)。 当调用运行时,我真的不介意是否用从数据库中获取的内容重新填充缓存...

我想听听您对如何实现这一目标的想法。

预先感谢您的指导,

到达与@Bozho建议的结果类似的结果如果传递了请求参数refresh-cache = true ,则绕过缓存

我扩展了DefaultRedisCacheWriter并将其用作CacheWriter

    RedisCacheManager cacheManager = RedisCacheManager
            .builder(new RefreshableRedisCacheWriter(redisConnectionFactory()))
            .cacheDefaults(cacheConfiguration)
            .build();

覆盖此编写器中的get()方法

@Override
public byte[] get(String name, byte[] key) {

    Assert.notNull(name, "Name must not be null!");
    Assert.notNull(key, "Key must not be null!");

    HttpServletRequest request = ApiRequestContext.getRequestContext();
    if (Boolean.valueOf(request.getParameter("refresh-cache"))) {
        logger.info("bypassing cache for key: {}", key);
        return null;
    }

    return execute(name, connection -> connection.get(key));
}

ApiRequestContext是一个自定义请求对象,存储在本地线程中。 请注意,当此方法返回null时,将从数据库中获取值并将其放回高速缓存中,从而有效地刷新高速缓存。

也许您可以扩展缓存管理器,并在现有检查之外提供布尔表达式。

您可以在控制器的ThreadLocal中设置该布尔值,以便每当调用服务时,布尔值就位。

您可以创建一种刷新缓存的方法,并且仅当需要从数据库获取值时才使用调试器运行它。

@Cacheable(modelId = "i18NTextCacheModel")
public List<I18NText> loadAll() {

    logger.debug("loadAll called");
    ...
}

@CacheFlush(modelId = "i18NTextFlushModel")
public Long save(I18NText entity) {

    logger.debug("save called");
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM