当前位置:网站首页>Redis configuration class redisconfig

Redis configuration class redisconfig

2022-07-07 01:56:00 Novice Zhang~

package com.menglar.soap.item.common.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.menglar.soap.item.common.listener.KeyExpiredListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericToStringSerializer;

/** * @program: soap-item_v2 * @description: redis Configuration class  * @author: ZhangRiTian * @create: 2021-10-25 09:24 */
@Configuration
public class RedisConfig {
    

	@Autowired
	private RedisConnectionFactory redisConnectionFactory;

	/** * Listener connection factory class  */
	@Bean
	public RedisMessageListenerContainer redisMessageListenerContainer() {
    
		RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
		redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
		return redisMessageListenerContainer;
	}

	/** * Monitor  */
	@Bean
	public KeyExpiredListener keyExpiredListener() {
    
		return new KeyExpiredListener(this.redisMessageListenerContainer());
	}



@Bean
	public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    
		RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(redisConnectionFactory);
		//  Use  GenericFastJsonRedisSerializer  Replace default serialization 
		GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
		//  Set up key and value Serialization rules 
		redisTemplate.setKeySerializer(new GenericToStringSerializer<>(Object.class));
		redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);
		//  Set up hashKey and hashValue Serialization rules 
		redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
		redisTemplate.setHashValueSerializer(genericFastJsonRedisSerializer);
	//  Set up supporting things 
	redisTemplate.setEnableTransactionSupport(true);
	redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}
}

原网站

版权声明
本文为[Novice Zhang~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130911190188.html