前提
可供訪(fǎng)問(wèn)的redis服務(wù)器 可以自己在本地啟動(dòng)虛擬機(jī)
如何在本地啟動(dòng)一個(gè)Redis參考bilibili尚硅谷Redis6
SpringBoot項(xiàng)目中需要添加的依賴(lài)
<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>
常見(jiàn)的用法
工具類(lèi) 用于獲取Redis連接
public class JedisUtils {
public static Jedis getJedisClient(){
Jedis jedis = new Jedis("192.168.110.101",6379);
return jedis;
}
}
檢測(cè)本地Redis是否可以使用
@Test
public void pingTest(String[] args) {
Jedis jedis = new Jedis("192.168.110.101",6379);
String ping = jedis.ping();
// 當(dāng)Redis能使用時(shí) 會(huì)輸出pong
System.out.println(ping);
}
關(guān)于Redis String 類(lèi)型的API
@Test
public void StringTest(){
Jedis jedis = new Jedis("192.168.110.101",6379);
jedis.set("name","lucy");
//redis 批量添加多個(gè)k v ?
jedis.mset("k1","v1","k2","v2");
List<String> mget = jedis.mget("k1", "k2");
String name = jedis.get("name");
System.out.println(name);
}
關(guān)于Jedis set 類(lèi)型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);
}
關(guān)于Jedis hash類(lèi)型的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類(lèi)型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實(shí)現(xiàn)一個(gè)簡(jiǎn)易版短信注冊(cè)功能
/**
* 注冊(cè)功能
* 生成手機(jī)驗(yàn)證碼 且五分鐘內(nèi)有效
* 1.連接redis
* 2.判斷驗(yàn)證碼生成的次數(shù)
* 3.生成驗(yàn)證碼 調(diào)用短信發(fā)送API
* 4.前臺(tái)回傳驗(yàn)證碼 ,校驗(yàn)驗(yàn)證碼的有效性
* 5.注冊(cè)成功
* @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("超過(guò)短信次數(shù)");
}
} 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;
// 計(jì)算一天發(fā)送短信的次數(shù) 不能超過(guò)三次
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不能轉(zhuǎn)換?
//基本類(lèi)的包裝類(lèi)無(wú)法自動(dòng)拆箱進(jìn)行相互比較
if (Integer.parseInt(times)>Max_TIME.intValue()){
return false;
}
client.incr(phone+KEY);
return true;
}
/**
* 生成六位數(shù)驗(yàn)證碼 調(diào)用api發(fā)送短信到手機(jī)
* @param phone
* @param client
* @return
*/
public static String getVerificationCode(String phone, Jedis client){
Random random = new Random();
StringBuffer stringBuffer = new StringBuffer();
// 有更好的方法生成六位隨機(jī)數(shù)
for (int j = 0; j < 6; j++) {
stringBuffer.Append(random.nextInt(10));
}
// 調(diào)用短信API發(fā)送 并做對(duì)應(yīng)的業(yè)務(wù)判斷
// 發(fā)送短信成功 將數(shù)據(jù)放入redis 并設(shè)置過(guò)期時(shí)間為五分鐘
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("校驗(yàn)成功");
}else {
System.out.println("校驗(yàn)失敗");
}
}
}






