<返回更多

在Java中使用Redis的方法

2022-06-30    杂文论
加入收藏

前提

可供访问的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("校验失败");

}

}

}

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>