Spring對Redis的支持是使用Spring Data Redis來實現的,通常使用Jedis或者lettuce(默認),Java客戶端在 org.springframework.boot.autoconfigure.data.redis(Spring Boot 2.x) 中redis的自動配置 AutoConfigureDataRedis redis
RedisAutoConfiguration提供了RedisTemplate與StringRedisTemplate(只針對鍵值都是字符型的數據)模板,其中註解 @ConditionalOnMissingBean 是關鍵,代表該Bean若是在Spring中已經存在,則忽略,若是沒有存在則在此處註冊由Spring管理,也就是說咱們能夠「重寫」該bean,實現本身的RedisTemplate與StringRedisTemplate,事實上,是要須要重寫的,理由以下:spring
@Bean @ConditionalOnMissingBean( name = {"redisTemplate"} ) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; }
1)須要spring-boot-starter-cache依賴,管理緩存緩存
<!-- Spring Boot Cache --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
2)須要spring-boot-starter-data-redis依賴(注:spring boot 2.x改成在data下),支持redis:主要覺得Jedis客戶端爲主,排除默認的lettuce做爲客戶端的依賴服務器
<!-- Redis Cache --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <!-- 排除lettuce包,使用jedis代替--> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency>
3)須要jedis-client依賴(注:Redis Client 3版本以上會報錯與spring-boot-starter-data-redis衝突,最好使用2.9.x),使用jedis做爲客戶端app
<!-- Redis Client 3版本以上會報錯與spring-boot-starter-data-redis衝突 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
建立RedisConfig配置類,增長@Configuration註解,同時開啓緩存管理支持(添加註解@EnableCaching),繼承CachingConfigurerSupport重寫key生成策略ide
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { /** * 生成key的策略:根據類名+方法名+全部參數的值生成惟一的一個key * @return */ @Bean @Override public KeyGenerator keyGenerator() { return (Object target, Method method, Object... params) -> { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); }; } }
以後使用的application.yml配置文件,其中這裏已經選擇jedis做爲客戶端。spring-boot
# redis 配置
redis:
port: 6379
# Redis服務器鏈接密碼(默認爲空)
password:
host: xxx.xxx.xxx.xxx
database: 0
jedis:
pool:
#鏈接池最大鏈接數(使用負值表示沒有限制)
max-active: 300
# 鏈接池中的最小空閒鏈接
max-idle: 100
# 鏈接池最大阻塞等待時間(使用負值表示沒有限制)
max-wait: 10000
# 鏈接超時時間(毫秒)
timeout: 5000
同時讀取配置屬性,注入JedisPoolConfig工具
/** * redis配置屬性讀取 */ @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.database}") private int database; @Value("${spring.redis.jedis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.jedis.pool.max-wait}") private long maxWaitMillis; @Value("${spring.redis.jedis.pool.max-active}") private int maxActive; /** * JedisPoolConfig配置 * @return */ @Bean public JedisPoolConfig jedisPoolConfig() { log.info("初始化JedisPoolConfig"); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(maxActive); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); jedisPoolConfig.setMaxIdle(maxIdle); return jedisPoolConfig; }
針對RedisTemplate或StringRedisTemplate進行序列化,同時重寫註冊Bean測試
RedisTemplate默認使用JdkSerializationRedisSerializer,StringRedisTmeplate默認使用的是StringRedisSerializer。但都是不符合實際要求的ui
/** * 從新實現RedisTemplate:解決序列化問題 * @param redisConnectionFactory * @return */ @Bean @SuppressWarnings({"rawtype", "unchecked"}) public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<String, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); // 設置任何字段可見 om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 設置不是final的屬性能夠轉換 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); log.info("objectMapper: {}", om); jackson2JsonRedisSerializer.setObjectMapper(om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key採用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key採用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式採用jackson序列化方式 template.setValueSerializer(jackson2JsonRedisSerializer); // hash的value序列化方式採用jackson序列化方式 template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); template.setEnableTransactionSupport(true); return template; } /** * 從新實現StringRedisTmeplate:鍵值都是String的的數據 * @param redisConnectionFactory * @return */ @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { StringRedisTemplate template = new StringRedisTemplate(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); template.setConnectionFactory(redisConnectionFactory); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key採用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key採用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式採用jackson序列化方式 template.setValueSerializer(jackson2JsonRedisSerializer); // hash的value序列化方式採用jackson序列化方式 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; }
注意Spring Boot 1.x與Spring Boot 2.x的區別,已在代碼中註釋代表,Spring Boot 1.x使用的是JedisConnectionFactory 。而Spring Boot 2.x使用的是RedisStandaloneConfiguration ,以後傳入JedisConnectionFactory返回Bean
/** * 注入RedisConnectionFactory * @return */ @Bean public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) { log.info("初始化JedisConnectionFactory"); /* 在Spring Boot 1.x中已通過時,採用RedisStandaloneConfiguration配置 JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig); jedisConnectionFactory.setHostName(host); jedisConnectionFactory.setDatabase(database);*/ // JedisConnectionFactory配置hsot、database、password等參數 RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setHostName(host); redisStandaloneConfiguration.setPort(port); redisStandaloneConfiguration.setDatabase(database); // JedisConnectionFactory配置jedisPoolConfig JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisPoolConfigBuilder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder(); jedisPoolConfigBuilder.poolConfig(jedisPoolConfig); return new JedisConnectionFactory(redisStandaloneConfiguration); }
/** * * @author jian * @date 2019/4/14 * @description * 1) RedisTemplate(或StringRedisTemplate)雖然已經自動配置,可是不靈活(第一沒有序列化,第二泛型爲<Object, Object>不是咱們想要的類型) * 因此本身實現RedisTemplate或StringRedisTemplate) * 2) 採用RedisCacheManager做爲緩存管理器 * */ @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { private static final Logger log = LoggerFactory.getLogger(RedisConfig.class); /** * redis配置屬性讀取 */ @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.database}") private int database; @Value("${spring.redis.jedis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.jedis.pool.max-wait}") private long maxWaitMillis; @Value("${spring.redis.jedis.pool.max-active}") private int maxActive; /** * JedisPoolConfig配置 * @return */ @Bean public JedisPoolConfig jedisPoolConfig() { log.info("初始化JedisPoolConfig"); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(maxActive); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); jedisPoolConfig.setMaxIdle(maxIdle); return jedisPoolConfig; } /** * 注入RedisConnectionFactory * @return */ @Bean public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) { log.info("初始化JedisConnectionFactory"); /* 在Spring Boot 1.x中已通過時,採用RedisStandaloneConfiguration配置 JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig); jedisConnectionFactory.setHostName(host); jedisConnectionFactory.setDatabase(database);*/ // JedisConnectionFactory配置hsot、database、password等參數 RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setHostName(host); redisStandaloneConfiguration.setPort(port); redisStandaloneConfiguration.setDatabase(database); // JedisConnectionFactory配置jedisPoolConfig JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisPoolConfigBuilder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder(); jedisPoolConfigBuilder.poolConfig(jedisPoolConfig); return new JedisConnectionFactory(redisStandaloneConfiguration); } /** * 採用RedisCacheManager做爲緩存管理器 * @param connectionFactory */ @Bean public CacheManager cacheManager(RedisConnectionFactory connectionFactory) { RedisCacheManager redisCacheManager = RedisCacheManager.create(connectionFactory); return redisCacheManager; } /** * 生成key的策略:根據類名+方法名+全部參數的值生成惟一的一個key * @return */ @Bean @Override public KeyGenerator keyGenerator() { return (Object target, Method method, Object... params) -> { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); }; } /** * 從新實現RedisTemplate:解決序列化問題 * @param redisConnectionFactory * @return */ @Bean @SuppressWarnings({"rawtype", "unchecked"}) public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<String, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); // 設置任何字段可見 om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 設置不是final的屬性能夠轉換 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); log.info("objectMapper: {}", om); jackson2JsonRedisSerializer.setObjectMapper(om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key採用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key採用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式採用jackson序列化方式 template.setValueSerializer(jackson2JsonRedisSerializer); // hash的value序列化方式採用jackson序列化方式 template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); template.setEnableTransactionSupport(true); return template; } /** * 從新實現StringRedisTmeplate:鍵值都是String的的數據 * @param redisConnectionFactory * @return */ @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { StringRedisTemplate template = new StringRedisTemplate(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); template.setConnectionFactory(redisConnectionFactory); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key採用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key採用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式採用jackson序列化方式 template.setValueSerializer(jackson2JsonRedisSerializer); // hash的value序列化方式採用jackson序列化方式 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } }
雖然RedisTemplate與StringRedisTemplate模板有提供的主要數據訪問方法:
可是相關比較抽象,實現起來比較複雜,有必要進一步封裝,好比使用redisTmeplate中的簡單value的get操做:
Object result = null; ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); result = operations.get(key);
可是封裝以後,相對客戶端用戶來講比較明瞭
/** * 讀取緩存 * * @param key * @return */ public Object get(final String key) { Object result = null; ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); result = operations.get(key); return result; }
@Component public class RedisUtils { @Autowired private RedisTemplate redisTemplate; /** * 批量刪除對應的value * * @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 批量刪除key * * @param pattern */ public void removePattern(final String pattern) { Set<Serializable> keys = redisTemplate.keys(pattern); if (keys.size() > 0) { redisTemplate.delete(keys); } } /** * 刪除對應的value * * @param key */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判斷緩存中是否有對應的value * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** * 讀取緩存 * * @param key * @return */ public Object get(final String key) { Object result = null; ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); result = operations.get(key); return result; } /** * 寫入緩存 * * @param key * @param value * @return */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 寫入緩存 * * @param key * @param value * @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } }
須要注意的是必定要實現序列化,而且有序列化版本ID
public class Person implements Serializable { private final long serialVersionUID = 1L; private String id; private String name; private int age; private String gender; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Person{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } }
Redis工具類Spring已經作了管理(增長@Compent註解),使用很簡單,只須要注入RedisUtils便可
@RunWith(SpringRunner.class) @SpringBootTest public class RedisTest { @Autowired private RedisUtils redisUtils; @Test public void test(){ Person person = new Person(); person.setAge(23); person.setId("001"); person.setName("Zhangsan"); redisUtils.set("person-001", person); System.out.println(redisUtils.get("person-001")); } }
在IDE控制檯中:
在登陸客戶端後查看value值