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);
}
}

springboot中redis做缓存时的配置的更多相关文章

  1. springboot中redis取缓存类型转换异常

    异常如下: [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested ...

  2. springboot中redis的缓存穿透问题

    什么是缓存穿透问题?? 我们使用redis是为了减少数据库的压力,让尽量多的请求去承压能力比较大的redis,而不是数据库.但是高并发条件下,可能会在redis还没有缓存的时候,大量的请求同时进入,导 ...

  3. 在项目中redis做缓存的一些思路

    首先,缓存的对象有三种: 1:数据库中单条的的数据(以表名跟id作为key永久保存到redis),在有更新的地方都要更新缓存(不适用于需要经常更新的数据): 2:对于一些不分页,不需要实时(需要多表查 ...

  4. springBoot 中redis 注解缓存的使用

    1,首先在启动类上加上 @EnableCaching 这个注解 在查询类的controller,或service ,dao 中方法上加 @Cacheable 更新或修改方法上加 @CachePut 注 ...

  5. springboot利用redis做缓存

    首先 配置redis redis: password: 123456 host: 127.0.0.1 port: 6379 #103.249.252.109:10086 expireSeconds: ...

  6. spring-boot集成mybatis,用redis做缓存

    网上有很多例子了,执行源码起码有3个,都是各种各样的小问题. 现在做了个小demo,实现spring-boot 用redis做缓存的实例,简单记录下思路,分享下源码. 缓存的实现,分担了数据库的压力, ...

  7. SpringBoot之Mybatis操作中使用Redis做缓存

    上一博客学习了SpringBoot集成Redis,今天这篇博客学习下Mybatis操作中使用Redis做缓存.这里其实主要学习几个注解:@CachePut.@Cacheable.@CacheEvict ...

  8. spring+redis的集成,redis做缓存

    1.前言 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.我们都知道,在日常的应用中,数据库瓶颈是最容易出现的 ...

  9. springboot2.0+redis实现消息队列+redis做缓存+mysql

    本博客仅供参考,本人实现没有问题. 1.环境 先安装redis.mysql 2.springboot2.0的项目搭建(请自行完成),本人是maven项目,因此只需配置,获取相应的jar包,配置贴出. ...

随机推荐

  1. JavaScript--开关思想

    就是男默女泪的立flag! <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  2. AtCoder Regular Contest 084 C - Snuke Festival【二分】

    C - Snuke Festival ....最后想到了,可是不应该枚举a[],这样要二重循环,而应该枚举b[],这样只需一重循环... #include<iostream> #inclu ...

  3. C++之MD5加密(签名)

    md5.h : #include <stdio.h> #include <stdlib.h>#include <time.h> #include <strin ...

  4. 【NS2】NS2机制浅显分析一下(转载)

    [我在之前看的是以ping协议为实例来理解TclCL机制和分裂对象模型] 本文以channel实例的创建过程为例,试图说明ns2的分裂机制,请在阅读本文前阅读<The NS Manual> ...

  5. 3DMAX安装失败怎样卸载重新安装3DMAX,解决3DMAX安装失败的方法总结

    技术帖:3DMAX没有按照正确方式卸载,导致3DMAX安装失败.楼主也查过网上关于如何解决3DMAX安装失败的一些文章,是说删除几个3DMAX文件和3DMAX软件注册表就可以解决3DMAX安装失败的问 ...

  6. Flask学习之三 web表单

    本部分Miguel Grinberg教程的翻译地址:http://www.pythondoc.com/flask-mega-tutorial/webforms.html 开源中国的:http://ww ...

  7. oracle函数 DUMP(w[,x[,y[,z]]])

    [功能]返回数据类型.字节长度和在内部的存储位置. [参数] w为各种类型的字符串(如字符型.数值型.日期型……) x为返回位置用什么方式表达,可为:8,10,16或17,分别表示:8/10/16进制 ...

  8. es6 中let与const的简析

    1.let 它的作用类似于var,用来声明变量,但是所声明的变量,只在let命令所在的代码块内有效. if(true){ ; let b = ; } document.write(a); docume ...

  9. 【[Offer收割]编程练习赛9 B】水陆距离

    [题目链接]:http://hihocoder.com/problemset/problem/1478 [题意] [题解] 一开始把所有的水域的位置都加入到队列中去; 然后跑一个bfs. 第一次到达的 ...

  10. 常用开源网站:sourceforge,github,foss,launchpad,PortableApps,datamation,opensourcewindows,opensourceMac,apache.org,kde,

    常用开源网站:sourceforge,github,foss,launchpad,PortableApps,datamation,opensourcewindows,opensourceMac,apa ...