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);
}
}
springboot中redis做缓存时的配置的更多相关文章
- springboot中redis取缓存类型转换异常
异常如下: [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested ...
- springboot中redis的缓存穿透问题
什么是缓存穿透问题?? 我们使用redis是为了减少数据库的压力,让尽量多的请求去承压能力比较大的redis,而不是数据库.但是高并发条件下,可能会在redis还没有缓存的时候,大量的请求同时进入,导 ...
- 在项目中redis做缓存的一些思路
首先,缓存的对象有三种: 1:数据库中单条的的数据(以表名跟id作为key永久保存到redis),在有更新的地方都要更新缓存(不适用于需要经常更新的数据): 2:对于一些不分页,不需要实时(需要多表查 ...
- springBoot 中redis 注解缓存的使用
1,首先在启动类上加上 @EnableCaching 这个注解 在查询类的controller,或service ,dao 中方法上加 @Cacheable 更新或修改方法上加 @CachePut 注 ...
- springboot利用redis做缓存
首先 配置redis redis: password: 123456 host: 127.0.0.1 port: 6379 #103.249.252.109:10086 expireSeconds: ...
- spring-boot集成mybatis,用redis做缓存
网上有很多例子了,执行源码起码有3个,都是各种各样的小问题. 现在做了个小demo,实现spring-boot 用redis做缓存的实例,简单记录下思路,分享下源码. 缓存的实现,分担了数据库的压力, ...
- SpringBoot之Mybatis操作中使用Redis做缓存
上一博客学习了SpringBoot集成Redis,今天这篇博客学习下Mybatis操作中使用Redis做缓存.这里其实主要学习几个注解:@CachePut.@Cacheable.@CacheEvict ...
- spring+redis的集成,redis做缓存
1.前言 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.我们都知道,在日常的应用中,数据库瓶颈是最容易出现的 ...
- springboot2.0+redis实现消息队列+redis做缓存+mysql
本博客仅供参考,本人实现没有问题. 1.环境 先安装redis.mysql 2.springboot2.0的项目搭建(请自行完成),本人是maven项目,因此只需配置,获取相应的jar包,配置贴出. ...
随机推荐
- js cookies 的写入、读取、删除
//写cookies //escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串.function setCookie(name,value) { var Days ...
- 小爬爬2:fiddler安装和了解fiddler
1.解压安装就可以了 fiddler优点:抓取移动和PC机器的请求 2.首先进行证书的配置 如果不配置只能抓取http的请求,https不能抓取. 先选择,第一个"清空所有内容" ...
- 使用iPhone为Apple Watch制作动画
(原文:Make Animations for APPLE WATCH Using iPhone 作者:Andy Drizen 译者:xiaoying) 无论要做一个像hamburger button ...
- python 嵌套列表
- Java练习 SDUT-1171_保留整数
C语言实验--保留整数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入一个字符串str1,把其中的连续非数字的字符 ...
- SDUT-2116_数据结构实验之链表一:顺序建立链表
数据结构实验之链表一:顺序建立链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入N个整数,按照输入的顺序建立单链 ...
- Flask学习之十三 日期和时间
英文博客地址:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xiii-dates-and-times 中文翻译地址: ...
- laravel重定向到上一个页面怎么带参数返回 withsucess 成功提示信息
//控制器中 return back()->with('success','操作成功'); //with的参数1是一个session变量名,参数2为该session变量值,在视图直接这样获取 @ ...
- tensorflow学习笔记(三十四):Saver(保存与加载模型)
Savertensorflow 中的 Saver 对象是用于 参数保存和恢复的.如何使用呢? 这里介绍了一些基本的用法. 官网中给出了这么一个例子: v1 = tf.Variable(..., nam ...
- Java中的Runnable、Callable、Future、FutureTask的区别与示例
Java中存在Runnable.Callable.Future.FutureTask这几个与线程相关的类或者接口,在Java中也是比较重要的几个概念,我们通过下面的简单示例来了解一下它们的作用于区别. ...