@Cacheable 注解的 CacheManager
@Cacheable 注解的 CacheManager
@Cacheable 注解的使用
开发中经常会使用Redis 缓存,可以使用 @Cacheable 相关的注解来操作缓存。
详情见:
CacheManager
@Cacheable 注解有一个属性,cacheManager ,可以自定义缓存管理器。
pom.xml 依赖包:
注意,需要用 springBoot 2.0 以上的 版本。
<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>
application.properties
配置如下:
# ############## redis config##########redis.host= xx.xx.xx.xx
redis.port=6579
redis.password=xxxxredis.master=xx
redis.host.proxy=xxxx
redis.port.proxy=6579
redis.password.proxy=xxxx#redis cluster
redis.cache.clusterNodes=xxx:xxx,xxx:xxx,xxx:xxx
redis.cache.password=xxx
redis.cachemandTimeout=1000
redis.cache.soTimeout=3000
redis.cache.maxAttempts=5
redis.cache.expireSeconds=120
redis.pool.maxActive=30
redis.pool.maxIdle=10
redis.pool.maxTotal=1024
redis.pool.maxWaitMillis=2000
redis.pool.testOnBorrow=false# ############## redis config end##########
代码如下:
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apachemons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.*;
import redis.clients.jedis.JedisPoolConfig;import java.time.Duration;@Configuration
@EnableCaching
public class RedisConfig {@Value("${spring.redis.host:localhost}")private String host;@Value("${spring.redis.port:6379}")private int port;@Value("${spring.redis.timeout:30000}")private int timeout;@Value("${spring.redis.password}")private String password;@Value("${spring.redis.pool.max-active:8}")private int maxActive;@Value("${spring.redis.pool.max-wait:-1}")private int maxWait;@Value("${spring.redis.pool.max-idle:8}")private int maxIdle;@Value("${spring.redis.pool.min-idle:0}")private int minIdle;/*** 用 @Primary 注解修饰,表示是默认的缓存管理器。* <p>* 缓存一小时。** @return*/@Bean@Primarypublic CacheManager cacheManagerOneHour() {Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = getJackson2JsonRedisSerializer();JedisConnectionFactory jedisConnectionFactory = redisConnectionFactory();return RedisCacheManager.builder(jedisConnectionFactory).cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))//entryTtl 表示缓存的时间. 24*60.entryTtl(Duration.ofMinutes(1440))).build();}/*** 缓存一天** @return*/@Beanpublic CacheManager cacheManagerOneDay() {Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = getJackson2JsonRedisSerializer();JedisConnectionFactory jedisConnectionFactory = redisConnectionFactory();return RedisCacheManager.builder(jedisConnectionFactory).cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))//entryTtl 表示缓存的时间.entryTtl(Duration.ofDays(1))).build();}/*** 序列化** @return*/public Jackson2JsonRedisSerializer<Object> getJackson2JsonRedisSerializer() {Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);return jackson2JsonRedisSerializer;}/*** redis 连接** @return*/@Beanpublic JedisConnectionFactory redisConnectionFactory() {RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();redisStandaloneConfiguration.setHostName(host);redisStandaloneConfiguration.setPort(port);redisStandaloneConfiguration.setPassword(password);
// redisStandaloneConfiguration.setDatabase();return new JedisConnectionFactory(redisStandaloneConfiguration);}}
最新文章
- 电脑的日常维护与故障诊断常识
- 九九乘法表c 语言
- 【shardingjdbc】sharding
- 学生用台灯什么光对眼睛好?分享专业的学生护眼台灯
- 自定义参数解析器,翻译实体属性
- 电子零部件工厂的WMS系统:业务特点、产品特点与优势
- 中国1024程序员节·上海站纪实
- k8s的service自动发现服务:实战版
- HTML设置标签栏的图标
- 【汇编】汇编语言的介绍
- SDL2 播放视频数据(YUV420P)
- HTTP——
- Transformers 中原生支持的量化方案概述
- Python中的数据增强技术
- 安全区域边界(设备和技术注解)
- 使用opencv实现图像的畸形矫正:仿射变换
- 【c++】——类和对象(中)——实现完整的日期类