首页>代码>SpringBoot2.1.4整合Redis、Jedis通过注解形式,进行简单的字符串数据类型的增删改查>/springboot-redis/src/main/java/com/example/demo/config/SRedisConfig.java
package com.example.demo.config; import java.io.Serializable; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * Redis配置文件 * @author 程就人生 * @date 2019年12月24日 */ @Configuration @EnableCaching //启动缓存机制 public class SRedisConfig extends CachingConfigurerSupport { /** * 配置lettuce连接池(多数据源的公共参数) * GenericObjectPoolConfig不是线程安全的 * @return */ @SuppressWarnings("rawtypes") @Bean @ConfigurationProperties(prefix = "spring.redis.lettuce.pool") public GenericObjectPoolConfig redisPool() { return new GenericObjectPoolConfig<>(); } /** * 配置数据源,从配置文件中读取 * * @return */ @Bean @ConfigurationProperties(prefix = "spring.redis") public RedisStandaloneConfiguration redisConfig() { return new RedisStandaloneConfiguration(); } @SuppressWarnings("rawtypes") @Bean("factory") public JedisConnectionFactory factory(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfig) { JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder(); jpcb.poolConfig(config); return new JedisConnectionFactory(redisConfig, jpcb.build()); } @Bean("redisTemplate") public RedisTemplate<Serializable, Serializable> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<Serializable, Serializable> template = new RedisTemplate<>(); template.setConnectionFactory(factory); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new JdkSerializationRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new JdkSerializationRedisSerializer()); //开启事务 template.setEnableTransactionSupport(true); template.afterPropertiesSet(); return template; } }