在Redis缓存中动态扩展TTL

时间:2019-11-14 21:07:21

标签: spring-boot caching redis

我一直在尝试使用Redis来学习Spring Boot,并且我可以浏览this出色的文章,该文章几乎解释了所有内容。但是我一直在尝试找出是否有可能通过在每次获取缓存条目时将其放回缓存中来动态重置缓存条目的TTL。例如,我的缓存条目的ttl是1hr,我是否可以继续对其进行扩展并防止它在被主动访问时被逐出。

@Service
public ControlledCacheService {
@Cacheable(cacheNames = "myControlledCache")
public String getFromCache() {
    return null;
}

@CachePut(cacheNames = "myControlledCache")
public String populateCache(String value) {
    return value;
}
}

.
.
.
@Autowired
ControlledCacheService controlledCacheService;
private String getFromControlledCache() {
    String fromCache = controlledCacheService.getFromCache();
    if (fromCache == null) {
        log.info("Oups - Cache was empty. Going to populate it");
        String myValue = "valueToPutInCache"
        String newValue = controlledCacheService.populateCache(myValue);
        log.info("Populated Cache with: {}", newValue);
        return newValue;
    }
    log.info("Returning from Cache: {}", fromCache);
    controlledCacheService.populateCache(myValue);  // **will calling this reset the ttl ?**
    return fromCache;
}

0 个答案:

没有答案