findCities() { return distributorMapper.selectCities();}@Override@Cacheable(cacheNames = {"car"}, key = "key2")public List findCities() { ..._cacheable 保存时长">
当前位置:   article > 正文

Redis:Spring Boot使用@Cacheable时设置部分缓存的过期时间_cacheable 保存时长

cacheable 保存时长

@Override
@Cacheable(cacheNames = {"distributor"}, key = "key1")
public List<CityVO> findCities() {
    return distributorMapper.selectCities();

@Override
@Cacheable(cacheNames = {"car"}, key = "key2")
public List<CityVO> findCities() {
    return distributorMapper.selectCities();

自定义缓存时间

distributor::key1 的有效期是永久的

car::key2 的有效期是6分钟

 

  1. @Slf4j
  2. @Configuration
  3. @EnableCaching //启用缓存
  4. public class RedisConfig {
  5. /**
  6. * 自定义缓存管理器
  7. */
  8. @Bean
  9. public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
  10. RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
  11. Set<String> cacheNames = new HashSet<>();
  12. cacheNames.add("car");
  13. cacheNames.add("distributor");
  14. ConcurrentHashMap<String, RedisCacheConfiguration> configMap = new ConcurrentHashMap<>();
  15. configMap.put("car", config.entryTtl(Duration.ofMinutes(6L)));//有效期6分钟
  16. configMap.put("distributor", config);//永久
  17. //需要先初始化缓存名称,再初始化其它的配置。
  18. RedisCacheManager cacheManager = RedisCacheManager.builder(factory).initialCacheNames(cacheNames).withInitialCacheConfigurations(configMap).build();
  19. return cacheManager;
  20. }
  21. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/353150
推荐阅读
相关标签