Spring Cache redis缓存配置

package com.baojiaren.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
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.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * @author ✎ℳ๓₯㎕.倾心❀、
 * @create 2021-07-20-12:29
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        //使用Jackson2JsonRedisSerializer替换默认的序列化规则
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        //设置value的序列化规则
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        //设置key的序列化规则
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory){
        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(1))
                .disableCachingNullValues()
                .serializeKeysWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();
    }
}

server:
  tomcat:
    threads:
      max: 10 #降低最大线程数方便模拟高并发
  port: 9090
  compression:
    #是否开启Gzip压缩
    enabled: true
spring:
  redis:
    timeout: 10000 #连接超时时间
    host: 139.196.186.27
    port: 6379
    password: b6356120...
    database: 0
    lettuce:
      pool:
        max-active: 1024 #最大连接数 默认8
        max-wait: 10000
        max-idle: 200 # 最大空闲连接 默认8
        min-idle: 5   #最小空闲连接 默认0
  devtools:
    restart:
      enabled: true  #热部署生效
  mvc:
    hiddenmethod:
      filter:
        enabled: true
  application:
    name: order-service-rest
    freemarker:
      prefer-file-system-access: false #是否优先从文件系统加载template
  cache:
    type: redis
eureka:
  instance:
    hostname: service-provider #主机名 不配置的时候将根据操作系统的主机名来获取
    prefer-ip-address: true #是否使用ip地址注册
    instance-id: ${spring.cloud.client.ip-address}:${server.port} #ip port
  client:
    registerWithEureka: false #是否将自己注册到注册中心,默认为true
    serviceUrl:             # 注册中心对外暴露的注册地址
      defaultZone: http://root:123456@localhost:8761/eureka/
    registry-fetch-interval-seconds: 10 #表示 Eureka Client 间隔多久去服务器拉取注册信息 默认为30秒

#为所有服务配置统一规则
#service-product: #为调用服务的名称
#  ribbon:
#    #配置随机策略
#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
#    #指定具体的provider 服务列表, 多个用逗号隔开
#    listOfServers: http://localhost:7070,http://localhost:7071
feign:
  httpclient:
    enabled: true #开启httpClient连接池
##关闭 Eureka  实现Ribbon 点对点直连
#ribbon:
#  eureka:
#    enabled: false # false:关闭, true:开启

#ribbon: #全局配置
#  ConnectTimeout: 5000 #请求连接的超时 时间 默认的时间为1秒
#  ReadTimeout: 5000 #请求处理超时 时间
service-product: #为调用服务的名称
  ribbon:
    #配置随机策略
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
    OkToRetryOnAllOperations: true #对所有请求都进行重试
    MaxAutoRetries: 2   #对当前实例的重试次数
    MaxAutoRetriesNextServer: 0 #切换实例的重试次数
    ConnectTimeout: 3000 #请求连接的超时 时间 默认的时间为1秒
    ReadTimeout: 3000 #请求处理超时 时间

   @Cacheable(cacheNames = "OrderList",unless="#result == null")
    @Override
    public Order selectOrderById(Integer id) {
        return new Order(id, "order-001", "中国", 3199D,
                Arrays.asList(productService.selectProductById(id)));
    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

௸྄ིོུ倾心ღ᭄ᝰꫛꫀꪝ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值