可以使用定时任务或者设置缓存的过期时间来解决该问题。
代码示例:
@Component
public class CacheClearTask {
@Autowired
private CacheManager cacheManager;
@Scheduled(fixedDelay = 60 * 60 * 1000) // 定时任务每隔一小时执行一次
public void clearCache() {
Cache cache = cacheManager.getCache("myCache"); // 获取需要清除缓存的Cache
if (cache != null) {
cache.clear(); // 清除缓存
}
}
}
代码示例:
@Cacheable(value = "myCache", key = "#id", expire = 3600) // 缓存保存1小时
public String getInfoById(Integer id) {
// 查询数据库或者其他操作
}
其中,value
表示该缓存的名称,key
表示缓存的key,expire
表示缓存的过期时间(单位为秒)。
下一篇:APIM未在指定时间后清除缓存