Java Code Examples for org.springframework.data.redis.serializer.JdkSerializationRedisSerializer

The following examples show how to use org.springframework.data.redis.serializer.JdkSerializationRedisSerializer. 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
Source Project: mica   Author: lets-mica   File: RedisTemplateConfiguration.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * value 值 序列化
 *
 * @return RedisSerializer
 */
@Bean
@ConditionalOnMissingBean(RedisSerializer.class)
public RedisSerializer<Object> redisSerializer(MicaRedisProperties properties,
											   ObjectProvider<ObjectMapper> objectProvider) {
	MicaRedisProperties.SerializerType serializerType = properties.getSerializerType();
	if (MicaRedisProperties.SerializerType.JDK == serializerType) {
		return new JdkSerializationRedisSerializer();
	}
	// jackson findAndRegisterModules,use copy
	ObjectMapper objectMapper = objectProvider.getIfAvailable(ObjectMapper::new).copy();
	// findAndRegisterModules
	objectMapper.findAndRegisterModules();
	// class type info to json
	GenericJackson2JsonRedisSerializer.registerNullValueSerializer(objectMapper, null);
	objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), DefaultTyping.NON_FINAL, As.PROPERTY);
	return new GenericJackson2JsonRedisSerializer(objectMapper);
}
 
Example #2
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 #3
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 #4
Source Project: spring-boot-email-tools   Author: ozimov   File: DefaultPersistenceService.java    License: Apache License 2.0 6 votes vote down vote up
@Autowired
public DefaultPersistenceService(@Qualifier("orderingTemplate") @NonNull final StringRedisTemplate orderingTemplate,
                                 @Qualifier("valueTemplate") @NonNull final RedisTemplate<String, EmailSchedulingData> valueTemplate) {
    this.orderingTemplate = orderingTemplate;
    this.orderingTemplate.setEnableTransactionSupport(true);

    this.valueTemplate = valueTemplate;
    RedisSerializer<String> stringSerializer = new StringRedisSerializer();
    JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
    this.valueTemplate.setKeySerializer(stringSerializer);
    this.valueTemplate.setValueSerializer(jdkSerializationRedisSerializer);
    this.valueTemplate.setHashKeySerializer(stringSerializer);
    this.valueTemplate.setHashValueSerializer(stringSerializer);
    this.valueTemplate.setEnableTransactionSupport(true);
    this.valueTemplate.afterPropertiesSet();
}
 
Example #5
Source Project: spring-boot-email-tools   Author: ozimov   File: EmailRedisTemplateConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
@Qualifier("valueTemplate")
public RedisTemplate<String, EmailSchedulingData> createValueTemplate() throws IOException {
    RedisTemplate<String, EmailSchedulingData> template = new RedisTemplate<>();
    RedisSerializer<String> stringSerializer = new StringRedisSerializer();
    JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(jdkSerializationRedisSerializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(stringSerializer);

    template.setConnectionFactory(redisConnectionFactory);
    template.setEnableTransactionSupport(true);
    template.afterPropertiesSet();
    return template;
}
 
Example #6
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 #7
Source Project: spring-session   Author: spring-projects   File: RedisIndexedSessionRepositoryTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void onMessageCreated() {
	MapSession session = this.cached;
	byte[] pattern = "".getBytes(StandardCharsets.UTF_8);
	String channel = "spring:session:event:0:created:" + session.getId();
	JdkSerializationRedisSerializer defaultSerailizer = new JdkSerializationRedisSerializer();
	this.redisRepository.setDefaultSerializer(defaultSerailizer);
	byte[] body = defaultSerailizer.serialize(new HashMap());
	DefaultMessage message = new DefaultMessage(channel.getBytes(StandardCharsets.UTF_8), body);

	this.redisRepository.setApplicationEventPublisher(this.publisher);

	this.redisRepository.onMessage(message, pattern);

	verify(this.publisher).publishEvent(this.event.capture());
	assertThat(this.event.getValue().getSessionId()).isEqualTo(session.getId());
}
 
Example #8
Source Project: spring-session   Author: spring-projects   File: RedisIndexedSessionRepositoryTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void onMessageCreatedInOtherDatabase() {
	JdkSerializationRedisSerializer serializer = new JdkSerializationRedisSerializer();
	this.redisRepository.setApplicationEventPublisher(this.publisher);
	this.redisRepository.setDefaultSerializer(serializer);

	MapSession session = this.cached;
	String channel = "spring:session:event:created:1:" + session.getId();
	byte[] body = serializer.serialize(new HashMap());
	DefaultMessage message = new DefaultMessage(channel.getBytes(StandardCharsets.UTF_8), body);

	this.redisRepository.onMessage(message, "".getBytes(StandardCharsets.UTF_8));

	assertThat(this.event.getAllValues()).isEmpty();
	verifyZeroInteractions(this.publisher);
}
 
Example #9
Source Project: spring-session   Author: spring-projects   File: RedisIndexedSessionRepositoryTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void onMessageDeletedInOtherDatabase() {
	JdkSerializationRedisSerializer serializer = new JdkSerializationRedisSerializer();
	this.redisRepository.setApplicationEventPublisher(this.publisher);
	this.redisRepository.setDefaultSerializer(serializer);

	MapSession session = this.cached;
	String channel = "[email protected]__:del";
	String body = "spring:session:sessions:expires:" + session.getId();
	DefaultMessage message = new DefaultMessage(channel.getBytes(StandardCharsets.UTF_8),
			body.getBytes(StandardCharsets.UTF_8));

	this.redisRepository.onMessage(message, "".getBytes(StandardCharsets.UTF_8));

	assertThat(this.event.getAllValues()).isEmpty();
	verifyZeroInteractions(this.publisher);
}
 
Example #10
Source Project: spring-session   Author: spring-projects   File: RedisIndexedSessionRepositoryTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void onMessageExpiredInOtherDatabase() {
	JdkSerializationRedisSerializer serializer = new JdkSerializationRedisSerializer();
	this.redisRepository.setApplicationEventPublisher(this.publisher);
	this.redisRepository.setDefaultSerializer(serializer);

	MapSession session = this.cached;
	String channel = "[email protected]__:expired";
	String body = "spring:session:sessions:expires:" + session.getId();
	DefaultMessage message = new DefaultMessage(channel.getBytes(StandardCharsets.UTF_8),
			body.getBytes(StandardCharsets.UTF_8));

	this.redisRepository.onMessage(message, "".getBytes(StandardCharsets.UTF_8));

	assertThat(this.event.getAllValues()).isEmpty();
	verifyZeroInteractions(this.publisher);
}
 
Example #11
Source Project: scaffold-cloud   Author: Fatezhang   File: JsonSerializerUtil.java    License: MIT License 5 votes vote down vote up
/**
 * 默认方式序列化对象
 *
 * @return
 */
public static byte[] serializeByDefaultSerializable(final Object object) {
    try {
        if (object != null) {
            return new JdkSerializationRedisSerializer().serialize(object);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }

    return new byte[0];
}
 
Example #12
Source Project: scaffold-cloud   Author: Fatezhang   File: JsonSerializerUtil.java    License: MIT License 5 votes vote down vote up
/**
 * 默认方式反序列化对象
 *
 * @param bytes
 * @return
 */
public static Object unserializeByDefaultSerializable(byte[] bytes) {
    try {
        if (bytes != null && bytes.length > 0) {
            return new JdkSerializationRedisSerializer().deserialize(bytes);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}
 
Example #13
Source Project: jt809-tcp-server   Author: ch0ice   File: TcpChannelMsgManage.java    License: MIT License 5 votes vote down vote up
@Autowired
public void setRedisTemplate(RedisTemplate redisTemplate) {
    RedisSerializer stringSerializer = new StringRedisSerializer();
    RedisSerializer jdkSerializer = new JdkSerializationRedisSerializer();
    redisTemplate.setKeySerializer(stringSerializer);
    redisTemplate.setValueSerializer(stringSerializer);
    redisTemplate.setHashKeySerializer(stringSerializer);
    redisTemplate.setHashValueSerializer(jdkSerializer);
    redisTemplate.afterPropertiesSet();
    this.redisTemplate = redisTemplate;
}
 
Example #14
Source Project: pig   Author: magicgis   File: RedisCacheConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new JdkSerializationRedisSerializer());
    return template;
}
 
Example #15
Source Project: agile-service-old   Author: choerodon   File: RedisCacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 设置数据存入 redis 的序列化方式
 *
 * @param redisTemplate redisTemplate
 * @param factory       factory
 */
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
    redisTemplate.setConnectionFactory(factory);
}
 
Example #16
Source Project: heimdall   Author: getheimdall   File: RedisConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Configures and returns {@link RedisTemplate} with Object and Object.
 * 
 * @return {@link RedisTemplate}
 */
@Bean
public RedisTemplate<Object, Object> redisTemplateObject() {

     RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
     redisTemplate.setConnectionFactory(jedisConnectionFactory());
     redisTemplate.setKeySerializer(new StringRedisSerializer());
     redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
     redisTemplate.setHashKeySerializer(new StringRedisSerializer());
     redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
     return redisTemplate;
}
 
Example #17
Source Project: heimdall   Author: getheimdall   File: RedisConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a configured {@link RedisTemplate}.
 * 
 * @return {@link RedisTemplate} Object, Object
 */
@Bean
public RedisTemplate<Object, Object> redisTemplateObject() {

     RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
     redisTemplate.setConnectionFactory(jedisConnectionFactory());
     redisTemplate.setKeySerializer(new StringRedisSerializer());
     redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
     redisTemplate.setHashKeySerializer(new StringRedisSerializer());
     redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
     return redisTemplate;
}
 
Example #18
Source Project: tx-lcn   Author: codingapi   File: RedisConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnClass(name = "org.springframework.data.redis.connection.RedisConnectionFactory")
public RedisTemplate<String, Object> functionDomainRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
    JdkSerializationRedisSerializer serializationRedisSerializer = new JdkSerializationRedisSerializer();
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setKeySerializer(stringRedisSerializer);
    redisTemplate.setHashKeySerializer(stringRedisSerializer);
    redisTemplate.setHashValueSerializer(serializationRedisSerializer);
    redisTemplate.setValueSerializer(serializationRedisSerializer);
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    return redisTemplate;
}
 
Example #19
Source Project: spring-boot-email-tools   Author: ozimov   File: BaseRedisTest.java    License: Apache License 2.0 5 votes vote down vote up
@Bean("valueTemplate")
public RedisTemplate<String, EmailSchedulingData> createValueTemplate() throws IOException {
    RedisTemplate<String, EmailSchedulingData> template = new RedisTemplate<>();
    RedisSerializer<String> stringSerializer = new StringRedisSerializer();
    JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(jdkSerializationRedisSerializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(stringSerializer);

    template.setConnectionFactory(connectionFactory);
    template.setEnableTransactionSupport(true);
    template.afterPropertiesSet();
    return template;
}
 
Example #20
Source Project: spring-boot-email-tools   Author: ozimov   File: EmailRedisTemplateConfigurationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void createValueTemplate() throws Exception {
    //Act
    RedisTemplate<String, EmailSchedulingData> template = emailRedisTemplateConfiguration.createValueTemplate();

    //Assert
    assertions.assertThat(template).isNotNull();
    assertions.assertThat(template.getKeySerializer()).isInstanceOf(StringRedisSerializer.class);
    assertions.assertThat(template.getHashKeySerializer()).isInstanceOf(StringRedisSerializer.class);
    assertions.assertThat(template.getHashValueSerializer()).isInstanceOf(StringRedisSerializer.class);
    assertions.assertThat(template.getValueSerializer()).isInstanceOf(JdkSerializationRedisSerializer.class);
}
 
Example #21
Source Project: spring-session   Author: spring-projects   File: RedisWebSessionConfiguration.java    License: 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 #22
Source Project: blade-tool   Author: chillzhuang   File: RedisTemplateConfiguration.java    License: GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * value 值 序列化
 *
 * @return RedisSerializer
 */
@Bean
@ConditionalOnMissingBean(RedisSerializer.class)
public RedisSerializer<Object> redisSerializer() {
	return new JdkSerializationRedisSerializer();
}
 
Example #23
Source Project: SpringCloud-Shop   Author: suxiongwei   File: RedisTokenManager.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setRedis (@Qualifier("stringRedisTemplate") RedisTemplate redis) {
    this.redis = redis;
    // 泛型设置成 Long 后必须更改对应的序列 化方案
    redis.setKeySerializer (new JdkSerializationRedisSerializer());
}
 
Example #24
Source Project: howsun-javaee-framework   Author: howsun   File: JedisCacheService.java    License: Apache License 2.0 4 votes vote down vote up
public JdkSerializationRedisSerializer getSerializer() {
	return serializer;
}
 
Example #25
Source Project: howsun-javaee-framework   Author: howsun   File: JedisCacheService.java    License: Apache License 2.0 4 votes vote down vote up
public void setSerializer(JdkSerializationRedisSerializer serializer) {
	this.serializer = serializer;
}