• 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);
    }
    }
  • 相关阅读:
    [React & Testing] Simulate Event testing
    [React & Testing] Snapshot testings
    [Node & Tests] Intergration tests for Authentication
    [CSS] Build a Fluid Loading Animation in CSS
    [Node & Testing] Intergration Testing with Node Express
    [Node] Stateful Session Management for login, logout and signup
    [Node.js] Serve Static Files with Express
    [JWT] JWT with HS256
    [Redux] Understand Redux Higher Order Reducers
    [Vue + TS] Create your own Decorators in Vue with TypeScript
  • 原文地址:https://www.cnblogs.com/carrychan/p/12089811.html
Copyright © 2020-2023  润新知