Java Code Examples for org.springframework.data.redis.cache.RedisCacheConfiguration

The following examples show how to use org.springframework.data.redis.cache.RedisCacheConfiguration. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
protected RedisCache createRedisCache(String name, @Nullable RedisCacheConfiguration cacheConfig) {
	if (StringUtil.isBlank(name) || !name.contains(StringPool.HASH)) {
		return super.createRedisCache(name, cacheConfig);
	}
	String[] cacheArray = name.split(StringPool.HASH);
	if (cacheArray.length < 2) {
		return super.createRedisCache(name, cacheConfig);
	}
	String cacheName = cacheArray[0];
	if (cacheConfig != null) {
		// 转换时间,支持时间单位例如:300ms,第二个参数是默认单位
		Duration duration = DurationStyle.detectAndParse(cacheArray[1], ChronoUnit.SECONDS);
		cacheConfig = cacheConfig.entryTtl(duration);
	}
	return super.createRedisCache(cacheName, cacheConfig);
}
 
Example 2
@Primary
@Bean("redisCacheManager")
public RedisCacheManager redisCacheManager(RedisConnectionFactory connectionFactory) {
	RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
	RedisCacheConfiguration cacheConfiguration = this.determineConfiguration();
	List<String> cacheNames = this.cacheProperties.getCacheNames();
	Map<String, RedisCacheConfiguration> initialCaches = new LinkedHashMap<>();
	if (!cacheNames.isEmpty()) {
		Map<String, RedisCacheConfiguration> cacheConfigMap = new LinkedHashMap<>(cacheNames.size());
		cacheNames.forEach(it -> cacheConfigMap.put(it, cacheConfiguration));
		initialCaches.putAll(cacheConfigMap);
	}
	boolean allowInFlightCacheCreation = true;
	boolean enableTransactions = false;
	RedisAutoCacheManager cacheManager = new RedisAutoCacheManager(redisCacheWriter, cacheConfiguration, initialCaches, allowInFlightCacheCreation);
	cacheManager.setTransactionAware(enableTransactions);
	return this.customizerInvoker.customize(cacheManager);
}
 
Example 3
private RedisCacheConfiguration determineConfiguration() {
	if (this.redisCacheConfiguration != null) {
		return this.redisCacheConfiguration;
	} else {
		CacheProperties.Redis redisProperties = this.cacheProperties.getRedis();
		RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
		config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer));
		if (redisProperties.getTimeToLive() != null) {
			config = config.entryTtl(redisProperties.getTimeToLive());
		}

		if (redisProperties.getKeyPrefix() != null) {
			config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
		}

		if (!redisProperties.isCacheNullValues()) {
			config = config.disableCachingNullValues();
		}

		if (!redisProperties.isUseKeyPrefix()) {
			config = config.disableKeyPrefix();
		}

		return config;
	}
}
 
Example 4
Source Project: microservices-platform   Source File: RedisAutoConfigure.java    License: Apache License 2.0 6 votes vote down vote up
@Bean(name = "cacheManager")
@Primary
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory
        , RedisSerializer<String> redisKeySerializer, RedisSerializer<Object> redisValueSerializer) {
    RedisCacheConfiguration difConf = getDefConf(redisKeySerializer, redisValueSerializer).entryTtl(Duration.ofHours(1));

    //自定义的缓存过期时间配置
    int configSize = cacheManagerProperties.getConfigs() == null ? 0 : cacheManagerProperties.getConfigs().size();
    Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(configSize);
    if (configSize > 0) {
        cacheManagerProperties.getConfigs().forEach(e -> {
            RedisCacheConfiguration conf = getDefConf(redisKeySerializer, redisValueSerializer).entryTtl(Duration.ofSeconds(e.getSecond()));
            redisCacheConfigurationMap.put(e.getKey(), conf);
        });
    }

    return RedisCacheManager.builder(redisConnectionFactory)
            .cacheDefaults(difConf)
            .withInitialCacheConfigurations(redisCacheConfigurationMap)
            .build();
}
 
Example 5
Source Project: spring-microservice-exam   Source File: MultitenantCacheManager.java    License: MIT License 6 votes vote down vote up
/**
 * 扩展@Cacheable,支持配置失效时间
 *
 * @param name        name
 * @param cacheConfig cacheConfig
 * @return RedisCache
 */
@Override
protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {
    if (StringUtils.isBlank(name) || !name.contains(SPLIT_FLAG)) {
        return super.createRedisCache(name, cacheConfig);
    }
    String[] cacheArray = name.split(SPLIT_FLAG);
    if (cacheArray.length < CACHE_LENGTH) {
        return super.createRedisCache(name, cacheConfig);
    }
    String cacheName = cacheArray[0];
    if (cacheConfig != null) {
        // 从系统属性里读取超时时间
        long cacheAge = Long.getLong(cacheArray[1], -1);
        cacheConfig = cacheConfig.entryTtl(Duration.ofSeconds(cacheAge));
    }
    return super.createRedisCache(cacheName, cacheConfig);
}
 
Example 6
Source Project: push   Source File: CacheConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {

    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofSeconds(30))
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()))
            .disableCachingNullValues();

    RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory)
            .cacheDefaults(config)
            .transactionAware()
            .build();

    return redisCacheManager;
}
 
Example 7
Source Project: syhthems-platform   Source File: RedisCacheConfig.java    License: MIT License 6 votes vote down vote up
@Override
@Bean
@Primary
public CacheManager cacheManager() {
    final RedisCacheWriter redisCacheWriter =
            RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
    final RedisCacheConfiguration defaultCacheConfig =
            RedisCacheConfiguration.defaultCacheConfig()

                    // 不缓存 null 值
                    .disableCachingNullValues()
                    // 使用注解时的序列化、反序列化对
                    .serializeKeysWith(stringPair)
                    .serializeValuesWith(jacksonPair)

                    .prefixKeysWith("syhthems:cache:");
    return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
 
Example 8
Source Project: syhthems-platform   Source File: RedisCacheConfig.java    License: MIT License 6 votes vote down vote up
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
@Bean
public CacheManager JDKCacheManager() {
    final RedisCacheWriter redisCacheWriter =
            RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
    final RedisCacheConfiguration defaultCacheConfig =
            RedisCacheConfiguration.defaultCacheConfig()

                    // 不缓存 null 值
                    .disableCachingNullValues()
                    // 使用注解时的序列化、反序列化对
                    .serializeKeysWith(stringPair)
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.java()))
                    .prefixKeysWith("syhthems:cache:");
    return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
 
Example 9
Source Project: spring-boot-vue-admin   Source File: MyRedisCacheManager.java    License: Apache License 2.0 6 votes vote down vote up
private void add(final String[] cacheNames, final CacheExpire cacheExpire) {
  for (final String cacheName : cacheNames) {
    if (cacheName == null || "".equals(cacheName.trim())) {
      continue;
    }
    final long expire = cacheExpire.expire();
    log.debug("cache name<{}> expire: {}", cacheName, expire);
    if (expire >= 0) {
      // 缓存配置
      final RedisCacheConfiguration config =
          RedisCacheConfiguration.defaultCacheConfig()
              .entryTtl(Duration.ofSeconds(expire))
              .disableCachingNullValues()
              // .prefixKeysWith(cacheName)
              .serializeKeysWith(STRING_PAIR)
              .serializeValuesWith(FASTJSON_PAIR);
      this.initialCacheConfiguration.put(cacheName, config);
    } else {
      log.warn("{} use default expiration.", cacheName);
    }
  }
}
 
Example 10
Source Project: black-shop   Source File: RedisCacheAutoConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
private RedisCacheConfiguration determineConfiguration(ClassLoader classLoader) {
	if (this.redisCacheConfiguration != null) {
		return this.redisCacheConfiguration;
	} else {
		CacheProperties.Redis redisProperties = this.cacheProperties.getRedis();
		RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
		config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new JdkSerializationRedisSerializer(classLoader)));
		if (redisProperties.getTimeToLive() != null) {
			config = config.entryTtl(redisProperties.getTimeToLive());
		}

		if (redisProperties.getKeyPrefix() != null) {
			config = config.prefixKeysWith(redisProperties.getKeyPrefix());
		}

		if (!redisProperties.isCacheNullValues()) {
			config = config.disableCachingNullValues();
		}

		if (!redisProperties.isUseKeyPrefix()) {
			config = config.disableKeyPrefix();
		}

		return config;
	}
}
 
Example 11
Source Project: black-shop   Source File: RedisAutoCacheManager.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected RedisCache createRedisCache(String name,@Nullable RedisCacheConfiguration cacheConfig) {
	if(StrUtil.isBlank(name) || !name.contains("#")){
		return super.createRedisCache(name, cacheConfig);
	}

	String[] cacheArray = name.split("#");
	if (cacheArray.length < 2) {
		return super.createRedisCache(name, cacheConfig);
	}
	if (cacheConfig != null) {
		long cacheTime = Long.parseLong(cacheArray[1]);
		cacheConfig = cacheConfig.entryTtl(Duration.ofSeconds(cacheTime));
	}
	return super.createRedisCache(name, cacheConfig);
}
 
Example 12
private <T> RedisCacheConfiguration determineConfiguration(
        Type type) {
    CacheProperties.Redis redisProperties = this.cacheProperties.getRedis();
    RedisCacheConfiguration config = RedisCacheConfiguration
            .defaultCacheConfig();
    Jackson2JsonRedisSerializer<T> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(TypeFactory.defaultInstance().constructType(type));
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    config = config.serializeValuesWith(RedisSerializationContext.SerializationPair
            .fromSerializer(jackson2JsonRedisSerializer));
    if (redisProperties.getTimeToLive() != null) {
        config = config.entryTtl(redisProperties.getTimeToLive());
    }
    if (redisProperties.getKeyPrefix() != null) {
        config = config.prefixKeysWith(redisProperties.getKeyPrefix());
    }
    if (!redisProperties.isCacheNullValues()) {
        config = config.disableCachingNullValues();
    }
    if (!redisProperties.isUseKeyPrefix()) {
        config = config.disableKeyPrefix();
    }
    return config;
}
 
Example 13
Source Project: cms   Source File: RedisConfigure.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 选择redis作为默认缓存工具
 *
 * @param redisConnectionFactory
 * @return
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    // 生成一个默认配置,通过config对象即可对缓存进行自定义配置
    RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
            // 设置缓存的默认过期时间,也是使用Duration设置
            .entryTtl(Duration.ofDays(7))
            // 设置 key为string序列化
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer()))
            // 设置value为json序列化
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer()))
            // 不缓存空值
            .disableCachingNullValues();
    return RedisCacheManager
            .builder(redisConnectionFactory)
            .transactionAware()
            .cacheDefaults(configuration)
            .build();
}
 
Example 14
Source Project: x7   Source File: RedisCacheConfig.java    License: Apache License 2.0 6 votes vote down vote up
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {

        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);

        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
        redisCacheConfiguration = redisCacheConfiguration
                .serializeValuesWith(
                        RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .entryTtl(Duration.ofSeconds(seconds));

        return redisCacheConfiguration;
    }
 
Example 15
Source Project: ywh-frame   Source File: RedisCacheConfig.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * 解决注解方式存放到redis中的值是乱码的情况
 * @param factory 连接工厂
 * @return CacheManager
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
    RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);

    // 配置注解方式的序列化
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
    RedisCacheConfiguration redisCacheConfiguration =
            config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer))
                    //配置注解默认的过期时间
                    .entryTtl(Duration.ofDays(1));
    // 加入白名单   https://github.com/alibaba/fastjson/wiki/enable_autotype
    ParserConfig.getGlobalInstance().addAccept("com.ywh");
    ParserConfig.getGlobalInstance().addAccept("com.baomidou");
    return RedisCacheManager.builder(factory).cacheDefaults(redisCacheConfiguration).build();
}
 
Example 16
Source Project: biliob_backend   Source File: RedisConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
    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);
    RedisSerializationContext.SerializationPair<Object> pair =
            RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer);
    return RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofHours(1))
            .serializeValuesWith(pair)
            .serializeKeysWith(
                    RedisSerializationContext.SerializationPair.fromSerializer(
                            new StringRedisSerializer()));
}
 
Example 17
Source Project: jim-framework   Source File: CustomizedRedisCacheManager.java    License: Apache License 2.0 6 votes vote down vote up
public CustomizedRedisCacheManager(
        RedisConnectionFactory connectionFactory,
        RedisOperations redisOperations,
        List<CacheItemConfig> cacheItemConfigList) {

    this(
            RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory),
            RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(30)),
            cacheItemConfigList
                    .stream()
                    .collect(Collectors.toMap(CacheItemConfig::getName,cacheItemConfig -> {
                        RedisCacheConfiguration cacheConfiguration =
                                RedisCacheConfiguration
                                        .defaultCacheConfig()
                                        .entryTtl(Duration.ofSeconds(cacheItemConfig.getExpiryTimeSecond()))
                                        .prefixKeysWith(cacheItemConfig.getName());
                        return cacheConfiguration;
                    }))
    );
    this.redisOperations=redisOperations;
    CacheContainer.init(cacheItemConfigList);

}
 
Example 18
Source Project: DouBiNovel   Source File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Override
    @Bean
    public CacheManager cacheManager() {

        // 设置序列化
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);

        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer))
                // value序列化方式
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer))
//                .disableCachingNullValues()
                // 缓存过期时间
                .entryTtl(Duration.ofMinutes(5));

        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder
                .fromConnectionFactory(lettuceConnectionFactory)
                .cacheDefaults(config)
                .transactionAware();
        @SuppressWarnings("serial")
        Set<String> cacheNames = new HashSet<String>() {
            {
                add("codeNameCache");
            }
        };
        builder.initialCacheNames(cacheNames);
        return builder.build();
    }
 
Example 19
MicaRedisCacheAutoConfiguration(RedisSerializer<Object> redisSerializer,
								CacheProperties cacheProperties,
								CacheManagerCustomizers customizerInvoker,
								ObjectProvider<RedisCacheConfiguration> redisCacheConfiguration) {
	this.redisSerializer = redisSerializer;
	this.cacheProperties = cacheProperties;
	this.customizerInvoker = customizerInvoker;
	this.redisCacheConfiguration = redisCacheConfiguration.getIfAvailable();
}
 
Example 20
Source Project: sk-admin   Source File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 设置 redis 数据默认过期时间,默认2小时
 * 设置@cacheable 序列化方式
 */
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
    FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
    RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
    configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofHours(2));
    return configuration;
}
 
Example 21
Source Project: ultimate-redis-boot   Source File: CacheConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, CacheConfigurationProperties properties) {
    Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();

    for (Entry<String, Long> cacheNameAndTimeout : properties.getCacheExpirations().entrySet()) {
        cacheConfigurations.put(cacheNameAndTimeout.getKey(), createCacheConfiguration(cacheNameAndTimeout.getValue()));
    }

    return RedisCacheManager
            .builder(redisConnectionFactory)
            .cacheDefaults(cacheConfiguration(properties))
            .withInitialCacheConfigurations(cacheConfigurations).build();
}
 
Example 22
Source Project: microservices-platform   Source File: RedisAutoConfigure.java    License: Apache License 2.0 5 votes vote down vote up
private RedisCacheConfiguration getDefConf(RedisSerializer<String> redisKeySerializer, RedisSerializer<Object> redisValueSerializer) {
    return RedisCacheConfiguration.defaultCacheConfig()
            .disableCachingNullValues()
            .computePrefixWith(cacheName -> "cache".concat(":").concat(cacheName).concat(":"))
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisKeySerializer))
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisValueSerializer));
}
 
Example 23
Source Project: open-cloud   Source File: RedisCacheAutoConfiguration.java    License: MIT License 5 votes vote down vote up
/**
 * 配置缓存管理器
 *
 * @param redisConnectionFactory
 * @return
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    // 生成一个默认配置,通过config对象即可对缓存进行自定义配置
    RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
    // 设置缓存的默认过期时间,也是使用Duration设置
    redisCacheConfiguration = redisCacheConfiguration
            // 设置缓存有效期一小时
            .entryTtl(Duration.ofHours(1));
    return RedisCacheManager
            .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
            .cacheDefaults(redisCacheConfiguration).build();
}
 
Example 24
Source Project: code   Source File: MyRedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 *  RedisCache 自定义序列化规则
 */
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
    RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
    configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer())).entryTtl(Duration.ofDays(30));
    return configuration;
}
 
Example 25
Source Project: code   Source File: MyRedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
    RedisCacheConfiguration config = redisCacheConfiguration();
    RedisCacheManager cm = RedisCacheManager.builder(connectionFactory)
            .cacheDefaults(config)
            .build();
    return cm;
}
 
Example 26
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(16);
//        redisCacheConfigurationMap.put("UserInfoList", this.getRedisCacheConfigurationWithTtl(3000));
//        redisCacheConfigurationMap.put("UserInfoListAnother", this.getRedisCacheConfigurationWithTtl(18000));

        return redisCacheConfigurationMap;
    }
 
Example 27
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {

        FstRedisSerializer kryoRedisSerializer = new FstRedisSerializer();

        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
        redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
                RedisSerializationContext
                        .SerializationPair
                        .fromSerializer(kryoRedisSerializer)
        ).entryTtl(Duration.ofSeconds(seconds));

        return redisCacheConfiguration;
    }
 
Example 28
Source Project: White-Jotter   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
    RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
    //设置Redis缓存有效期为1天
    RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer())).entryTtl(Duration.ofDays(1));
    return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
}
 
Example 29
Source Project: light-reading-cloud   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
/** 缓存管理器 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    //初始化一个RedisCacheWriter
    RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
    //设置CacheManager的值序列化方式为json序列化
    RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
    RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer);
    RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);
    //设置默认超过期时间是30秒
    defaultCacheConfig = defaultCacheConfig.entryTtl(Duration.ofSeconds(300));
    //初始化RedisCacheManager
    return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
 
Example 30
Source Project: light-reading-cloud   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
/** 缓存管理器 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    //初始化一个RedisCacheWriter
    RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
    //设置CacheManager的值序列化方式为json序列化
    RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
    RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer);
    RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);
    //设置默认超过期时间是30秒
    defaultCacheConfig = defaultCacheConfig.entryTtl(Duration.ofSeconds(300));
    //初始化RedisCacheManager
    return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}