Redis 是徹底開源免費的,遵照BSD協議,是一個高性能的key-value數據庫。前端
Redis 與其餘 key - value 緩存產品有如下三個特色:java
- Redis支持數據的持久化,能夠將內存中的數據保存在磁盤中,重啓的時候能夠再次加載進行使用。
- Redis不單單支持簡單的key-value類型的數據,同時還提供list,set,zset,hash等數據結構的存儲。
- Redis支持數據的備份,即master-slave模式的數據備份。
- 性能極高 – Redis能讀的速度是110000次/s,寫的速度是81000次/s 。
- 豐富的數據類型 – Redis支持二進制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 數據類型操做。
- 原子 – Redis的全部操做都是原子性的,意思就是要麼成功執行要麼失敗徹底不執行。單個操做是原子性的。多個操做也支持事務,即原子性,經過MULTI和EXEC指令包起來。
- 豐富的特性 – Redis還支持 publish/subscribe, 通知, key 過時等等特性。
SpringBoot整合Redis經常使用的方式有兩種,分別是Jedis和RedisTemplate。Jedis是Redis官方推薦的面向Java的操做Redis的客戶端,RedisTemplate是SpringDataRedis中對JedisApi的高度封裝。具體使用哪一種方式,根據我的需求場景,本文以Jedis爲例,整合使用Redisredis
請參照: 基於SpringBoot構建分模塊項目spring
<!-- 基於SpringBoot項目的基礎上,額外引入一下兩個jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
# Redis服務器地址 redis.host=127.0.0.1 # Redis服務器鏈接端口 redis.port=6379 # Redis服務器鏈接密碼(默認爲空) redis.password=null redis.timeout=30000 # 鏈接池最大鏈接數(使用負值表示沒有限制) redis.maxTotal=30 # 鏈接池中的最大空閒鏈接 redis.maxIdle=10 redis.numTestsPerEvictionRun=1024 redis.timeBetweenEvictionRunsMillis=30000 redis.minEvictableIdleTimeMillis=1800000 redis.softMinEvictableIdleTimeMillis=10000 # 鏈接池最大阻塞等待時間(使用負值表示沒有限制) redis.maxWaitMillis=1500 redis.testOnBorrow=true redis.testWhileIdle=true redis.blockWhenExhausted=false
package com.wayne.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * redis配置類,用於讀取redis地址、端口等基礎參數 * @author Wayne * @date 2019/4/30 */ @Configuration @PropertySource("classpath:application-dev.properties") public class RedisConfig { @Value("${redis.host}") private String host; @Value("${redis.port}") private int port; @Value("${redis.timeout}") private int timeout; @Value("${redis.maxIdle}") private int maxIdle; @Value("${redis.maxWaitMillis}") private int maxWaitMillis; @Value("${redis.blockWhenExhausted}") private Boolean blockWhenExhausted; @Value("${redis.JmxEnabled}") private Boolean JmxEnabled; @Bean public JedisPool jedisPoolFactory() { System.out.println("JedisPool注入開始..."); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); // 鏈接耗盡時是否阻塞, false報異常,true阻塞直到超時, 默認true jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted); // 是否啓用pool的jmx管理功能, 默認tru jedisPoolConfig.setJmxEnabled(JmxEnabled); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout); System.out.println("JedisPool注入成功..."); return jedisPool; } }
package com.wayne.utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; /** * Redis工具類 * @author Wayne * @date 2019/4/30 */ @Component public class RedisUtil { @Autowired private JedisPool jedisPool; /** * 向Redis中存值,永久有效 */ public String set(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.set(key, value); } catch (Exception e) { return "0"; } finally { returnResource(jedisPool, jedis); } } /** * 根據傳入Key獲取指定Value */ public String get(String key) { Jedis jedis = null; String value; try { jedis = jedisPool.getResource(); value = jedis.get(key); } catch (Exception e) { return "0"; } finally { returnResource(jedisPool, jedis); } return value; } /** * 校驗Key值是否存在 */ public Boolean exists(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.exists(key); } catch (Exception e) { return false; } finally { returnResource(jedisPool, jedis); } } /** * 刪除指定Key-Value */ public Long delete(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.del(key); } catch (Exception e) { return 0L; } finally { returnResource(jedisPool, jedis); } } // 以上爲經常使用方法,更多方法自行百度 /** * 釋放鏈接 */ private static void returnResource(JedisPool jedisPool, Jedis jedis) { if (jedis != null) { jedisPool.returnResource(jedis); } } }
此處以註冊發送短信驗證碼爲例,驗證碼有效時間爲2分鐘數據庫
@Autowired private RedisUtil redisUtil; @Test public void sendMessage() { // 驗證碼爲後臺隨機生成 final String CAPTCHA = "666666"; // 手機號爲前端傳入 final String MOBILE = "18888888888"; // 發送短信工具類 MessageUtils.sendMessage(CAPTCHA, MOBILE); // 將驗證碼存入Redis redisUtil.set(MOBILE, CAPTCHA); // 設置驗證碼過時時間爲2分鐘 redisUtil.expire(MOBILE, 60*2); System.out.println("驗證碼發送成功"); } @Test public void validateCaptcha () { // 此驗證碼和手機號均爲前端傳入 String CAPTCHA = "666666"; String MOBILE = "18888888888"; // 校驗驗證碼是否存在Redis中 if (!redisUtil.exists(MOBILE)) { System.out.println("驗證碼已過時"); return; } // 獲取Redis中的驗證碼 String tempCaptcha = redisUtil.get(MOBILE); // 校驗驗證碼 if (!CAPTCHA.equals(tempCaptcha)) { System.out.println("驗證碼錯誤"); return; } // 刪除Redis中的驗證碼 redisUtil.delete(MOBILE); }
截圖時該Key-Value 剩餘時間還有118秒,當時間變爲0時,該條數據將會自動從Redis中刪除緩存