SpringBoot 2.x 使用Redis作为缓存 设置有效时间

redis 配置

 redis:
    database: 0
    host: localhost
    port: 6379
    password:
    jedis:
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0
    timeout: 0

redis config

 @Bean
    CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        /* 默认配置, 默认超时时间为30s */
        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration
                .ofSeconds(30L)).disableCachingNullValues();
        RedisCacheManager cacheManager = RedisCacheManager.builder(RedisCacheWriter.lockingRedisCacheWriter
                (connectionFactory)).cacheDefaults(defaultCacheConfig).transactionAware().build();
        return cacheManager;
    }

在serviceImpl里面用注解

@Cacheable(key = "'pageNum=' + #pageNum + '&pageSize=' + #pageSize")

参考
https://blog.csdn.net/yingziisme/article/details/81463391

猜你喜欢

转载自blog.csdn.net/u012724418/article/details/84396661