Java Code Examples for org.springframework.data.redis.cache.RedisCacheConfiguration#entryTtl()

The following examples show how to use org.springframework.data.redis.cache.RedisCacheConfiguration#entryTtl() . 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
Source File: RedisAutoCacheManager.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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
Source File: MicaRedisCacheAutoConfiguration.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 3
Source File: MultitenantCacheManager.java    From spring-microservice-exam with 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 4
Source File: RedisCacheAutoConfiguration.java    From black-shop with 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 5
Source File: RedisAutoCacheManager.java    From black-shop with 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 6
Source File: RedisGenericCacheManager.java    From faster-framework-project with Apache License 2.0 6 votes vote down vote up
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 7
Source File: RedisCacheAutoConfiguration.java    From open-cloud with 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 8
Source File: RedisConfig.java    From light-reading-cloud with 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 9
Source File: RedisConfig.java    From light-reading-cloud with 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 10
Source File: RedisConfig.java    From light-reading-cloud with 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 11
Source File: RedisConfig.java    From Ffast-Java with MIT License 5 votes vote down vote up
/**
 * 设置 redis 数据默认过期时间
 * 设置@cacheable 序列化方式
 *
 * @return
 */
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {

    RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
    configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(getRedisSerializer()));
    if (expirationSecondTime > 0) {
        configuration = configuration.entryTtl(Duration.ofSeconds(expirationSecondTime));
    }
    return configuration;
}
 
Example 12
Source File: CacheConfig.java    From platform with Apache License 2.0 5 votes vote down vote up
/**
 * RedisCacheConfiguration
 * 自定义配置,支持缓存使用Json文本格式
 *
 * @return {@link RedisCacheConfiguration}
 */
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {

    // ObjectMapper & GenericJackson2JsonRedisSerializer
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
    objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
    // LocalDateTime && LocalDate
    objectMapper.registerModule(new JavaTimeModule());

    GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(objectMapper);

    // RedisCacheConfiguration
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
    config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));

    CacheProperties.Redis redisProperties = cacheProperties.getRedis();
    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;
}