org.springframework.data.redis.serializer.RedisSerializationContext Java Examples

The following examples show how to use org.springframework.data.redis.serializer.RedisSerializationContext. 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 Project: mica   Author: lets-mica   File: MicaRedisCacheAutoConfiguration.java    License: 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 #2
Source Project: jetlinks-community   Author: jetlinks   File: JetLinksRedisConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public ReactiveRedisTemplate<Object, Object> reactiveRedisTemplate(
    ReactiveRedisConnectionFactory reactiveRedisConnectionFactory, ResourceLoader resourceLoader) {

    FstSerializationRedisSerializer serializer = new FstSerializationRedisSerializer(() -> {
        FSTConfiguration configuration = FSTConfiguration.createDefaultConfiguration()
            .setForceSerializable(true);
        configuration.setClassLoader(resourceLoader.getClassLoader());
        return configuration;
    });
    @SuppressWarnings("all")
    RedisSerializationContext<Object, Object> serializationContext = RedisSerializationContext
        .newSerializationContext()
        .key((RedisSerializer)new StringRedisSerializer())
        .value(serializer)
        .hashKey(StringRedisSerializer.UTF_8)
        .hashValue(serializer)
        .build();

    return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
}
 
Example #3
Source Project: syhthems-platform   Author: ehaut   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 #4
Source Project: soul   Author: Dromara   File: RateLimiterPluginDataHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handlerPlugin(final PluginData pluginData) {
    if (Objects.nonNull(pluginData) && pluginData.getEnabled()) {
        //init redis
        RateLimiterConfig rateLimiterConfig = GsonUtils.getInstance().fromJson(pluginData.getConfig(), RateLimiterConfig.class);
        //spring data redisTemplate
        if (Objects.isNull(Singleton.INST.get(ReactiveRedisTemplate.class))
                || Objects.isNull(Singleton.INST.get(RateLimiterConfig.class))
                || !rateLimiterConfig.equals(Singleton.INST.get(RateLimiterConfig.class))) {
            LettuceConnectionFactory lettuceConnectionFactory = createLettuceConnectionFactory(rateLimiterConfig);
            lettuceConnectionFactory.afterPropertiesSet();
            RedisSerializer<String> serializer = new StringRedisSerializer();
            RedisSerializationContext<String, String> serializationContext =
                    RedisSerializationContext.<String, String>newSerializationContext().key(serializer).value(serializer).hashKey(serializer).hashValue(serializer).build();
            ReactiveRedisTemplate<String, String> reactiveRedisTemplate = new ReactiveRedisTemplate<>(lettuceConnectionFactory, serializationContext);
            Singleton.INST.single(ReactiveRedisTemplate.class, reactiveRedisTemplate);
            Singleton.INST.single(RateLimiterConfig.class, rateLimiterConfig);
        }
    }
}
 
Example #5
Source Project: black-shop   Author: lizibin   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 #6
Source Project: faster-framework-project   Author: faster-framework   File: RedisCacheAutoConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration(
        ClassLoader classLoader,
        CacheProperties cacheProperties
) {
    CacheProperties.Redis redisProperties = cacheProperties.getRedis();
    org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.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 #7
Source Project: faster-framework-project   Author: faster-framework   File: RedisGenericCacheManager.java    License: 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 #8
Source Project: x7   Author: x-ream   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 #9
Source Project: ywh-frame   Author: ywhs   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 #10
Source Project: biliob_backend   Author: Jannchie   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 #11
Source Project: cms   Author: myxzjie   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 #12
Source Project: spring-session   Author: spring-projects   File: IndexDocTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unused")
void newReactiveRedisSessionRepository() {
	LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
	RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext
			.<String, Object>newSerializationContext(new JdkSerializationRedisSerializer()).build();

	// tag::new-reactiveredissessionrepository[]
	// ... create and configure connectionFactory and serializationContext ...

	ReactiveRedisTemplate<String, Object> redisTemplate = new ReactiveRedisTemplate<>(connectionFactory,
			serializationContext);

	ReactiveSessionRepository<? extends Session> repository = new ReactiveRedisSessionRepository(redisTemplate);
	// end::new-reactiveredissessionrepository[]
}
 
Example #13
Source Project: spring-session   Author: spring-projects   File: RedisWebSessionConfigurationTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void customRedisSerializerConfig() {
	registerAndRefresh(RedisConfig.class, CustomRedisSerializerConfig.class);

	ReactiveRedisSessionRepository repository = this.context.getBean(ReactiveRedisSessionRepository.class);
	@SuppressWarnings("unchecked")
	RedisSerializer<Object> redisSerializer = this.context.getBean("springSessionDefaultRedisSerializer",
			RedisSerializer.class);
	assertThat(repository).isNotNull();
	assertThat(redisSerializer).isNotNull();
	ReactiveRedisOperations redisOperations = (ReactiveRedisOperations) ReflectionTestUtils.getField(repository,
			"sessionRedisOperations");
	assertThat(redisOperations).isNotNull();
	RedisSerializationContext serializationContext = redisOperations.getSerializationContext();
	assertThat(ReflectionTestUtils.getField(serializationContext.getValueSerializationPair().getReader(),
			"serializer")).isEqualTo(redisSerializer);
	assertThat(ReflectionTestUtils.getField(serializationContext.getValueSerializationPair().getWriter(),
			"serializer")).isEqualTo(redisSerializer);
	assertThat(ReflectionTestUtils.getField(serializationContext.getHashValueSerializationPair().getReader(),
			"serializer")).isEqualTo(redisSerializer);
	assertThat(ReflectionTestUtils.getField(serializationContext.getHashValueSerializationPair().getWriter(),
			"serializer")).isEqualTo(redisSerializer);
}
 
Example #14
Source Project: DouBiNovel   Author: luckymorning   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 #15
Source Project: sk-admin   Author: DengSinkiang   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 #16
Source Project: code   Author: lastwhispers   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 #17
Source Project: Sentinel-Dashboard-Nacos   Author: eacdy   File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, String> stringReactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory){
    RedisSerializationContext<String, String> serializationContext = RedisSerializationContext
        .<String, String>newSerializationContext(new StringRedisSerializer())
        .hashKey(new StringRedisSerializer())
        .hashValue(new StringRedisSerializer())
        .build();
    return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example #18
Source Project: mall-learning   Author: macrozheng   File: RedisConfig.java    License: Apache License 2.0 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 #19
Source Project: White-Jotter   Author: Antabot   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 #20
Source Project: kvf-admin   Author: kalvinGit   File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Primary
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        //对象的序列化
        RedisSerializationContext.SerializationPair valueSerializationPair
                = RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer());
        //全局redis缓存过期时间
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofDays(1))
//                .serializeKeysWith()
                .serializeValuesWith(valueSerializationPair);

        return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(factory), redisCacheConfiguration);
    }
 
Example #21
Source Project: spring-boot-examples   Author: laolunsi   File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 自定义 redis cache 序列化方式
 * @return
 */
@Bean
public RedisCacheConfiguration redisCacheConfiguration(){
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
    configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).entryTtl(Duration.ofDays(30));
    return configuration;
}
 
Example #22
Source Project: sophia_scaffolding   Author: SophiaLeo   File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 *  设置 redis 数据默认过期时间
 *  设置@cacheable 序列化方式
 * @return
 */
@Bean
public RedisCacheConfiguration redisCacheConfiguration(){
    FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
    RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
    configuration = configuration
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofDays(1));
    return configuration;
}
 
Example #23
Source Project: sophia_scaffolding   Author: SophiaLeo   File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 *  设置 redis 数据默认过期时间
 *  设置@cacheable 序列化方式
 * @return
 */
@Bean
public RedisCacheConfiguration redisCacheConfiguration(){
    FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
    RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
    configuration = configuration
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofDays(1));
    return configuration;
}
 
Example #24
Source Project: sophia_scaffolding   Author: SophiaLeo   File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 *  设置 redis 数据默认过期时间
 *  设置@cacheable 序列化方式
 * @return
 */
@Bean
public RedisCacheConfiguration redisCacheConfiguration(){
    FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
    RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
    configuration = configuration
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofDays(1));
    return configuration;
}
 
Example #25
Source Project: yshopmall   Author: guchengwuyue   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 #26
Source Project: mall   Author: macrozheng   File: RedisConfig.java    License: Apache License 2.0 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 #27
Source Project: hdw-dubbo   Author: JacksonTu   File: RedisConfig.java    License: Apache License 2.0 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 #28
Source Project: eladmin   Author: elunez   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 #29
Source Project: Sentinel   Author: alibaba   File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, String> stringReactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory){
    RedisSerializationContext<String, String> serializationContext = RedisSerializationContext
        .<String, String>newSerializationContext(new StringRedisSerializer())
        .hashKey(new StringRedisSerializer())
        .hashValue(new StringRedisSerializer())
        .build();
    return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example #30
Source Project: iot-dc3   Author: pnoker   File: RedisCacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 自定义 RedisCacheManager 类,主要是设置序列化,解决乱码问题
 *
 * @param factory RedisConnectionFactory
 * @return CacheManager
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    // 解决查询缓存转换异常的问题
    ObjectMapper om = new ObjectMapper();
    om.activateDefaultTyping(om.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    // 配置序列化(解决乱码的问题)
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
            .disableCachingNullValues().entryTtl(timeToLive);
    return RedisCacheManager.builder(factory).cacheDefaults(config).build();
}