前提
- 可供访问的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();
System.out.println(ping);
}
- 关于Redis String 类型的API
@Test
public void StringTest(){
Jedis jedis = new Jedis("192.168.110.101",6379);
jedis.set("name","lucy");
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(){
Jedis jedisClient = JedisUtils.getJedisClient();
jedisClient.sadd("name1","lucy","mary","jack");
Set<String> name = jedisClient.smembers("name1");
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(){
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实现一个简易版短信注册功能
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());
}
if (Integer.parseInt(times)>Max_TIME.intValue()){
return false;
}
client.incr(phone+KEY);
return true;
}
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));
}
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("校验失败");
}
}
}
|