前提
可供訪問的redis服務器 可以自己在本地啟動虛擬機
如何在本地啟動一個Redis參考bilibili尚硅谷Redis6
SpringBoot項目中需要添加的依賴
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
常見的用法
工具類 用于獲取Redis連接
public class JedisUtils {
public static Jedis getJedisClient(){
Jedis jedis = new Jedis("192.168.110.101",6379);
return jedis;
}
}
檢測本地Redis是否可以使用
@Test
public void pingTest(String[] args) {
Jedis jedis = new Jedis("192.168.110.101",6379);
String ping = jedis.ping();
// 當Redis能使用時 會輸出pong
System.out.println(ping);
}
關于Redis String 類型的API
@Test
public void StringTest(){
Jedis jedis = new Jedis("192.168.110.101",6379);
jedis.set("name","lucy");
//redis 批量添加多個k v ?
jedis.mset("k1","v1","k2","v2");
List<String> mget = jedis.mget("k1", "k2");
String name = jedis.get("name");
System.out.println(name);
}
關于Jedis set 類型API
@Test
public void setTest(){
//操作set 集合
Jedis jedisClient = JedisUtils.getJedisClient();
//set中添加元素
jedisClient.sadd("name1","lucy","mary","jack");
Set<String> name = jedisClient.smembers("name1");
//set中刪除元素
jedisClient.srem("name1","lucy");
Set<String> name1 = jedisClient.smembers("name1");
System.out.println(name1);
System.out.println(name);
}
關于Jedis hash類型的API
@Test
public void hashTest(){
// hash的兩種添加值方式以及取值方式
Jedis jedisClient = JedisUtils.getJedisClient();
jedisClient.hset("hset","lucy","20");
String hget = jedisClient.hget("hset", "lucy");
System.out.println(hget);
Map<String,String> hashTsetMap = new HashMap<>(16);
hashTsetMap.put("jack","30");
jedisClient.hset("hset",hashTsetMap);
List<String> age = jedisClient.hmget("hset","lucy");
System.out.println(age);
jedisClient.hdel("hset","lucy");
String lucy = jedisClient.hget("hset", "lucy");
System.out.println(lucy);
}
zSet類型API
@Test
public void zSetTest(){
Jedis jedisClient = JedisUtils.getJedisClient();
jedisClient.zadd("china",100d,"shanghai");
Set<String> china = jedisClient.zrange("china", 0, -1);
System.out.println(china);
}
使用Redis實現一個簡易版短信注冊功能
/**
* 注冊功能
* 生成手機驗證碼 且五分鐘內有效
* 1.連接redis
* 2.判斷驗證碼生成的次數
* 3.生成驗證碼 調用短信發送API
* 4.前臺回傳驗證碼 ,校驗驗證碼的有效性
* 5.注冊成功
* @param args
*/
//
public void getMessage() {
try (Jedis jedisClient=JedisUtils.getJedisClient()){
String phone = "12345678";
if (getGenerateTimes(phone,jedisClient)){
String verificationCode = getVerificationCode(phone, jedisClient);
System.out.println(verificationCode);
}else {
System.out.println("超過短信次數");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static final String KEY = "generateTime";
public static final Integer Max_TIME = 2;
public static final Integer MINI_TIME = 0;
// 計算一天發送短信的次數 不能超過三次
public static boolean getGenerateTimes(String phone, Jedis client){
String times = client.get(phone+KEY);
if (times==null){
client.setex(phone+KEY, 24*60*60,MINI_TIME.toString());
}
// 使用Interger.valueOf不能轉換?
//基本類的包裝類無法自動拆箱進行相互比較
if (Integer.parseInt(times)>Max_TIME.intValue()){
return false;
}
client.incr(phone+KEY);
return true;
}
/**
* 生成六位數驗證碼 調用api發送短信到手機
* @param phone
* @param client
* @return
*/
public static String getVerificationCode(String phone, Jedis client){
Random random = new Random();
StringBuffer stringBuffer = new StringBuffer();
// 有更好的方法生成六位隨機數
for (int j = 0; j < 6; j++) {
stringBuffer.Append(random.nextInt(10));
}
// 調用短信API發送 并做對應的業務判斷
// 發送短信成功 將數據放入redis 并設置過期時間為五分鐘
String setex = client.setex(phone, 300, stringBuffer.toString());
return stringBuffer.toString();
}
public static Boolean verification(String phone, String code, Jedis client){
String storeCode = client.get(phone);
if (code!=null&&code.equals(storeCode)){
return Boolean.TRUE;
}
return Boolean.FALSE;
}
@Test
public void verificationTest(){
try(Jedis jedisClient = JedisUtils.getJedisClient()) {
if (verification("12345678","465481",jedisClient)){
System.out.println("校驗成功");
}else {
System.out.println("校驗失敗");
}
}
}