zoukankan      html  css  js  c++  java
  • springboot中redis做缓存时的配置

    import com.google.common.collect.ImmutableMap;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.cache.Cache;
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.interceptor.CacheErrorHandler;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.RedisConnectionFactory;

    import java.time.Duration;
    import java.util.Map;

    /**
    * @author cjy
    * @description
    * @date 2019/12/24
    **/
    @Configuration
    public class RedisConfig extends CachingConfigurerSupport {

    private static Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class);

    /**
    * 缓存管理器
    */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory lettuceConnectionFactory) {
    RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
    // 设置缓存管理器管理的缓存的默认过期时间
    .entryTtl(Duration.ofMinutes(5))
    // 不缓存空值
    .disableCachingNullValues();
    Map<String, RedisCacheConfiguration> configMap = ImmutableMap.<String, RedisCacheConfiguration>builder()
    .put("your cacheName", RedisCacheConfiguration.defaultCacheConfig().entryTtl(
    Duration.ofMinutes(1)
    ))
    .build();
    return RedisCacheManager.builder(lettuceConnectionFactory)
    .cacheDefaults(defaultCacheConfig)
    .withInitialCacheConfigurations(configMap)
    .build();
    }

    /**
    * redis数据操作异常处理 这里的处理:在日志中打印出错误信息,但是放行
    * 保证redis服务器出现连接等问题的时候不影响程序的正常运行,使得能够出问题时不用缓存
    *
    * @return
    */
    @Bean
    @Override
    public CacheErrorHandler errorHandler() {
    return new CacheErrorHandler() {
    @Override
    public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
    redisErrorException(exception, key);
    }
    @Override
    public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
    redisErrorException(exception, key);
    }
    @Override
    public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
    redisErrorException(exception, key);
    }
    @Override
    public void handleCacheClearError(RuntimeException exception, Cache cache) {
    redisErrorException(exception, null);
    }
    };
    }

    private void redisErrorException(Exception exception, Object key) {
    LOGGER.error("Redis exception: key={}", key, exception);
    }
    }
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法训练 黑色星期五
    Java实现 蓝桥杯VIP 算法训练 比赛安排
    Java实现 蓝桥杯VIP 算法训练 比赛安排
    Java实现 蓝桥杯VIP 算法训练 斜率计算
    Java实现 蓝桥杯VIP 算法训练 斜率计算
    Java实现 蓝桥杯VIP 算法训练 整数平均值
    Java实现 蓝桥杯VIP 算法训练 整数平均值
    控件动态产生器(使用RegisterClasses提前进行注册)
    Delphi编写自定义控件以及接口的使用(做了一个TpgDbEdit)
    Log4delphi使用心得
  • 原文地址:https://www.cnblogs.com/carrychan/p/12089811.html
Copyright © 2011-2022 走看看