springboot整合redis

1.关键依赖
<!--redis-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--common-poo12-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.67</version>
</dependency>

2.配置文件

server:
  port: 9999
  servlet:
    context-path: /kgc

spring:
  redis:
    port: 6379
    host: localhost
  cache:
    type: redis                 # 缓存类型
    cache-names: xxx,yyy
    redis:
      cache-null-values: true  # 允许Redis缓存空值
      key-prefix: dacai_
      time-to-live: 0ms         # 缓存超时时间戳,配置为0则永不过期
      use-key-prefix: true      # 启用Redis的键前缀


  thymeleaf:
    cache: false
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql:///db2109
    #type第三方连接池的主类
  mvc:
    format:
      date: yyyy-MM-dd
    hiddenmethod:
      filter:
        enabled: true

mybatis:
  mapper-locations: classpath:/mybatis/mapper/*.xml
  type-aliases-package: cn.kgc.*.entity
  configuration:
    map-underscore-to-camel-case: true

logging:
  level:
    cn.kgc: debug

3.config.RedisConfig类

@Configuration
@EnableCaching //启用缓存注解
public class RedisConfig extends CachingConfigurerSupport {

    /*@Autowired
    private RedisConnectionFactory connectionFactory;*/
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(connectionFactory);

        // 指定k/v的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
        redisTemplate.setValueSerializer(serializer);

        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        om.setTimeZone(TimeZone.getDefault());
        om.configure(MapperFeature.USE_ANNOTATIONS, false);
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        // 此项必须配置,否则会报java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to XXX
        //om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);//过时方法,用activateDefaultTyping替代
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        serializer.setObjectMapper(om);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(serializer);

        return  redisTemplate;
    }

    @Bean
    public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));
        return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
    }
}

4. service.xx类impl

@Autowired
private RedisTemplate redisTemplate;

@Cacheable(cacheNames = "xxx",key = "'getEmpList'")
@Cacheable(cacheNames = "xxx",key = "'emp:id'+#id")

增改

@CachePut(cacheNames = "xxx",key = "'emp:id'+#shopping.id")

@CacheEvict(cacheNames = "xxx",key = "'emp:id'+#id")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值