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 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 #2
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 #3
Source File: RateLimiterPluginDataHandler.java    From soul with 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 #4
Source File: RedisCacheConfig.java    From syhthems-platform with 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 #5
Source File: RedisCacheAutoConfiguration.java    From faster-framework-project with 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 #6
Source File: RedisConfigure.java    From cms with 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 #7
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 #8
Source File: RedisConfig.java    From biliob_backend with 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 #9
Source File: IndexDocTests.java    From spring-session with 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 #10
Source File: RedisCacheConfig.java    From ywh-frame with 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 #11
Source File: RedisWebSessionConfigurationTests.java    From spring-session with 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 #12
Source File: RedisCacheConfig.java    From x7 with 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 #13
Source File: JetLinksRedisConfiguration.java    From jetlinks-community with 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 #14
Source File: RedisConfig.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Post> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
    return new ReactiveRedisTemplate<String, Post>(
        factory,
        RedisSerializationContext.fromSerializer(new Jackson2JsonRedisSerializer(Post.class))
    );
}
 
Example #15
Source File: RedisConfig.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Post> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
    return new ReactiveRedisTemplate<String, Post>(
        factory,
        RedisSerializationContext.fromSerializer(new Jackson2JsonRedisSerializer(Post.class))
    );
}
 
Example #16
Source File: DemoApplication.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Post> reactiveJsonPostRedisTemplate(
    ReactiveRedisConnectionFactory connectionFactory) {

    RedisSerializationContext<String, Post> serializationContext = RedisSerializationContext
        .<String, Post>newSerializationContext(new StringRedisSerializer())
        .hashKey(new StringRedisSerializer())
        .hashValue(new Jackson2JsonRedisSerializer<>(Post.class))
        .build();


    return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example #17
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;
}
 
Example #18
Source File: RedisCacheConfig.java    From iot-dc3 with 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();
}
 
Example #19
Source File: RedisCacheConfigurer.java    From bird-java with MIT License 5 votes vote down vote up
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
    RedisCacheWriter cacheWriter = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);

    RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofMinutes(10))
            .prefixKeysWith(cachePrefix + ":")
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

    return new RedisCacheManager(cacheWriter, cacheConfiguration);
}
 
Example #20
Source File: RedisConfig.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 配置使用注解的时候缓存配置,默认是序列化反序列化的形式,加上此配置则为 json 形式
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
    // 配置序列化
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
    RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

    return RedisCacheManager.builder(factory).cacheDefaults(redisCacheConfiguration).build();
}
 
Example #21
Source File: RedisConfig.java    From SpringCloud with Apache License 2.0 5 votes vote down vote up
@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 #22
Source File: RedisCachingConfig.java    From POC with Apache License 2.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Book> reactiveJsonBookRedisTemplate(
		ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {

	RedisSerializationContext<String, Book> serializationContext = RedisSerializationContext
			.<String, Book>newSerializationContext(new StringRedisSerializer()).hashKey(new StringRedisSerializer())
			.hashValue(new Jackson2JsonRedisSerializer<>(Book.class)).build();

	return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
}
 
Example #23
Source File: RedisTestConfiguration.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Configures a {@link ReactiveRedisTemplate} with {@link String} keys and a typed
 * {@link Jackson2JsonRedisSerializer}.
 */
@Bean
public ReactiveRedisTemplate<String, Person> reactiveJsonPersonRedisTemplate(
		ReactiveRedisConnectionFactory connectionFactory) {

	Jackson2JsonRedisSerializer<Person> serializer = new Jackson2JsonRedisSerializer<>(Person.class);
	RedisSerializationContextBuilder<String, Person> builder = RedisSerializationContext
			.newSerializationContext(new StringRedisSerializer());

	RedisSerializationContext<String, Person> serializationContext = builder.value(serializer).build();

	return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example #24
Source File: RedisTestConfiguration.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Configures a {@link ReactiveRedisTemplate} with {@link String} keys and {@link GenericJackson2JsonRedisSerializer}.
 */
@Bean
public ReactiveRedisTemplate<String, Object> reactiveJsonObjectRedisTemplate(
		ReactiveRedisConnectionFactory connectionFactory) {

	RedisSerializationContextBuilder<String, Object> builder = RedisSerializationContext
			.newSerializationContext(new StringRedisSerializer());

	RedisSerializationContext<String, Object> serializationContext = builder
			.value(new GenericJackson2JsonRedisSerializer("_type")).build();

	return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example #25
Source File: RedisWebSessionConfiguration.java    From spring-session with Apache License 2.0 5 votes vote down vote up
private ReactiveRedisTemplate<String, Object> createReactiveRedisTemplate() {
	RedisSerializer<String> keySerializer = new StringRedisSerializer();
	RedisSerializer<Object> defaultSerializer = (this.defaultRedisSerializer != null) ? this.defaultRedisSerializer
			: new JdkSerializationRedisSerializer(this.classLoader);
	RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext
			.<String, Object>newSerializationContext(defaultSerializer).key(keySerializer).hashKey(keySerializer)
			.build();
	return new ReactiveRedisTemplate<>(this.redisConnectionFactory, serializationContext);
}
 
Example #26
Source File: RedisConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Employee> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
    Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<>(Employee.class);
    RedisSerializationContext.RedisSerializationContextBuilder<String, Employee> builder = RedisSerializationContext.newSerializationContext(new StringRedisSerializer());
    RedisSerializationContext<String, Employee> context = builder.value(serializer)
        .build();
    return new ReactiveRedisTemplate<>(factory, context);
}
 
Example #27
Source File: RedisConfig.java    From DouBiNovel with 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 #28
Source File: RedisConfig.java    From yshopmall with 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 File: RedisConfig.java    From sk-admin with 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 #30
Source File: MyRedisConfig.java    From code with 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;
}